-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
124 lines (100 loc) · 2.92 KB
/
index.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
import makeEmitter, { Emitter } from 'better-emitter'
export interface BasicObservable<T> extends Emitter {
get(): T,
set(newValue: T): void,
locked(): boolean
}
export interface WargObservable<T> extends BasicObservable<T> {
subscribe(cb: (value: T) => void): () => void,
map<R>(fn: (value: T) => R): WargObservable<R>
}
type DependencyObject<T extends { [key: string]: any }> = {
[key in keyof T]: BasicObservable<T[key]>
}
export const value = <T>(value: T): WargObservable<T> => {
const emitter = makeEmitter({
get() {
return value
},
set(newValue: T) {
emitter.emit(`dirty`)
value = newValue
emitter.emit(`value`, value)
},
locked() {
return false
},
})
return addHelpers(emitter)
}
const combine = <D extends { [key: string]: any }>(dependencies: DependencyObject<D>): BasicObservable<D> => {
let value: D = resolve(dependencies)
let locked = false
const setDirty = () => {
locked = true
emitter.emit(`dirty`)
}
onEventFromAnyDependency(dependencies, `dirty`, setDirty)
onEventFromAnyDependency(dependencies, `value`, () => {
if (!locked) {
setDirty()
}
if (allDependenciesAreUnlocked(dependencies)) {
value = resolve(dependencies)
locked = false
emitter.emit(`value`, value)
}
})
const emitter = makeEmitter({
get() {
if (locked) {
throw new Error(`Unreadable!`)
}
return value
},
set() {
throw new Error(`Combinations may not be set`)
},
locked() {
return locked
},
})
return emitter
}
export const computed = <D extends { [key: string]: any }, T>(dependencies: DependencyObject<D>, calculation: (D) => T): WargObservable<T> => {
const combined = combine(dependencies)
const observableValue: BasicObservable<T> = value(calculation(combined.get()))
const onValueChange = dependencyValues => observableValue.set(calculation(dependencyValues))
combined.on(`value`, onValueChange)
return Object.assign(addHelpers(observableValue), {
set() {
throw new Error(`Transforms may not be set`)
},
})
}
type Resolved<T> = {
[key in keyof T]: T[key]
}
const resolve = <D extends { [key: string]: any }, Values extends Resolved<D>>(dependencies: DependencyObject<D>): Values =>
Object.entries(dependencies).reduce((previous, [ key, dependency ]) => ({
...previous,
[key]: dependency.get(),
}), {} as Values)
const allDependenciesAreUnlocked = (dependencies: DependencyObject<any>) => Object.values(dependencies).every(
dependency => !dependency.locked(),
)
const onEventFromAnyDependency = (dependencies: DependencyObject<any>, event: string, cb) => {
Object.values(dependencies).forEach(dependency => {
dependency.on(event, cb)
})
}
const addHelpers = <T>(observable: BasicObservable<T>): WargObservable<T> => Object.assign({}, observable, {
subscribe(cb) {
const unsubscribe = observable.on(`value`, cb)
cb(observable.get())
return unsubscribe
},
map(fn) {
return computed({ observable }, ({ observable }) => fn(observable))
},
})