/
LazyEvt.ts
44 lines (28 loc) 路 1.04 KB
/
LazyEvt.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
import { overwriteReadonlyProp } from "https://raw.githubusercontent.com/garronej/tsafe/v1.0.0/deno_dist/lab/overwriteReadonlyProp.ts";
import { importProxy } from "./importProxy.ts";
import type { Evt } from "./types/index.ts";
export class LazyEvt<T> {
private initialPostCount = 0;
get evt(): Evt<T> {
if (this.__evt === undefined) {
this.__evt = new importProxy.Evt();
overwriteReadonlyProp(this.__evt, "postCount", this.initialPostCount);
}
return this.__evt;
}
declare private __evt: Evt<T>;
private __post(data: T, doWait: false): number;
private __post(data: T, doWait: true): Promise<void>;
private __post(data: T, doWait: boolean): number | Promise<void> {
if (this.__evt === undefined) {
return ++this.initialPostCount;
}
return this.__evt[doWait ? "postAndWait" : "post"](data);
}
post(data: T) {
return this.__post(data, false);
}
postAndWait(data: T) {
return this.__post(data, true);
}
}