-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-commit.ts
More file actions
42 lines (34 loc) · 833 Bytes
/
async-commit.ts
File metadata and controls
42 lines (34 loc) · 833 Bytes
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
import { SigmaType } from "preact-sigma";
const SaveIndicator = new SigmaType<
{
savedCount: number;
saving: boolean;
},
{
saved: {
count: number;
};
}
>("SaveIndicator")
.defaultState({
savedCount: 0,
saving: false,
})
.actions({
async save() {
this.saving = true;
this.commit(); // Publish before the async boundary.
await Promise.resolve();
this.savedCount += 1;
this.saving = false;
this.commit(); // Publish before emitting the event boundary.
this.emit("saved", { count: this.savedCount });
},
});
const indicator = new SaveIndicator();
indicator.on("saved", ({ count }) => {
console.log(`Saved ${count} times`);
});
await indicator.save();
console.log(indicator.saving); // false
console.log(indicator.savedCount); // 1