-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbadge.ts
More file actions
267 lines (245 loc) · 7.82 KB
/
Copy pathbadge.ts
File metadata and controls
267 lines (245 loc) · 7.82 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { RpcApi } from "@/app/store/wshclientapi";
import { TabRpcClient } from "@/app/store/wshrpcutil";
import { WaveEnv, WaveEnvSubset } from "@/app/waveenv/waveenv";
import { fireAndForget, NullAtom } from "@/util/util";
import { atom, Atom, PrimitiveAtom } from "jotai";
import { v7 as uuidv7, version as uuidVersion } from "uuid";
import { globalStore } from "./jotaiStore";
import * as WOS from "./wos";
import { waveEventSubscribeSingle } from "./wps";
export type BadgeEnv = WaveEnvSubset<{
rpc: {
EventPublishCommand: WaveEnv["rpc"]["EventPublishCommand"];
};
}>;
export type LoadBadgesEnv = WaveEnvSubset<{
rpc: {
GetAllBadgesCommand: WaveEnv["rpc"]["GetAllBadgesCommand"];
};
}>;
export type TabBadgesEnv = WaveEnvSubset<{
wos: WaveEnv["wos"];
}>;
const BadgeMap = new Map<string, PrimitiveAtom<Badge>>();
const TabBadgeAtomCache = new Map<string, Atom<Badge[]>>();
function publishBadgeEvent(eventData: WaveEvent, env?: BadgeEnv) {
if (env != null) {
fireAndForget(() => env.rpc.EventPublishCommand(TabRpcClient, eventData));
} else {
fireAndForget(() => RpcApi.EventPublishCommand(TabRpcClient, eventData));
}
}
function clearBadgeInternal(oref: string, env?: BadgeEnv) {
const eventData: WaveEvent = {
event: "badge",
scopes: [oref],
data: {
oref: oref,
clear: true,
} as BadgeEvent,
};
publishBadgeEvent(eventData, env);
}
function clearBadgesForBlockOnFocus(blockId: string, env?: BadgeEnv) {
const oref = WOS.makeORef("block", blockId);
const badgeAtom = BadgeMap.get(oref);
const badge = badgeAtom != null ? globalStore.get(badgeAtom) : null;
if (badge != null && !badge.pidlinked) {
clearBadgeInternal(oref, env);
}
}
function clearBadgesForTabOnFocus(tabId: string, env?: BadgeEnv) {
const oref = WOS.makeORef("tab", tabId);
const badgeAtom = BadgeMap.get(oref);
const badge = badgeAtom != null ? globalStore.get(badgeAtom) : null;
if (badge != null && !badge.pidlinked) {
clearBadgeInternal(oref, env);
}
}
function clearAllBadges(env?: BadgeEnv) {
const eventData: WaveEvent = {
event: "badge",
scopes: [],
data: {
oref: "",
clearall: true,
} as BadgeEvent,
};
publishBadgeEvent(eventData, env);
}
function clearBadgesForTab(tabId: string, env?: BadgeEnv) {
const tabAtom = WOS.getWaveObjectAtom<Tab>(WOS.makeORef("tab", tabId));
const tab = globalStore.get(tabAtom);
const blockIds = (tab as Tab)?.blockids ?? [];
for (const blockId of blockIds) {
const oref = WOS.makeORef("block", blockId);
const badgeAtom = BadgeMap.get(oref);
if (badgeAtom != null && globalStore.get(badgeAtom) != null) {
clearBadgeInternal(oref, env);
}
}
}
function getBadgeAtom(oref: string): PrimitiveAtom<Badge> {
if (oref == null) {
return NullAtom as PrimitiveAtom<Badge>;
}
let rtn = BadgeMap.get(oref);
if (rtn == null) {
rtn = atom(null) as PrimitiveAtom<Badge>;
BadgeMap.set(oref, rtn);
}
return rtn;
}
function getBlockBadgeAtom(blockId: string): Atom<Badge> {
if (blockId == null) {
return NullAtom as Atom<Badge>;
}
const oref = WOS.makeORef("block", blockId);
return getBadgeAtom(oref);
}
function getTabBadgeAtom(tabId: string, env?: TabBadgesEnv): Atom<Badge[]> {
if (tabId == null) {
return NullAtom as Atom<Badge[]>;
}
let rtn = TabBadgeAtomCache.get(tabId);
if (rtn != null) {
return rtn;
}
const tabOref = WOS.makeORef("tab", tabId);
const tabBadgeAtom = getBadgeAtom(tabOref);
const tabAtom = env != null ? env.wos.getWaveObjectAtom<Tab>(tabOref) : WOS.getWaveObjectAtom<Tab>(tabOref);
rtn = atom((get) => {
const tab = get(tabAtom);
const blockIds = tab?.blockids ?? [];
const badges: Badge[] = [];
for (const blockId of blockIds) {
const badge = get(getBadgeAtom(WOS.makeORef("block", blockId)));
if (badge != null) {
badges.push(badge);
}
}
const tabBadge = get(tabBadgeAtom);
if (tabBadge != null) {
badges.push(tabBadge);
}
return sortBadgesForTab(badges);
});
TabBadgeAtomCache.set(tabId, rtn);
return rtn;
}
async function loadBadges(env?: LoadBadgesEnv) {
const rpc = env != null ? env.rpc : RpcApi;
const badges = await rpc.GetAllBadgesCommand(TabRpcClient);
if (badges == null) {
return;
}
for (const badgeEvent of badges) {
if (badgeEvent.oref == null) {
continue;
}
const curAtom = getBadgeAtom(badgeEvent.oref);
globalStore.set(curAtom, badgeEvent.badge ?? null);
}
}
function setBadge(blockId: string, badge: Omit<Badge, "badgeid"> & { badgeid?: string }, env?: BadgeEnv) {
if (!badge.badgeid) {
badge = { ...badge, badgeid: uuidv7() };
} else if (uuidVersion(badge.badgeid) !== 7) {
throw new Error(`setBadge: badgeid must be a v7 UUID, got version ${uuidVersion(badge.badgeid)}`);
}
const oref = WOS.makeORef("block", blockId);
const eventData: WaveEvent = {
event: "badge",
scopes: [oref],
data: {
oref: oref,
badge: badge,
} as BadgeEvent,
};
publishBadgeEvent(eventData, env);
}
function clearBadgeById(blockId: string, badgeId: string, env?: BadgeEnv) {
const oref = WOS.makeORef("block", blockId);
const eventData: WaveEvent = {
event: "badge",
scopes: [oref],
data: {
oref: oref,
clearbyid: badgeId,
} as BadgeEvent,
};
publishBadgeEvent(eventData, env);
}
function setupBadgesSubscription() {
waveEventSubscribeSingle({
eventType: "badge",
handler: (event) => {
const data = event.data;
if (data?.clearall) {
for (const atom of BadgeMap.values()) {
globalStore.set(atom, null);
}
return;
}
if (data?.oref == null) {
return;
}
const curAtom = getBadgeAtom(data.oref);
if (data.clearbyid) {
const existing = globalStore.get(curAtom);
if (existing?.badgeid === data.clearbyid) {
globalStore.set(curAtom, null);
}
return;
}
if (data.clear) {
globalStore.set(curAtom, null);
return;
}
if (data.badge == null) {
return;
}
const existing = globalStore.get(curAtom);
if (existing == null || cmpBadge(data.badge, existing) > 0) {
globalStore.set(curAtom, data.badge);
}
},
});
}
function cmpBadge(a: Badge, b: Badge): number {
if (a.priority !== b.priority) {
return a.priority > b.priority ? 1 : -1;
}
if (a.badgeid !== b.badgeid) {
return a.badgeid > b.badgeid ? 1 : -1;
}
return 0;
}
function sortBadges(badges: Badge[]): Badge[] {
return [...badges].sort((a, b) => cmpBadge(b, a));
}
function sortBadgesForTab(badges: Badge[]): Badge[] {
return [...badges].sort((a, b) => {
if (a.priority !== b.priority) {
return b.priority - a.priority;
}
return a.badgeid < b.badgeid ? -1 : a.badgeid > b.badgeid ? 1 : 0;
});
}
export {
clearAllBadges,
clearBadgeById,
clearBadgesForBlockOnFocus,
clearBadgesForTab,
clearBadgesForTabOnFocus,
getBadgeAtom,
getBlockBadgeAtom,
getTabBadgeAtom,
loadBadges,
setBadge,
setupBadgesSubscription,
sortBadges,
sortBadgesForTab,
};