EVT
Search…
evt.post*(data)

evt.post(data)

Equivalent of eventEmitter.emit() and subject.next().
Returns evt.postCount

evt.postCount: number

The number of times evt.post() has been called. It's a read-only property.
1
import { Evt } from "evt";
2
3
const evtText= new Evt<string>();
4
5
//prints 0
6
console.log(evtText.postCount);
7
8
evtText.post("foo");
9
evtText.post("bar");
10
evtText.post("baz");
11
12
//prints 3
13
console.log(evtText.postCount);
Copied!

evt.postAsyncOnceHandled(data)

Post the event data only once there is at least one handler candidate to handle it.
When evt.isHandled(data) return true, post(data) is invoked synchronously and the new post count is returned. When postAsyncOnceHandled(data) is invoked at a time whereevt.isHandled(data) returns false, the data will be kept on hold and posted only once a candidate handler is attached.
evt.post(data) is not invoked synchronously as soon as the candidate handler is attached but is scheduled to be invoked in a microtask. When the call to post is delayed postAsyncOnceHandled(data) returns a promise that resolves with the new post count after post(data) has been invoked.
1
import { Evt } from "evt";
2
3
function createPreloadedEvtText(): Evt<string>{
4
5
const evtText = new Evt<string>();
6
7
(async ()=>{
8
9
await evtText.postAsyncOnceHandled("foo");
10
evtText.post("bar");
11
12
})();
13
14
15
return evtText;
16
17
}
18
19
const evtText = createPreloadedEvtText();
20
21
evtText.attach(text => console.log("1 " + text));
22
evtText.attach(text => console.log("2 " + text));
23
24
console.log("BEFORE");
25
26
//"BEFORE" then (next micro task) "1 foo" "2 foo" "1 bar" "2 bar"
Copied!
evt.postSyncOnceHandled() does not exist because it is preferable to wait for the next event cycle before posting the event. For example, the previous example would not print "2 foo" if we had used evt.postSyncOnceHandled()

evt.postAndWait(data): Promise<void>

Flavor of post that returns a promise that resolves after all asynchronous Handler's callbacks that matches the event data has resolved.
1
import { Evt } from "evt";
2
3
const evt = Evt.create();
4
5
evt.attach(async () => {
6
7
await new Promise(resolve => setTimeout(resolve, 100));
8
9
console.log("bar");
10
11
});
12
13
(async () => {
14
15
console.log("foo");
16
17
await evt.postAndWait();
18
19
console.log("baz");
20
21
22
})();
23
24
//"foo bar baz" is printed to the console.
Copied!
Last modified 11mo ago