-
Notifications
You must be signed in to change notification settings - Fork 4
/
Flow.ts
62 lines (51 loc) · 1.31 KB
/
Flow.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
export class Boxed<T> {
public readonly flow: Flow<T>;
constructor(flow: Flow<T>) {
this.flow = flow;
}
}
export class Flow<T> {
public readonly value: T;
public readonly source?: Source;
private constructor(value: T, source?: Source) {
this.value = value;
this.source = source;
}
public static ["of"]<T>(value: Boxed<T> | T): Flow<T> {
if (value instanceof Boxed) {
return value.flow;
}
return new Flow(value);
}
public static tainted<T>(value: T): T {
return Flow.of(value).taint().watch();
}
public get isTainted() {
return this.source !== undefined;
}
public taint(meta?: {}): Flow<T> {
let {source} = this;
if (source === undefined) {
source = {
error: new Error(),
meta,
};
}
return new Flow(this.value, source);
}
public alter<V>(value: V): Flow<V> {
return new Flow(value, this.source);
}
public release(): T {
return this.value;
}
public watch(): T {
// We're intentionally tricking the type system at this point.
// tslint:disable-next-line: no-any
return <any> new Boxed(this);
}
}
interface Source {
error: Error;
meta?: {};
}