Example #1
0
	constructor(underlyingSink: Sink<T> = {}, strategy: Strategy<T> = {}) {
		this._underlyingSink = underlyingSink;

		this._closedPromise = new Promise<void>((resolve, reject) => {
			this._resolveClosedPromise = resolve;
			this._rejectClosedPromise = reject;
		});

		this._advancing = false;
		this._readyPromise = Promise.resolve();
		this._queue = new SizeQueue<Record<T>>();
		this._state = State.Writable;
		this._started = false;
		this._writing = false;
		this._strategy = util.normalizeStrategy(strategy);
		this._syncStateWithQueue();

		this._startedPromise = Promise.resolve(
			util.invokeOrNoop(this._underlyingSink, 'start', [ this._error.bind(this) ])
		).then(() => {
			this._started = true;
			this._startedPromise = undefined;
		}, (error: Error) => {
			this._error(error);
		});
	}
Example #2
0
	start(): Promise<void> {
		if (this._isClosed) {
			return Promise.reject(new Error('Stream is closed'));
		}

		return Promise.resolve();
	}
Example #3
0
	storeObservable.then = function<V>(onFulfilled?: ((value?: T[]) => (V | Promise<V> | null | undefined)) | null | undefined, onRejected?: (reason?: Error) => void): Promise<V> {
		// Wrap in a shim promise because the interface that leaks through observable.toPromise is missing some
		// properties on the shim(e.g. promise)
		return Promise.resolve(storeObservable.toPromise()).then(function(result) {
			return transform(result);
		}).then<V>(onFulfilled, onRejected);
	};
Example #4
0
	/**
	 * Signals that the producer can no longer write to the stream and it should be immediately moved to an "errored"
	 * state. Any un-written data that is queued will be discarded.
	 */
	abort(reason: any): Promise<void> {
		// 4.2.4.4-1
		if (!isWritableStream(this)) {
			return Promise.reject(
				new Error('WritableStream method called in context of object that is not a WritableStream instance')
			);
		}

		if (this.state === State.Closed) {
			// 4.2.4.4-2
			return Promise.resolve();
		}

		if (this.state === State.Errored) {
			// 4.2.4.4-3
			return Promise.reject(this._storedError);
		}

		const error: Error = reason instanceof Error ? reason : new Error(reason);

		this._error(error);

		return util.promiseInvokeOrFallbackOrNoop(this._underlyingSink, 'abort', [ reason ], 'close')
			.then(function () {
				return;
			});
	}
		let registryHandle = widgets.get(app).register(id, () => {
			const promise = Promise.resolve().then(() => {
				// Always call the factory in a future turn. This harmonizes behavior regardless of whether the
				// factory is registered through this method or loaded from a definition.

				const { registryProvider, defaultStore } = app;
				interface Options {
					id: string;
					registryProvider: RegistryProvider;
					stateFrom?: StoreLike;
				}
				const options: Options = { id, registryProvider };
				if (defaultStore) {
					options.stateFrom = defaultStore;
				}
				return factory(options);
			}).then((widget) => {
				if (!destroyed) {
					instanceHandle = app._instanceRegistry.addWidget(widget, id);
				}

				return widget;
			});
			// Replace the registered factory to ensure next time this widget is needed, the same widget is returned.
			registryHandle.destroy();
			registryHandle = widgets.get(app).register(id, () => promise);
			return promise;
		});
Example #6
0
function catchRejection(router: Router<Context>, context: Context, path: string, thenable: void | Thenable<any>) {
	if (thenable) {
		Promise.resolve(thenable).catch((error) => {
			reportError(router, context, path, error);
		});
	}
}
Example #7
0
					return this.reader.read().then((result: ReadResult<T>) => {
						if (result.done || this.reader.currentPosition === position) {
							return Promise.resolve(this.reader.currentPosition);
						}
						else {
							return discardNext();
						}
					});
Example #8
0
			return reader.read().then(function (result: ReadResult<number>) {
				if (result.done) {
					return Promise.resolve();
				}
				else {
					results.push(result.value);
					return readNext();
				}
			});
				.then((action) => {
					if (!destroyed) {
						instanceHandle = app._instanceRegistry.addAction(action, id);
					}

					// Configure the action, allow for a promise to be returned.
					return Promise.resolve(action.configure(app._registry)).then(() => {
						return action;
					});
				});
Example #10
0
	seek(controller: ReadableStreamController<T>, position: number): Promise<number> {
		if (position >= this.data.length || position < 0) {
			let error = new Error('Invalid seek position: ' + position);
			controller.error(error);

			return Promise.reject(error);
		}

		this.currentPosition = position;

		return Promise.resolve(this.currentPosition);
	}