/
StatefulEvt.ts
172 lines (116 loc) · 5.02 KB
/
StatefulEvt.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
import { LazyEvt } from "./LazyEvt.ts";
import { LazyStatefulEvt } from "./LazyStatefulEvt.ts";
import { importProxy } from "./importProxy.ts";
import { Evt, onAddHandlerByEvt } from "./Evt.ts";
import type { CtxLike, StateDiff, NonPostableEvt, StatefulReadonlyEvt } from "./types/index.ts";
import { assert } from "https://deno.land/x/tsafe@v1.6.6/assert.ts";
import { id } from "https://deno.land/x/tsafe@v1.6.6/id.ts";
/** https://docs.evt.land/api/statefulevt */
export type StatefulEvt<T> = import("./types/interfaces/index.ts").StatefulEvt<T>;
const runSideEffect = (sideEffect: ()=> void) => sideEffect();
class StatefulEvtImpl<T> extends Evt<T> implements StatefulEvt<T> {
__state: T;
get state(): T { return this.__state; }
set state(value: T) {
if (this.state === value) return;
this.post(value);
}
constructor(initialState: T) {
super();
this.__state = initialState;
this.lazyEvtChange = new LazyStatefulEvt(this.__state);
onAddHandlerByEvt.set(
this,
(handler, handlerTrigger) => {
if( handler.extract ){
return;
}
let sideEffect= id<(()=> void) | undefined>(undefined);
const opResult = this.getInvocableOp(handler.op)(
this.__state,
this.setOpResultForPipe !== undefined ? (sideEffect_=>sideEffect =sideEffect_) : runSideEffect
);
if( this.setOpResultForPipe !== undefined ){
this.setOpResultForPipe(opResult);
if( sideEffect !== undefined ){
sideEffect();
}
}
if (!opResult) {
return;
}
handlerTrigger(opResult);
}
);
}
private readonly lazyEvtDiff = new LazyEvt<StateDiff<T>>();
get evtDiff(): NonPostableEvt<StateDiff<T>> { return this.lazyEvtDiff.evt; }
private readonly lazyEvtChange: LazyStatefulEvt<T>;
get evtChange(): StatefulReadonlyEvt<T> { return this.lazyEvtChange.evt; }
private readonly lazyEvtChangeDiff = new LazyEvt<StateDiff<T>>();
get evtChangeDiff(): NonPostableEvt<StateDiff<T>> { return this.lazyEvtChangeDiff.evt; }
post(data: T): number {
return this.__post(data, false, false);
}
postForceChange(wData?: readonly [T]): number {
return this.__post(!!wData ? wData[0] : this.state, true, false);
}
postAndWait(data: T) {
return this.__post(data, false, true);
}
private __post(data: T, forceChange: boolean, doWait: false): number;
private __post(data: T, forceChange: boolean, doWait: true): Promise<void>;
private __post(data: T, forceChange: boolean, doWait: boolean): number | Promise<void> {
const prevState = this.state;
this.__state = data;
const diff = { prevState, "newState": this.state };
const postVariantName = doWait ? "postAndWait" : "post";
const prs: Promise<void>[] = [];
const r1 = this.lazyEvtDiff[postVariantName](diff);
if (doWait) {
prs.push(r1 as any);
}
if (forceChange || !Object.is(prevState, this.state)) {
const r2 = this.lazyEvtChange[postVariantName](this.state);
const r3 = this.lazyEvtChangeDiff[postVariantName](diff);
if (doWait) {
prs.push(r2 as any, r3 as any);
}
}
const r4 = super[postVariantName](data);
return doWait ?
(prs.push(r4 as any), Promise.all(prs).then(() => { })) :
r4;
}
private setOpResultForPipe: ((opResult: readonly [any] | null) => void) | undefined;
pipe(...args: any[]): StatefulEvt<any> {
let opResult: readonly [any] | null | undefined = undefined;
this.setOpResultForPipe = opResult_ => opResult = opResult_;
const evt = super
.pipe(...(args as Parameters<typeof importProxy.Evt.prototype.pipe>))
;
assert(opResult !== undefined);
this.setOpResultForPipe = undefined;
if (!opResult) {
throw new Error([
"Cannot pipe StatefulEvt because the operator does not match",
"it's current state. You would end up with evt.state === undefined",
"Use evt.toStateless([ctx]).pipe(op).toStatic(initialState)",
"to be sure the StatefulEvt does not have an undefined state"
].join(" "));
}
return evt.toStateful(opResult[0]);
}
toStateless(ctx?: CtxLike): Evt<any> {
const onAddHandler= onAddHandlerByEvt.get(this)!;
onAddHandlerByEvt.delete(this);
const out= !!ctx ? super.pipe(ctx) : super.pipe();
onAddHandlerByEvt.set(this, onAddHandler);
return out;
}
}
export const StatefulEvt: {
new <T>(initialState: T): StatefulEvt<T>;
readonly prototype: StatefulEvt<any>;
} = StatefulEvtImpl;
importProxy.StatefulEvt = StatefulEvt;