on-screen-keyboard-detector
Version:
Detects presence of the On-Screen-Keyboard in mobile browsers
1,571 lines (1,532 loc) • 124 kB
JavaScript
/* @license
On-screen keyboard detector (OSKD) v.2.3.0
(c) 2020-2021 Matthias Seemann
OSKD may be freely distributed under the MIT license.
*/
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* a with x appended
*/
function append(x, a) {
var l = a.length;
var b = new Array(l + 1);
for (var i = 0; i < l; ++i) {
b[i] = a[i];
}
b[l] = x;
return b;
}
/**
* transform each element with f
*/
function map(f, a) {
var l = a.length;
var b = new Array(l);
for (var i = 0; i < l; ++i) {
b[i] = f(a[i]);
}
return b;
}
/**
* accumulate via left-fold
*/
function reduce(f, z, a) {
var r = z;
for (var i = 0, l = a.length; i < l; ++i) {
r = f(r, a[i], i);
}
return r;
}
/**
* remove element at index
* @throws
*/
function remove(i, a) {
if (i < 0) {
throw new TypeError('i must be >= 0');
}
var l = a.length;
if (l === 0 || i >= l) { // exit early if index beyond end of array
return a;
}
if (l === 1) { // exit early if index in bounds and length === 1
return [];
}
return unsafeRemove(i, a, l - 1);
}
/**
* Internal helper to remove element at index
*/
function unsafeRemove(i, a, l) {
var b = new Array(l);
var j;
for (j = 0; j < i; ++j) {
b[j] = a[j];
}
for (j = i; j < l; ++j) {
b[j] = a[j + 1];
}
return b;
}
/**
* find index of x in a, from the left
*/
function findIndex(x, a) {
for (var i = 0, l = a.length; i < l; ++i) {
if (x === a[i]) {
return i;
}
}
return -1;
}
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var id = function (x) { return x; };
var compose = function (f, g) { return function (x) { return f(g(x)); }; };
function curry2(f) {
function curried(a, b) {
switch (arguments.length) {
case 0: return curried;
case 1: return function (b) { return f(a, b); };
default: return f(a, b);
}
}
return curried;
}
function curry3(f) {
function curried(a, b, c) {
switch (arguments.length) {
case 0: return curried;
case 1: return curry2(function (b, c) { return f(a, b, c); });
case 2: return function (c) { return f(a, b, c); };
default: return f(a, b, c);
}
}
return curried;
}
var RelativeScheduler = /** @class */ (function () {
function RelativeScheduler(origin, scheduler) {
this.origin = origin;
this.scheduler = scheduler;
}
RelativeScheduler.prototype.currentTime = function () {
return this.scheduler.currentTime() - this.origin;
};
RelativeScheduler.prototype.scheduleTask = function (localOffset, delay, period, task) {
return this.scheduler.scheduleTask(localOffset + this.origin, delay, period, task);
};
RelativeScheduler.prototype.relative = function (origin) {
return new RelativeScheduler(origin + this.origin, this.scheduler);
};
RelativeScheduler.prototype.cancel = function (task) {
return this.scheduler.cancel(task);
};
RelativeScheduler.prototype.cancelAll = function (f) {
return this.scheduler.cancelAll(f);
};
return RelativeScheduler;
}());
/**
* Read the current time from the provided Scheduler
*/
var currentTime = function (scheduler) {
return scheduler.currentTime();
};
/**
* Schedule a task to run as soon as possible, but
* not in the current call stack
*/
var asap = curry2(function (task, scheduler) {
return scheduler.scheduleTask(0, 0, -1, task);
});
/**
* Schedule a task to run after a millisecond delay
*/
var delay = curry3(function (delay, task, scheduler) {
return scheduler.scheduleTask(0, delay, -1, task);
});
/**
* Cancel a scheduledTask
*/
var cancelTask = function (scheduledTask) {
return scheduledTask.dispose();
};
var schedulerRelativeTo = curry2(function (offset, scheduler) {
return new RelativeScheduler(offset, scheduler);
});
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* a with x appended
*/
function append$1(x, a) {
var l = a.length;
var b = new Array(l + 1);
for (var i = 0; i < l; ++i) {
b[i] = a[i];
}
b[l] = x;
return b;
}
/**
* Concats two `ArrayLike`s
*/
function concat(a, b) {
var al = a.length;
var bl = b.length;
var r = new Array(al + bl);
var i = 0;
for (i = 0; i < al; i++) {
r[i] = a[i];
}
for (var j = 0; j < bl; j++) {
r[i++] = b[j];
}
return r;
}
/**
* accumulate via left-fold
*/
function reduce$1(f, z, a) {
var r = z;
for (var i = 0, l = a.length; i < l; ++i) {
r = f(r, a[i], i);
}
return r;
}
function curry2$1(f) {
function curried(a, b) {
switch (arguments.length) {
case 0: return curried;
case 1: return function (b) { return f(a, b); };
default: return f(a, b);
}
}
return curried;
}
function curry3$1(f) {
function curried(a, b, c) {
switch (arguments.length) {
case 0: return curried;
case 1: return curry2$1(function (b, c) { return f(a, b, c); });
case 2: return function (c) { return f(a, b, c); };
default: return f(a, b, c);
}
}
return curried;
}
var disposeNone = function () { return NONE; };
var NONE = new (/** @class */ (function () {
function DisposeNone() {
}
DisposeNone.prototype.dispose = function () { };
return DisposeNone;
}()))();
var isDisposeNone = function (d) {
return d === NONE;
};
/**
* Wrap an existing disposable (which may not already have been once()d)
* so that it will only dispose its underlying resource at most once.
*/
var disposeOnce = function (disposable) {
return new DisposeOnce(disposable);
};
var DisposeOnce = /** @class */ (function () {
function DisposeOnce(disposable) {
this.disposed = false;
this.disposable = disposable;
}
DisposeOnce.prototype.dispose = function () {
if (!this.disposed) {
this.disposed = true;
if (this.disposable) {
this.disposable.dispose();
this.disposable = undefined;
}
}
};
return DisposeOnce;
}());
/** @license MIT License (c) copyright 2010 original author or authors */
/**
* Aggregate a list of disposables into a DisposeAll
*/
var disposeAll = function (ds) {
var merged = reduce$1(merge, [], ds);
return merged.length === 0 ? disposeNone() : new DisposeAll(merged);
};
/**
* Convenience to aggregate 2 disposables
*/
var disposeBoth = curry2$1(function (d1, d2) {
return disposeAll([d1, d2]);
});
var merge = function (ds, d) {
return isDisposeNone(d) ? ds
: d instanceof DisposeAll ? concat(ds, d.disposables)
: append$1(d, ds);
};
var DisposeAll = /** @class */ (function () {
function DisposeAll(disposables) {
this.disposables = disposables;
}
DisposeAll.prototype.dispose = function () {
throwIfErrors(disposeCollectErrors(this.disposables));
};
return DisposeAll;
}());
/**
* Dispose all, safely collecting errors into an array
*/
var disposeCollectErrors = function (disposables) {
return reduce$1(appendIfError, [], disposables);
};
/**
* Call dispose and if throws, append thrown error to errors
*/
var appendIfError = function (errors, d) {
try {
d.dispose();
}
catch (e) {
errors.push(e);
}
return errors;
};
/**
* Throw DisposeAllError if errors is non-empty
* @throws
*/
var throwIfErrors = function (errors) {
if (errors.length > 0) {
throw new DisposeAllError(errors.length + " errors", errors);
}
};
var DisposeAllError = /** @class */ (function () {
function DisposeAllError(message, errors) {
this.name = 'DisposeAllError';
this.message = message;
this.errors = errors;
Error.call(this, message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, DisposeAllError);
}
this.stack = "" + this.stack + formatErrorStacks(this.errors);
}
return DisposeAllError;
}());
DisposeAllError.prototype = Object.create(Error.prototype);
var formatErrorStacks = function (errors) {
return reduce$1(formatErrorStack, '', errors);
};
var formatErrorStack = function (s, e, i) {
return s + ("\n[" + (i + 1) + "] " + e.stack);
};
/** @license MIT License (c) copyright 2010-2017 original author or authors */
// Try to dispose the disposable. If it throws, send
// the error to sink.error with the provided Time value
var tryDispose = curry3$1(function (t, disposable, sink) {
try {
disposable.dispose();
}
catch (e) {
sink.error(t, e);
}
});
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */
function fatalError(e) {
setTimeout(rethrow, 0, e);
}
function rethrow(e) {
throw e;
}
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var propagateTask = function (run, value, sink) { return new PropagateRunEventTask(run, value, sink); };
var propagateEventTask = function (value, sink) { return new PropagateEventTask(value, sink); };
var propagateEndTask = function (sink) { return new PropagateEndTask(sink); };
var PropagateTask = /** @class */ (function () {
function PropagateTask(sink) {
this.sink = sink;
this.active = true;
}
PropagateTask.prototype.dispose = function () {
this.active = false;
};
PropagateTask.prototype.run = function (t) {
if (!this.active) {
return;
}
this.runIfActive(t);
};
PropagateTask.prototype.error = function (t, e) {
// TODO: Remove this check and just do this.sink.error(t, e)?
if (!this.active) {
return fatalError(e);
}
this.sink.error(t, e);
};
return PropagateTask;
}());
var PropagateRunEventTask = /** @class */ (function (_super) {
__extends(PropagateRunEventTask, _super);
function PropagateRunEventTask(runEvent, value, sink) {
var _this = _super.call(this, sink) || this;
_this.runEvent = runEvent;
_this.value = value;
return _this;
}
PropagateRunEventTask.prototype.runIfActive = function (t) {
this.runEvent(t, this.value, this.sink);
};
return PropagateRunEventTask;
}(PropagateTask));
var PropagateEventTask = /** @class */ (function (_super) {
__extends(PropagateEventTask, _super);
function PropagateEventTask(value, sink) {
var _this = _super.call(this, sink) || this;
_this.value = value;
return _this;
}
PropagateEventTask.prototype.runIfActive = function (t) {
this.sink.event(t, this.value);
};
return PropagateEventTask;
}(PropagateTask));
var PropagateEndTask = /** @class */ (function (_super) {
__extends(PropagateEndTask, _super);
function PropagateEndTask() {
return _super !== null && _super.apply(this, arguments) || this;
}
PropagateEndTask.prototype.runIfActive = function (t) {
this.sink.end(t);
};
return PropagateEndTask;
}(PropagateTask));
var PropagateErrorTask = /** @class */ (function (_super) {
__extends(PropagateErrorTask, _super);
function PropagateErrorTask(value, sink) {
var _this = _super.call(this, sink) || this;
_this.value = value;
return _this;
}
PropagateErrorTask.prototype.runIfActive = function (t) {
this.sink.error(t, this.value);
};
return PropagateErrorTask;
}(PropagateTask));
/** @license MIT License (c) copyright 2010-2017 original author or authors */
var empty = function () { return EMPTY; };
var isCanonicalEmpty = function (stream) {
return stream === EMPTY;
};
var Empty = /** @class */ (function () {
function Empty() {
}
Empty.prototype.run = function (sink, scheduler) {
return asap(propagateEndTask(sink), scheduler);
};
return Empty;
}());
var EMPTY = new Empty();
/** @license MIT License (c) copyright 2010-2017 original author or authors */
var at = function (t, x) { return new At(t, x); };
var At = /** @class */ (function () {
function At(t, x) {
this.time = t;
this.value = x;
}
At.prototype.run = function (sink, scheduler) {
return delay(this.time, propagateTask(runAt, this.value, sink), scheduler);
};
return At;
}());
function runAt(t, x, sink) {
sink.event(t, x);
sink.end(t);
}
/** @license MIT License (c) copyright 2010-2017 original author or authors */
var now = function (x) { return at(0, x); };
var SettableDisposable = /** @class */ (function () {
function SettableDisposable() {
this.disposable = undefined;
this.disposed = false;
}
SettableDisposable.prototype.setDisposable = function (disposable) {
if (this.disposable !== undefined) {
throw new Error('setDisposable called more than once');
}
this.disposable = disposable;
if (this.disposed) {
disposable.dispose();
}
};
SettableDisposable.prototype.dispose = function () {
if (this.disposed) {
return;
}
this.disposed = true;
if (this.disposable !== undefined) {
this.disposable.dispose();
}
};
return SettableDisposable;
}());
/** @license MIT License (c) copyright 2010-2017 original author or authors */
var runEffects = curry2(function (stream, scheduler) {
return new Promise(function (resolve, reject) {
return runStream(stream, scheduler, resolve, reject);
});
});
function runStream(stream, scheduler, resolve, reject) {
var disposable = new SettableDisposable();
var observer = new RunEffectsSink(resolve, reject, disposable);
disposable.setDisposable(stream.run(observer, scheduler));
}
var RunEffectsSink = /** @class */ (function () {
function RunEffectsSink(end, error, disposable) {
this._end = end;
this._error = error;
this._disposable = disposable;
this.active = true;
}
RunEffectsSink.prototype.event = function () { };
RunEffectsSink.prototype.end = function () {
if (!this.active) {
return;
}
this.dispose(this._error, this._end, undefined);
};
RunEffectsSink.prototype.error = function (_t, e) {
this.dispose(this._error, this._error, e);
};
RunEffectsSink.prototype.dispose = function (error, end, x) {
this.active = false;
tryDispose$1(error, end, x, this._disposable);
};
return RunEffectsSink;
}());
function tryDispose$1(error, end, x, disposable) {
try {
disposable.dispose();
}
catch (e) {
error(e);
return;
}
end(x);
}
/**
* Run a Stream, sending all its events to the provided Sink.
*/
var run = function (sink, scheduler, stream) {
return stream.run(sink, scheduler);
};
var RelativeSink = /** @class */ (function () {
function RelativeSink(offset, sink) {
this.sink = sink;
this.offset = offset;
}
RelativeSink.prototype.event = function (t, x) {
this.sink.event(t + this.offset, x);
};
RelativeSink.prototype.error = function (t, e) {
this.sink.error(t + this.offset, e);
};
RelativeSink.prototype.end = function (t) {
this.sink.end(t + this.offset);
};
return RelativeSink;
}());
/**
* Create a stream with its own local clock
* This transforms time from the provided scheduler's clock to a stream-local
* clock (which starts at 0), and then *back* to the scheduler's clock before
* propagating events to sink. In other words, upstream sources will see local times,
* and downstream sinks will see non-local (original) times.
*/
var withLocalTime = function (origin, stream) {
return new WithLocalTime(origin, stream);
};
var WithLocalTime = /** @class */ (function () {
function WithLocalTime(origin, source) {
this.origin = origin;
this.source = source;
}
WithLocalTime.prototype.run = function (sink, scheduler) {
return this.source.run(relativeSink(this.origin, sink), schedulerRelativeTo(this.origin, scheduler));
};
return WithLocalTime;
}());
/**
* Accumulate offsets instead of nesting RelativeSinks, which can happen
* with higher-order stream and combinators like continueWith when they're
* applied recursively.
*/
var relativeSink = function (origin, sink) {
return sink instanceof RelativeSink
? new RelativeSink(origin + sink.offset, sink.sink)
: new RelativeSink(origin, sink);
};
var Pipe = /** @class */ (function () {
function Pipe(sink) {
this.sink = sink;
}
Pipe.prototype.end = function (t) {
return this.sink.end(t);
};
Pipe.prototype.error = function (t, e) {
return this.sink.error(t, e);
};
return Pipe;
}());
var LoopSink = /** @class */ (function (_super) {
__extends(LoopSink, _super);
function LoopSink(stepper, seed, sink) {
var _this = _super.call(this, sink) || this;
_this.step = stepper;
_this.seed = seed;
return _this;
}
LoopSink.prototype.event = function (t, x) {
var result = this.step(this.seed, x);
this.seed = result.seed;
this.sink.event(t, result.value);
};
return LoopSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* Create a stream containing successive reduce results of applying f to
* the previous reduce result and the current stream item.
* @param f reducer function
* @param initial initial value
* @param stream stream to scan
* @returns new stream containing successive reduce results
*/
var scan = function (f, initial, stream) {
return new Scan(f, initial, stream);
};
var Scan = /** @class */ (function () {
function Scan(f, z, source) {
this.source = source;
this.f = f;
this.value = z;
}
Scan.prototype.run = function (sink, scheduler) {
var d1 = asap(propagateEventTask(this.value, sink), scheduler);
var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler);
return disposeBoth(d1, d2);
};
return Scan;
}());
var ScanSink = /** @class */ (function (_super) {
__extends(ScanSink, _super);
function ScanSink(f, z, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
_this.value = z;
return _this;
}
ScanSink.prototype.event = function (t, x) {
var f = this.f;
this.value = f(this.value, x);
this.sink.event(t, this.value);
};
return ScanSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var continueWith = function (f, stream) {
return new ContinueWith(f, stream);
};
var ContinueWith = /** @class */ (function () {
function ContinueWith(f, source) {
this.f = f;
this.source = source;
}
ContinueWith.prototype.run = function (sink, scheduler) {
return new ContinueWithSink(this.f, this.source, sink, scheduler);
};
return ContinueWith;
}());
var ContinueWithSink = /** @class */ (function (_super) {
__extends(ContinueWithSink, _super);
function ContinueWithSink(f, source, sink, scheduler) {
var _this = _super.call(this, sink) || this;
_this.f = f;
_this.scheduler = scheduler;
_this.active = true;
_this.disposable = disposeOnce(source.run(_this, scheduler));
return _this;
}
ContinueWithSink.prototype.event = function (t, x) {
if (!this.active) {
return;
}
this.sink.event(t, x);
};
ContinueWithSink.prototype.end = function (t) {
if (!this.active) {
return;
}
tryDispose(t, this.disposable, this.sink);
this.startNext(t, this.sink);
};
ContinueWithSink.prototype.startNext = function (t, sink) {
try {
this.disposable = this.continue(this.f, t, sink);
}
catch (e) {
sink.error(t, e);
}
};
ContinueWithSink.prototype.continue = function (f, t, sink) {
return run(sink, this.scheduler, withLocalTime(t, f()));
};
ContinueWithSink.prototype.dispose = function () {
this.active = false;
return this.disposable.dispose();
};
return ContinueWithSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2017 original author or authors */
var startWith = function (x, stream) {
return continueWith(function () { return stream; }, now(x));
};
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var Filter = /** @class */ (function () {
function Filter(p, source) {
this.p = p;
this.source = source;
}
Filter.prototype.run = function (sink, scheduler) {
return this.source.run(new FilterSink(this.p, sink), scheduler);
};
/**
* Create a filtered source, fusing adjacent filter.filter if possible
* @param {function(x:*):boolean} p filtering predicate
* @param {{run:function}} source source to filter
* @returns {Filter} filtered source
*/
Filter.create = function (p, source) {
if (isCanonicalEmpty(source)) {
return source;
}
if (source instanceof Filter) {
return new Filter(and(source.p, p), source.source);
}
return new Filter(p, source);
};
return Filter;
}());
var FilterSink = /** @class */ (function (_super) {
__extends(FilterSink, _super);
function FilterSink(p, sink) {
var _this = _super.call(this, sink) || this;
_this.p = p;
return _this;
}
FilterSink.prototype.event = function (t, x) {
var p = this.p;
p(x) && this.sink.event(t, x);
};
return FilterSink;
}(Pipe));
var and = function (p, q) { return function (x) { return p(x) && q(x); }; };
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var FilterMap = /** @class */ (function () {
function FilterMap(p, f, source) {
this.p = p;
this.f = f;
this.source = source;
}
FilterMap.prototype.run = function (sink, scheduler) {
return this.source.run(new FilterMapSink(this.p, this.f, sink), scheduler);
};
return FilterMap;
}());
var FilterMapSink = /** @class */ (function (_super) {
__extends(FilterMapSink, _super);
function FilterMapSink(p, f, sink) {
var _this = _super.call(this, sink) || this;
_this.p = p;
_this.f = f;
return _this;
}
FilterMapSink.prototype.event = function (t, x) {
var f = this.f;
var p = this.p;
p(x) && this.sink.event(t, f(x));
};
return FilterMapSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var Map = /** @class */ (function () {
function Map(f, source) {
this.f = f;
this.source = source;
}
Map.prototype.run = function (sink, scheduler) {
return this.source.run(new MapSink(this.f, sink), scheduler);
};
/**
* Create a mapped source, fusing adjacent map.map, filter.map,
* and filter.map.map if possible
* @param {function(*):*} f mapping function
* @param {{run:function}} source source to map
* @returns {Map|FilterMap} mapped source, possibly fused
*/
Map.create = function (f, source) {
if (isCanonicalEmpty(source)) {
return empty();
}
if (source instanceof Map) {
return new Map(compose(f, source.f), source.source);
}
if (source instanceof Filter) {
return new FilterMap(source.p, f, source.source);
}
return new Map(f, source);
};
return Map;
}());
var MapSink = /** @class */ (function (_super) {
__extends(MapSink, _super);
function MapSink(f, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
return _this;
}
MapSink.prototype.event = function (t, x) {
var f = this.f;
this.sink.event(t, f(x));
};
return MapSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* Transform each value in the stream by applying f to each
* @param f mapping function
* @param stream stream to map
* @returns stream containing items transformed by f
*/
var map$1 = function (f, stream) {
return Map.create(f, stream);
};
/**
* Perform a side effect for each item in the stream
* @param f side effect to execute for each item. The return value will be discarded.
* @param stream stream to tap
* @returns new stream containing the same items as this stream
*/
var tap = function (f, stream) {
return new Tap(f, stream);
};
var Tap = /** @class */ (function () {
function Tap(f, source) {
this.source = source;
this.f = f;
}
Tap.prototype.run = function (sink, scheduler) {
return this.source.run(new TapSink(this.f, sink), scheduler);
};
return Tap;
}());
var TapSink = /** @class */ (function (_super) {
__extends(TapSink, _super);
function TapSink(f, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
return _this;
}
TapSink.prototype.event = function (t, x) {
var f = this.f;
f(x);
this.sink.event(t, x);
};
return TapSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
var IndexSink = /** @class */ (function (_super) {
__extends(IndexSink, _super);
function IndexSink(i, sink) {
var _this = _super.call(this, sink) || this;
_this.index = i;
_this.active = true;
_this.value = undefined;
return _this;
}
IndexSink.prototype.event = function (t, x) {
if (!this.active) {
return;
}
this.value = x;
this.sink.event(t, this);
};
IndexSink.prototype.end = function (t) {
if (!this.active) {
return;
}
this.active = false;
this.sink.event(t, this);
};
return IndexSink;
}(Pipe));
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */
/**
* TODO: find a better way (without `any`)
*/
function invoke(f, args) {
/* eslint complexity: [2,7] */
switch (args.length) {
case 0: return f();
case 1: return f(args[0]);
case 2: return f(args[0], args[1]);
case 3: return f(args[0], args[1], args[2]);
case 4: return f(args[0], args[1], args[2], args[3]);
case 5: return f(args[0], args[1], args[2], args[3], args[4]);
default:
return f.apply(undefined, args);
}
}
var CombineSink = /** @class */ (function (_super) {
__extends(CombineSink, _super);
function CombineSink(disposables, length, sink, f) {
var _this = _super.call(this, sink) || this;
_this.disposables = disposables;
_this.f = f;
_this.awaiting = length;
_this.values = new Array(length);
_this.hasValue = new Array(length).fill(false);
_this.activeCount = length;
return _this;
}
CombineSink.prototype.event = function (t, indexedValue) {
if (!indexedValue.active) {
this.dispose(t, indexedValue.index);
return;
}
var i = indexedValue.index;
var awaiting = this.updateReady(i);
this.values[i] = indexedValue.value;
if (awaiting === 0) {
this.sink.event(t, invoke(this.f, this.values));
}
};
CombineSink.prototype.updateReady = function (index) {
if (this.awaiting > 0) {
if (!this.hasValue[index]) {
this.hasValue[index] = true;
this.awaiting -= 1;
}
}
return this.awaiting;
};
CombineSink.prototype.dispose = function (t, index) {
tryDispose(t, this.disposables[index], this.sink);
if (--this.activeCount === 0) {
this.sink.end(t);
}
};
return CombineSink;
}(Pipe));
/** @license MIT License (c) copyright 2010 original author or authors */
var mergeConcurrently = function (concurrency, stream) {
return mergeMapConcurrently(id, concurrency, stream);
};
var mergeMapConcurrently = function (f, concurrency, stream) {
return isCanonicalEmpty(stream) ? empty()
: new MergeConcurrently(f, concurrency, stream);
};
var MergeConcurrently = /** @class */ (function () {
function MergeConcurrently(f, concurrency, source) {
this.f = f;
this.concurrency = concurrency;
this.source = source;
}
MergeConcurrently.prototype.run = function (sink, scheduler) {
return new Outer(this.f, this.concurrency, this.source, sink, scheduler);
};
return MergeConcurrently;
}());
var isNonEmpty = function (array) { return array.length > 0; };
var Outer = /** @class */ (function () {
function Outer(f, concurrency, source, sink, scheduler) {
this.f = f;
this.concurrency = concurrency;
this.sink = sink;
this.scheduler = scheduler;
this.pending = [];
this.current = [];
this.disposable = disposeOnce(source.run(this, scheduler));
this.active = true;
}
Outer.prototype.event = function (t, x) {
this.addInner(t, x);
};
Outer.prototype.addInner = function (t, x) {
if (this.current.length < this.concurrency) {
this.startInner(t, x);
}
else {
this.pending.push(x);
}
};
Outer.prototype.startInner = function (t, x) {
try {
this.initInner(t, x);
}
catch (e) {
this.error(t, e);
}
};
Outer.prototype.initInner = function (t, x) {
var innerSink = new Inner(t, this, this.sink);
innerSink.disposable = mapAndRun(this.f, t, x, innerSink, this.scheduler);
this.current.push(innerSink);
};
Outer.prototype.end = function (t) {
this.active = false;
tryDispose(t, this.disposable, this.sink);
this.checkEnd(t);
};
Outer.prototype.error = function (t, e) {
this.active = false;
this.sink.error(t, e);
};
Outer.prototype.dispose = function () {
this.active = false;
this.pending.length = 0;
this.disposable.dispose();
disposeAll(this.current).dispose();
};
Outer.prototype.endInner = function (t, inner) {
var i = this.current.indexOf(inner);
if (i >= 0) {
this.current.splice(i, 1);
}
tryDispose(t, inner, this);
var pending = this.pending;
if (isNonEmpty(pending)) {
this.startInner(t, pending.shift());
}
else {
this.checkEnd(t);
}
};
Outer.prototype.checkEnd = function (t) {
if (!this.active && this.current.length === 0) {
this.sink.end(t);
}
};
return Outer;
}());
var mapAndRun = function (f, t, x, sink, scheduler) {
return f(x).run(sink, schedulerRelativeTo(t, scheduler));
};
var Inner = /** @class */ (function () {
function Inner(time, outer, sink) {
this.time = time;
this.outer = outer;
this.sink = sink;
this.disposable = disposeNone();
}
Inner.prototype.event = function (t, x) {
this.sink.event(t + this.time, x);
};
Inner.prototype.end = function (t) {
this.outer.endInner(t + this.time, this);
};
Inner.prototype.error = function (t, e) {
this.outer.error(t + this.time, e);
};
Inner.prototype.dispose = function () {
return this.disposable.dispose();
};
return Inner;
}());
/**
* Monadic join. Flatten a Stream<Stream<X>> to Stream<X> by merging inner
* streams to the outer. Event arrival times are preserved.
* @param stream stream of streams
* @returns new stream containing all events of all inner streams
*/
var join = function (stream) { return mergeConcurrently(Infinity, stream); };
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* @returns stream containing events from two streams in time order.
* If two events are simultaneous they will be merged in arbitrary order.
*/
function merge$1(stream1, stream2) {
return mergeArray([stream1, stream2]);
}
/**
* @param streams array of stream to merge
* @returns stream containing events from all input observables
* in time order. If two events are simultaneous they will be merged in
* arbitrary order.
*/
var mergeArray = function (streams) {
return mergeStreams(withoutCanonicalEmpty(streams));
};
/**
* This implements fusion/flattening for merge. It will
* fuse adjacent merge operations. For example:
* - a.merge(b).merge(c) effectively becomes merge(a, b, c)
* - merge(a, merge(b, c)) effectively becomes merge(a, b, c)
* It does this by concatenating the sources arrays of
* any nested Merge sources, in effect "flattening" nested
* merge operations into a single merge.
* TODO: use {@link MergeArray}
*/
var mergeStreams = function (streams) {
return streams.length === 0 ? empty()
: streams.length === 1 ? streams[0]
: new Merge(reduce(appendSources, [], streams));
};
var withoutCanonicalEmpty = function (streams) {
return streams.filter(isNotCanonicalEmpty);
};
var isNotCanonicalEmpty = function (stream) {
return !isCanonicalEmpty(stream);
};
var appendSources = function (sources, stream) {
return sources.concat(stream instanceof Merge ? stream.sources : stream);
};
var Merge = /** @class */ (function () {
function Merge(sources) {
this.sources = sources;
}
Merge.prototype.run = function (sink, scheduler) {
var l = this.sources.length;
var disposables = new Array(l);
var sinks = new Array(l);
var mergeSink = new MergeSink(disposables, sinks, sink);
for (var indexSink = void 0, i = 0; i < l; ++i) {
indexSink = sinks[i] = new IndexSink(i, mergeSink);
disposables[i] = this.sources[i].run(indexSink, scheduler);
}
return disposeAll(disposables);
};
return Merge;
}());
var MergeSink = /** @class */ (function (_super) {
__extends(MergeSink, _super);
function MergeSink(disposables, sinks, sink) {
var _this = _super.call(this, sink) || this;
_this.disposables = disposables;
_this.activeCount = sinks.length;
return _this;
}
MergeSink.prototype.event = function (t, indexValue) {
if (!indexValue.active) {
this.dispose(t, indexValue.index);
return;
}
this.sink.event(t, indexValue.value);
};
MergeSink.prototype.dispose = function (t, index) {
tryDispose(t, this.disposables[index], this.sink);
if (--this.activeCount === 0) {
this.sink.end(t);
}
};
return MergeSink;
}(Pipe));
var snapshot = function (f, values, sampler) {
return isCanonicalEmpty(sampler) || isCanonicalEmpty(values)
? empty()
: new Snapshot(f, values, sampler);
};
var Snapshot = /** @class */ (function () {
function Snapshot(f, values, sampler) {
this.f = f;
this.values = values;
this.sampler = sampler;
}
Snapshot.prototype.run = function (sink, scheduler) {
var sampleSink = new SnapshotSink(this.f, sink);
var valuesDisposable = this.values.run(sampleSink.latest, scheduler);
var samplerDisposable = this.sampler.run(sampleSink, scheduler);
return disposeBoth(samplerDisposable, valuesDisposable);
};
return Snapshot;
}());
var SnapshotSink = /** @class */ (function (_super) {
__extends(SnapshotSink, _super);
function SnapshotSink(f, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
_this.latest = new LatestValueSink(_this);
return _this;
}
SnapshotSink.prototype.event = function (t, x) {
if (this.latest.hasValue) {
var f = this.f;
// TODO: value should be boxed to avoid ! bang
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.sink.event(t, f(this.latest.value, x));
}
};
return SnapshotSink;
}(Pipe));
var LatestValueSink = /** @class */ (function (_super) {
__extends(LatestValueSink, _super);
function LatestValueSink(sink) {
var _this = _super.call(this, sink) || this;
_this.hasValue = false;
return _this;
}
LatestValueSink.prototype.event = function (_t, x) {
this.value = x;
this.hasValue = true;
};
LatestValueSink.prototype.end = function () { };
return LatestValueSink;
}(Pipe));
var SliceSink = /** @class */ (function (_super) {
__extends(SliceSink, _super);
function SliceSink(skip, take, sink, disposable) {
var _this = _super.call(this, sink) || this;
_this.skip = skip;
_this.take = take;
_this.disposable = disposable;
return _this;
}
SliceSink.prototype.event = function (t, x) {
/* eslint complexity: [1, 4] */
if (this.skip > 0) {
this.skip -= 1;
return;
}
if (this.take === 0) {
return;
}
this.take -= 1;
this.sink.event(t, x);
if (this.take === 0) {
this.disposable.dispose();
this.sink.end(t);
}
};
return SliceSink;
}(Pipe));
var TakeWhileSink = /** @class */ (function (_super) {
__extends(TakeWhileSink, _super);
function TakeWhileSink(p, sink, disposable) {
var _this = _super.call(this, sink) || this;
_this.p = p;
_this.active = true;
_this.disposable = disposable;
return _this;
}
TakeWhileSink.prototype.event = function (t, x) {
if (!this.active) {
return;
}
var p = this.p;
this.active = p(x);
if (this.active) {
this.sink.event(t, x);
}
else {
this.disposable.dispose();
this.sink.end(t);
}
};
return TakeWhileSink;
}(Pipe));
var SkipWhileSink = /** @class */ (function (_super) {
__extends(SkipWhileSink, _super);
function SkipWhileSink(p, sink) {
var _this = _super.call(this, sink) || this;
_this.p = p;
_this.skipping = true;
return _this;
}
SkipWhileSink.prototype.event = function (t, x) {
if (this.skipping) {
var p = this.p;
this.skipping = p(x);
if (this.skipping) {
return;
}
}
this.sink.event(t, x);
};
return SkipWhileSink;
}(Pipe));
var skipAfter = function (p, stream) {
return isCanonicalEmpty(stream) ? empty()
: new SkipAfter(p, stream);
};
var SkipAfter = /** @class */ (function () {
function SkipAfter(p, source) {
this.p = p;
this.source = source;
}
SkipAfter.prototype.run = function (sink, scheduler) {
return this.source.run(new SkipAfterSink(this.p, sink), scheduler);
};
return SkipAfter;
}());
var SkipAfterSink = /** @class */ (function (_super) {
__extends(SkipAfterSink, _super);
function SkipAfterSink(p, sink) {
var _this = _super.call(this, sink) || this;
_this.p = p;
_this.skipping = false;
return _this;
}
SkipAfterSink.prototype.event = function (t, x) {
if (this.skipping) {
return;
}
var p = this.p;
this.skipping = p(x);
this.sink.event(t, x);
if (this.skipping) {
this.sink.end(t);
}
};
return SkipAfterSink;
}(Pipe));
var ZipItemsSink = /** @class */ (function (_super) {
__extends(ZipItemsSink, _super);
function ZipItemsSink(f, items, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
_this.items = items;
_this.index = 0;
return _this;
}
ZipItemsSink.prototype.event = function (t, b) {
var f = this.f;
this.sink.event(t, f(this.items[this.index], b));
this.index += 1;
};
return ZipItemsSink;
}(Pipe));
var ZipSink = /** @class */ (function (_super) {
__extends(ZipSink, _super);
function ZipSink(f, buffers, sinks, sink) {
var _this = _super.call(this, sink) || this;
_this.f = f;
_this.sinks = sinks;
_this.buffers = buffers;
return _this;
}
ZipSink.prototype.event = function (t, indexedValue) {
/* eslint complexity: [1, 5] */
if (!indexedValue.active) {
this.dispose(t, indexedValue.index);
return;
}
var buffers = this.buffers;
var buffer = buffers[indexedValue.index];
buffer.push(indexedValue.value);
if (buffer.length() === 1) {
if (!ready(buffers)) {
return;
}
emitZipped(this.f, t, buffers, this.sink);
if (ended(this.buffers, this.sinks)) {
this.sink.end(t);
}
}
};
ZipSink.prototype.dispose = function (t, index) {
var buffer = this.buffers[index];
if (buffer.isEmpty()) {
this.sink.end(t);
}
};
return ZipSink;
}(Pipe));
var emitZipped = function (f, t, buffers, sink) {
return sink.event(t, invoke(f, map(head, buffers)));
};
var head = function (buffer) { return buffer.shift(); };
function ended(buffers, sinks) {
for (var i = 0, l = buffers.length; i < l; ++i) {
if (buffers[i].isEmpty() && !sinks[i].active) {
return true;
}
}
return false;
}
function ready(buffers) {
for (var i = 0, l = buffers.length; i < l; ++i) {
if (buffers[i].isEmpty()) {
return false;
}
}
return true;
}
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/**
* Given a stream of streams, return a new stream that adopts the behavior
* of the most recent inner stream.
* @param stream of streams on which to switch
* @returns switching stream
*/
var switchLatest = function (stream) {
return isCanonicalEmpty(stream)
? empty()
: new Switch(stream);
};
var Switch = /** @class */ (function () {
function Switch(source) {
this.source = source;
}
Switch.prototype.run = function (sink, scheduler) {
var switchSink = new SwitchSink(sink, scheduler);
return disposeBoth(switchSink, this.source.run(switchSink, scheduler));
};
return Switch;
}());
var SwitchSink = /** @class */ (function () {
function SwitchSink(sink, scheduler) {
this.sink = sink;
this.scheduler = scheduler;
this.current = null;
this.ended = false;
}
SwitchSink.prototype.event = function (t, stream) {
this.disposeCurrent(t);
this.current = new Segment(stream, t, Infinity, this, this.sink, this.scheduler);
};
SwitchSink.prototype.end = function (t) {
this.ended = true;
this.checkEnd(t);
};
SwitchSink.prototype.error = function (t, e) {
this.ended = true;
this.sink.error(t, e);
};
SwitchSink.prototype.dispose = function () {
return this.disposeCurrent(currentTime(this.scheduler));
};
SwitchSink.prototype.disposeCurrent = function (t) {
if (this.current !== null) {
return this.current.dispose(t);
}
};
SwitchSink.prototype.disposeInner = function (t, inner) {
inner.dispose(t);
if (inner === this.current) {
this.current = null;
}
};
SwitchSink.prototype.checkEnd = function (t) {
if (this.ended && this.current === null) {
this.sink.end(t);
}
};
SwitchSink.prototype.endInner = function (t, inner) {
this.disposeInner(t, inner);
this.checkEnd(t);
};
SwitchSink.prototype.errorInner = function (t, e, inner) {
this.disposeInner(t, inner);
this.sink.error(t, e);
};
return SwitchSink;
}());
var Segment = /** @class */ (function () {
function Segment(source, min, max, outer, sink, scheduler) {
this.min = min;
this.max = max;
this.outer = outer;
this.sink = sink;
this.disposable = source.run(this, schedulerRelativeTo(min, scheduler));
}
Segment.prototype.event = function (t, x) {
var time = Math.max(0, t + this.min);
if (time < this.max) {
this.sink.event(time, x);
}
};
Segment.prototype.end = function (t) {
this.outer.endInner(t + this.min, this);
};
Segment.prototype.error = function (t, e) {
this.outer.errorInner(t + this.min, e, this);
};
Segment.prototype.dispose = function (t) {
tryDispose(t, this.disposable, this.sink);
};
return Segment;
}());
/** @license MIT License (c) copyright 2010-2016 original author or authors */
function filter(p, stream) {
return Filter.create(p, stream);
}
/**
* Skip repeated events, using === to detect duplicates
* @param stream stream from which to omit repeated events
* @returns stream without repeated events
*/
var skipRepeats = function (stream) {
return skipRepeatsWith(same, stream);
};
/**
* Skip repeated events using the provided equals function to detect duplicates
* @param equals optional function to compare items
* @param stream stream from which to omit repeated events
* @returns stream without repeated events
*/
var skipRepeatsWith = function (equals, stream) {
return isCanonicalEmpty(stream) ? empty()
: new SkipRepeats(equals, stream);
};
var SkipRepeats = /** @class */ (function () {
function SkipRepeats(equals, source) {
this.equals = equals;
this.source = source;
}
SkipRepeats.prototype.run = function (sink, scheduler) {
return this.source.run(new SkipRepeatsSink(this.equals, sink), scheduler);
};
return SkipRepeats;
}());
var SkipRepeatsSink = /** @class */ (function (_super) {
__extends(SkipRepeatsSink, _super);
function SkipRepeatsSink(equals, sink) {
var _this = _super.call(this, sink) || this;
_this.equals = equals;
_this.value = undefined;
_this.init = true;
return _this;
}
SkipRepeatsSink.prototype.event = function (t, x) {
if (this.init) {
this.init = false;
this.value = x;
this.sink.event(t, x);