UNPKG

2.11 kBTypeScriptView Raw
1export interface IStreamObserver<T> {
2 next?(value: T): void;
3 error?(error: any): void;
4 complete?(): void;
5}
6export interface ISubscription {
7 unsubscribe(): void;
8}
9export interface IObservableStream<T> {
10 subscribe(observer?: IStreamObserver<T> | null): ISubscription;
11 subscribe(observer?: ((value: T) => void) | null): ISubscription;
12}
13/**
14 * Converts an expression to an observable stream (a.k.a. TC 39 Observable / RxJS observable).
15 * The provided expression is tracked by mobx as long as there are subscribers, automatically
16 * emitting when new values become available. The expressions respect (trans)actions.
17 *
18 * @example
19 *
20 * const user = observable({
21 * firstName: "C.S",
22 * lastName: "Lewis"
23 * })
24 *
25 * Rx.Observable
26 * .from(mobxUtils.toStream(() => user.firstname + user.lastName))
27 * .scan(nameChanges => nameChanges + 1, 0)
28 * .subscribe(nameChanges => console.log("Changed name ", nameChanges, "times"))
29 *
30 * @export
31 * @template T
32 * @param {() => T} expression
33 * @param {boolean} fireImmediately (by default false)
34 * @returns {IObservableStream<T>}
35 */
36export declare function toStream<T>(expression: () => T, fireImmediately?: boolean): IObservableStream<T>;
37/**
38 * Converts a subscribable, observable stream (TC 39 observable / RxJS stream)
39 * into an object which stores the current value (as `current`). The subscription can be cancelled through the `dispose` method.
40 * Takes an initial value as second optional argument
41 *
42 * @example
43 * const debouncedClickDelta = MobxUtils.fromStream(Rx.Observable.fromEvent(button, 'click')
44 * .throttleTime(1000)
45 * .map(event => event.clientX)
46 * .scan((count, clientX) => count + clientX, 0)
47 * )
48 *
49 * autorun(() => {
50 * console.log("distance moved", debouncedClickDelta.current)
51 * })
52 */
53export declare function fromStream<T>(observable: IObservableStream<T>): IStreamListener<T | undefined>;
54export declare function fromStream<T, I>(observable: IObservableStream<T>, initialValue: I): IStreamListener<T | I>;
55export interface IStreamListener<T> {
56 current: T;
57 dispose(): void;
58}