-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathobservable.ts
More file actions
108 lines (89 loc) · 2.76 KB
/
Copy pathobservable.ts
File metadata and controls
108 lines (89 loc) · 2.76 KB
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
import { monitorError } from './monitor'
export interface Subscription {
unsubscribe: () => void
}
type Observer<T> = (data: T) => void
// eslint-disable-next-line no-restricted-syntax
export class Observable<T> {
protected observers: Array<Observer<T>> = []
private onLastUnsubscribe?: () => void
constructor(private onFirstSubscribe?: (observable: Observable<T>) => (() => void) | void) {}
subscribe(observer: Observer<T>): Subscription {
this.addObserver(observer)
return {
unsubscribe: () => this.removeObserver(observer),
}
}
notify(data: T) {
this.observers.forEach((observer) => observer(data))
}
protected addObserver(observer: Observer<T>) {
this.observers.push(observer)
if (this.observers.length === 1 && this.onFirstSubscribe) {
this.onLastUnsubscribe = this.onFirstSubscribe(this) || undefined
}
}
protected removeObserver(observer: Observer<T>) {
this.observers = this.observers.filter((other) => observer !== other)
if (!this.observers.length && this.onLastUnsubscribe) {
this.onLastUnsubscribe()
}
}
}
export function mergeObservables<T>(...observables: Array<Observable<T>>) {
return new Observable<T>((globalObservable) => {
const subscriptions: Subscription[] = observables.map((observable) =>
observable.subscribe((data) => globalObservable.notify(data))
)
return () => subscriptions.forEach((subscription) => subscription.unsubscribe())
})
}
// eslint-disable-next-line no-restricted-syntax
export class BufferedObservable<T> extends Observable<T> {
private buffer: T[] = []
constructor(private maxBufferSize: number) {
// no onFirstSubscribe as it makes less sense with buffered data
super()
}
notify(data: T) {
this.buffer.push(data)
if (this.buffer.length > this.maxBufferSize) {
this.buffer.shift()
}
super.notify(data)
}
subscribe(observer: Observer<T>): Subscription {
let closed = false
const subscription = {
unsubscribe: () => {
closed = true
this.removeObserver(observer)
},
}
enqueueMicroTask(() => {
for (const data of this.buffer) {
if (closed) {
return
}
observer(data)
}
if (!closed) {
this.addObserver(observer)
}
})
return subscription
}
/**
* Drop buffered data and don't buffer future data. This is to avoid leaking memory when it's not
* needed anymore. This is not be required in most cases, but still useful to clarify our intent
* and lowering our memory impact.
*/
unbuffer() {
enqueueMicroTask(() => {
this.maxBufferSize = this.buffer.length = 0
})
}
}
function enqueueMicroTask(callback: () => void) {
Promise.resolve().then(callback).catch(monitorError)
}