Repository navigation
Expand file tree
/
Copy pathwritable.ts
More file actions
64 lines (55 loc) · 2.04 KB
/
Copy pathwritable.ts
File metadata and controls
64 lines (55 loc) · 2.04 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
import * as Interface from '../interface'
import * as Utils from '../utils'
const subscriberQueue: any[] = []
export class Writable<T> implements Interface.Writable<T> {
protected stop: Interface.Unsubscriber | null = null
protected subscribers: Array<Interface.SubscribeInvalidateTuple<T>> = []
protected value?: T
protected start: Interface.StartStopNotifier<T>
constructor(value?: T, start: Interface.StartStopNotifier<T> = Utils.noop) {
this.value = value
this.start = start
}
get() {
return Utils.getStoreValue(this)
}
set(newValue: T) {
if (Utils.safeNotEqual(this.value, newValue)) {
this.value = newValue
if (this.stop) {
const runQueue = !subscriberQueue.length
for (let i = 0; i < this.subscribers.length; i += 1) {
const s = this.subscribers[i]
s[1]()
subscriberQueue.push(s, this.value)
}
if (runQueue) {
for (let i = 0; i < subscriberQueue.length; i += 2)
subscriberQueue[i][0](subscriberQueue[i + 1])
subscriberQueue.length = 0
}
}
}
}
update(callback: Interface.Updater<T>) {
this.set(callback(this.value))
}
subscribe(
run: Interface.Subscriber<T>,
invalidate: Interface.Invalidator<T> = Utils.noop
): Interface.Unsubscriber {
const subscriber: Interface.SubscribeInvalidateTuple<T> = [run, invalidate]
this.subscribers.push(subscriber)
if (this.subscribers.length === 1)
this.stop = this.start(this.set) || Utils.noop
if (this.value) run(this.value)
return () => {
const index = this.subscribers.indexOf(subscriber)
if (index !== -1) this.subscribers.splice(index, 1)
if (this.subscribers.length === 0) {
if (this.stop) this.stop()
this.stop = null
}
}
}
}