@tempest/core
Version:
The core of the Tempest Stream Library
75 lines • 2.23 kB
JavaScript
import { defaultScheduler } from './scheduler/defaultScheduler';
export function withDefaultScheduler(f, source) {
return withScheduler(f, source, defaultScheduler);
}
export function withScheduler(f, source, scheduler) {
return new Promise((resolve, reject) => {
runSource(f, source, scheduler, resolve, reject);
});
}
export function runSource(f, source, scheduler, end, error) {
const disposable = new SettableDisposable();
const observer = new Drain(f, end, error, disposable);
disposable.setDisposable(source.run(observer, scheduler));
}
class SettableDisposable {
constructor() {
this.disposable = void 0;
this.disposed = false;
this._resolve = void 0;
const self = this;
this._result = new Promise((resolve) => {
self._resolve = resolve;
});
}
dispose() {
if (this.disposed)
return this._result;
this.disposed = true;
if (this.disposable) {
this._result = this.disposable.dispose();
}
return this._result;
}
setDisposable(disposable) {
if (this.disposable !== void 0) {
throw new Error('Disposable can only be set one time');
}
this.disposable = disposable;
if (this.disposed) {
this._resolve(disposable.dispose());
}
}
}
class Drain {
constructor(_event, _end, _error, disposable) {
this._event = _event;
this._end = _end;
this._error = _error;
this.disposable = disposable;
this.active = true;
}
event(time, value) {
if (!this.active)
return;
this._event(value);
}
error(time, err) {
if (!this.active)
return;
this.active = false;
disposeThen(this._error, this._error, this.disposable, err);
}
end(time, value) {
if (!this.active)
return;
this.active = false;
disposeThen(this._error, this._error, this.disposable, value);
}
}
function disposeThen(end, error, disposable, value) {
Promise.resolve(disposable.dispose()).then(() => {
end(value);
}, error);
}
//# sourceMappingURL=runSource.js.map