/
observable-stream.ts
148 lines (133 loc) · 4.1 KB
/
observable-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { computed, observable, action, runInAction } from "mobx"
declare var Symbol: any
function observableSymbol() {
return (typeof Symbol === "function" && Symbol.observable) || "@@observable"
}
function self() {
return this
}
export interface IStreamObserver<T> {
next?(value: T): void
error?(error: any): void
complete?(): void
}
export interface ISubscription {
unsubscribe(): void
}
export interface IObservableStream<T> {
subscribe(observer?: IStreamObserver<T> | null): ISubscription
subscribe(observer?: ((value: T) => void) | null): ISubscription
// [Symbol.observable](): IObservable;
}
/**
* Converts an expression to an observable stream (a.k.a. TC 39 Observable / RxJS observable).
* The provided expression is tracked by mobx as long as there are subscribers, automatically
* emitting when new values become available. The expressions respect (trans)actions.
*
* @example
*
* const user = observable({
* firstName: "C.S",
* lastName: "Lewis"
* })
*
* Rx.Observable
* .from(mobxUtils.toStream(() => user.firstname + user.lastName))
* .scan(nameChanges => nameChanges + 1, 0)
* .subscribe(nameChanges => console.log("Changed name ", nameChanges, "times"))
*
* @export
* @template T
* @param {() => T} expression
* @param {boolean} fireImmediately (by default false)
* @returns {IObservableStream<T>}
*/
export function toStream<T>(
expression: () => T,
fireImmediately: boolean = false
): IObservableStream<T> {
const computedValue = computed(expression)
return {
subscribe(observer?: IStreamObserver<T> | ((value: T) => void) | null): ISubscription {
if ("function" === typeof observer) {
return {
unsubscribe: computedValue.observe(
({ newValue }: { newValue: T }) => observer(newValue),
fireImmediately
),
}
}
if (observer && "object" === typeof observer && observer.next) {
return {
unsubscribe: computedValue.observe(
({ newValue }: { newValue: T }) => observer.next!(newValue),
fireImmediately
),
}
}
return {
unsubscribe: () => {},
}
},
[observableSymbol()]: self,
}
}
class StreamListener<T> implements IStreamObserver<T> {
@observable.ref current!: T
subscription!: ISubscription
constructor(observable: IObservableStream<T>, initialValue: T) {
runInAction(() => {
this.current = initialValue
this.subscription = observable.subscribe(this)
})
}
dispose() {
if (this.subscription) {
this.subscription.unsubscribe()
}
}
@action.bound
next(value: T) {
this.current = value
}
@action.bound
complete() {
this.dispose()
}
@action.bound
error(value: T) {
this.current = value
this.dispose()
}
}
/**
* Converts a subscribable, observable stream (TC 39 observable / RxJS stream)
* into an object which stores the current value (as `current`). The subscription can be cancelled through the `dispose` method.
* Takes an initial value as second optional argument
*
* @example
* const debouncedClickDelta = MobxUtils.fromStream(Rx.Observable.fromEvent(button, 'click')
* .throttleTime(1000)
* .map(event => event.clientX)
* .scan((count, clientX) => count + clientX, 0)
* )
*
* autorun(() => {
* console.log("distance moved", debouncedClickDelta.current)
* })
*/
export function fromStream<T>(observable: IObservableStream<T>): IStreamListener<T | undefined>
export function fromStream<T, I>(
observable: IObservableStream<T>,
initialValue: I
): IStreamListener<T | I>
export function fromStream<T>(
observable: IObservableStream<T>,
initialValue: T = undefined as any
): IStreamListener<T> {
return new StreamListener(observable, initialValue)
}
export interface IStreamListener<T> {
current: T
dispose(): void
}