export type Source = (subject: IFutureSubject) => void; export type CancelCallback = () => void; export interface IFutureSubscriber { onComplete: (value: T) => void; onError: (error: Error) => void; onSubscribe: (cancel: CancelCallback) => void; } export interface IFutureSubject { onComplete: (value: T) => void; onError: (error: Error) => void; onSubscribe: (cancel: CancelCallback | null | undefined) => void; } /** * Represents a lazy computation that will either produce a value of type T * or fail with an error. Calling `subscribe()` starts the * computation and returns a subscription object, which has an `unsubscribe()` * method that can be called to prevent completion/error callbacks from being * invoked and, where supported, to also cancel the computation. * Implementations may optionally implement cancellation; if they do not * `cancel()` is a no-op. * * Note: Unlike Promise, callbacks (onComplete/onError) may be invoked * synchronously. * * Example: * * ``` * const value = new Single(subscriber => { * const id = setTimeout( * () => subscriber.onComplete('Hello!'), * 250 * ); * // Optional: Call `onSubscribe` with a cancellation callback * subscriber.onSubscribe(() => clearTimeout(id)); * }); * * // Start the computation. onComplete will be called after the timeout * // with 'hello' unless `cancel()` is called first. * value.subscribe({ * onComplete: value => console.log(value), * onError: error => console.error(error), * onSubscribe: cancel => ... * }); * ``` */ export default class Single { static of(value: U): Single; static error(error: Error): Single<{}>; constructor(source: Source); subscribe(partialSubscriber?: Partial>): void; flatMap(fn: (data: T) => Single): Single; /** * Return a new Single that resolves to the value of this Single applied to * the given mapping function. */ map(fn: (data: T) => R): Single; then(successFn?: (data: T) => void, errorFn?: (error: Error) => void): void; }