-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathabstractions.ts
300 lines (268 loc) · 7.56 KB
/
abstractions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { Dispatcher } from "./dispatcher"
import { HKT } from "./hkt"
import { Pipeable } from "./pipeable"
import { toString } from "./tostring"
import { GenericTransformOpScoped } from "./transform"
export type TypeBitfield = number
export const T_SCOPED = 0x0007 // scoped observables always implement seed and source interfaces as well
export const T_SEED = 0x0002
export const T_SOURCE = 0x0004
export const T_PROPERTY = 0x0010
export const T_STREAM = 0x0020
export const T_ATOM = 0x0050 // atoms are always properties
export const T_EVENT = 0x0100
export const T_SCOPE = 0x0200
export const T_VALUE = 0x1100
export const T_END = 0x2100
export function matchFlags(o: any, flags: TypeBitfield) {
if (!o) return false
return (o._L & flags) === flags
}
export function isProperty<V>(e: any): e is Property<V> {
return matchFlags(e, T_SCOPED | T_PROPERTY)
}
export function isPropertySeed<V>(e: any): e is PropertySeed<V> {
return matchFlags(e, T_SEED | T_PROPERTY)
}
export function isPropertySource<V>(e: any): e is PropertySource<V> {
return matchFlags(e, T_SOURCE | T_PROPERTY)
}
export function isEventStream<V>(e: any): e is EventStream<V> {
return matchFlags(e, T_SCOPED | T_STREAM)
}
export function isEventStreamSeed<V>(e: any): e is EventStreamSeed<V> {
return matchFlags(e, T_SEED | T_STREAM)
}
export function isEventStreamSource<V>(e: any): e is EventStreamSeed<V> {
return matchFlags(e, T_SOURCE | T_STREAM)
}
export function isAtom<V>(e: any): e is Atom<V> {
return matchFlags(e, T_SCOPED | T_ATOM)
}
export function isAtomSeed<V>(e: any): e is AtomSeed<V> {
return matchFlags(e, T_SEED | T_ATOM)
}
export function isAtomSource<V>(e: any): e is AtomSeed<V> {
return matchFlags(e, T_SOURCE | T_ATOM)
}
export function isObservableSeed<
V,
C extends Observable<any>,
O extends ScopedObservable<any>
>(e: any): e is ObservableSeed<V, C, O> {
return e._L !== undefined
}
export type Callback = () => void
export type Observer<V> = (value: V) => void
export type Subscribe<V> = (
onValue: Observer<V>,
onEnd?: Observer<void>
) => Unsub
export type Unsub = Callback
export class Description {
desc: Desc
constructor(desc: Desc) {
this.desc = desc
}
toString(): string {
if (typeof this.desc === "string") {
return this.desc
} else if (this.desc instanceof Description) {
return this.desc.toString()
} else if (this.desc.length == 2) {
return `${toString(this.desc[0])}(${this.desc[1]
.map(toString)
.join(",")})`
} else if (this.desc.length === 3) {
return `${toString(this.desc[0])}.${this.desc[1]}(${this.desc[2]
.map(toString)
.join(",")})`
} else {
throw Error("Unexpected desc: " + toString(this.desc))
}
}
}
export type Desc = Description | DescWithContext | MethodDesc | string
export type MethodDesc = [string, any[]]
export type DescWithContext = [any, string, any[]]
export function composeDesc(
context: any,
methodCall: MethodDesc
): DescWithContext {
const [method, args] = methodCall
return [context, method, args]
}
export abstract class Event<V> {
_L: TypeBitfield = T_EVENT
}
export class Value<V> extends Event<V> {
_L: TypeBitfield = T_VALUE
value: V
constructor(value: V) {
super()
this.value = value
}
}
export class End extends Event<any> {
_L: TypeBitfield = T_END
}
export type EventLike<V> = Event<V>[] | Event<V> | V
export function toEvent<V>(value: Event<V> | V): Event<V> {
if (isEvent<V>(value)) {
return value
}
return valueEvent(value)
}
export function isEvent<V>(value: any): value is Event<V> {
return matchFlags(value, T_EVENT)
}
export function toEvents<V>(value: EventLike<V>): Event<V>[] {
if (value instanceof Array) {
return value.map(toEvent)
}
return [toEvent(value)]
}
export function valueEvent<V>(value: V): Value<V> {
return new Value(value)
}
export function isValue<V>(event: Event<V>): event is Value<V> {
return matchFlags(event, T_VALUE)
}
export function isEnd<V>(event: Event<V>): event is End {
return matchFlags(event, T_END)
}
export const endEvent: End = new End()
export function valueObserver<V>(observer: Observer<V>): Observer<Event<V>> {
return (event) => {
if (isValue(event)) observer(event.value)
}
}
export interface ObservableIdentifiers {
_L: TypeBitfield // Discriminator bitfield for detecting implemented interfaces runtime. Used by the is* methods above.
desc: Description
toString(): string
}
export interface ForEach<V> {
forEach(observer: Observer<V>): Unsub
log(message?: string): Unsub
}
export type Observable<V> = ObservableIdentifiers &
ForEach<V> & {
subscribe(onValue: Observer<V>, onEnd?: Observer<void>): Unsub
}
export interface ObservableSeed<
V,
C extends Observable<unknown>,
O extends ScopedObservable<unknown>
> extends Pipeable,
ObservableIdentifiers,
ForEach<V> {
observableType(): string
consume(): C
applyScope(scope: Scope): O
}
export type ScopedObservable<V> = Observable<V> & {
getScope(): Scope
}
export interface PropertyMethods<V> {
get(): V
onChange(onValue: Observer<V>, onEnd?: Observer<void> | undefined): Unsub
}
export type PropertySource<V> = Observable<V> &
PropertySeed<V> &
PropertyMethods<V> &
HKT<PropertySource<V>> & {
observableType(): "PropertySource" | "Property" | "AtomSource" | "Atom"
}
export type Property<V> = ScopedObservable<V> &
PropertySource<V> &
PropertyMethods<V> &
HKT<Property<V>> & {
observableType(): "Property" | "Atom"
}
export type PropertySeed<V> = ObservableSeed<
V,
PropertySource<V>,
Property<V>
> &
HKT<PropertySeed<V>> & {
observableType():
| "PropertySeed"
| "PropertySource"
| "Property"
| "AtomSeed"
| "AtomSource"
| "Atom"
}
export type EventStreamSource<V> = Observable<V> &
EventStreamSeed<V> &
HKT<EventStreamSource<V>> & {
observableType(): "EventStreamSource" | "EventStream" | "Bus"
}
export type EventStream<V> = ScopedObservable<V> &
EventStreamSource<V> &
HKT<EventStream<V>> & {
observableType(): "EventStream" | "Bus"
}
export type EventStreamSeed<V> = ObservableSeed<
V,
EventStreamSource<V>,
EventStream<V>
> &
HKT<EventStreamSeed<V>> & {
observableType():
| "EventStreamSeed"
| "EventStreamSource"
| "EventStream"
| "Bus"
}
export type AtomSource<V> = PropertySource<V> &
PropertySeed<V> &
AtomSeed<V> &
HKT<AtomSource<V>> & {
observableType(): "AtomSource" | "Atom"
set(updatedValue: V): void
}
export type AtomSeed<V> = ObservableSeed<V, AtomSource<V>, Atom<V>> &
HKT<AtomSeed<V>> & {
observableType(): "AtomSeed" | "AtomSource" | "Atom"
}
export type Atom<V> = Property<V> &
AtomSource<V> &
HKT<Atom<V>> & {
observableType(): "Atom"
set(newValue: V): void
modify(fn: (old: V) => V): void
}
export interface Bus<V> extends EventStream<V> {
push(newValue: V): void
end(): void
}
export type ScopeFn = (onIn: () => Unsub) => void
export type Scope = GenericTransformOpScoped & {
_L: typeof T_SCOPE
subscribe: ScopeFn
}
export function isScope(x: any): x is Scope {
return matchFlags(x, T_SCOPE)
}
export type Function0<R> = () => R
export type Function1<T1, R> = (t1: T1) => R
export type Function2<T1, T2, R> = (t1: T1, t2: T2) => R
export type Function3<T1, T2, T3, R> = (t1: T1, t2: T2, t3: T3) => R
export type Function4<T1, T2, T3, T4, R> = (t1: T1, t2: T2, t3: T3, t4: T4) => R
export type Function5<T1, T2, T3, T4, T5, R> = (
t1: T1,
t2: T2,
t3: T3,
t4: T4,
t5: T5
) => R
export type Function6<T1, T2, T3, T4, T5, T6, R> = (
t1: T1,
t2: T2,
t3: T3,
t4: T4,
t5: T5,
t6: T6
) => R