Skip to content

Commit 7bbf7b0

Browse files
committed
standardize badge logic
1 parent afb73e8 commit 7bbf7b0

2 files changed

Lines changed: 54 additions & 23 deletions

File tree

frontend/app/store/badge.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,31 @@ function setupBadgesSubscription() {
214214
}
215215
if (data.clear) {
216216
globalStore.set(curAtom, null);
217-
} else if (data.badge != null) {
218-
const existing = globalStore.get(curAtom);
219-
const candidates = existing != null ? [existing, data.badge] : [data.badge];
220-
globalStore.set(curAtom, sortBadges(candidates)[0]);
217+
return;
218+
}
219+
if (data.badge == null) {
220+
return;
221+
}
222+
const existing = globalStore.get(curAtom);
223+
if (existing == null || cmpBadge(data.badge, existing) > 0) {
224+
globalStore.set(curAtom, data.badge);
221225
}
222226
},
223227
});
224228
}
225229

230+
function cmpBadge(a: Badge, b: Badge): number {
231+
if (a.priority !== b.priority) {
232+
return a.priority > b.priority ? 1 : -1;
233+
}
234+
if (a.badgeid !== b.badgeid) {
235+
return a.badgeid > b.badgeid ? 1 : -1;
236+
}
237+
return 0;
238+
}
239+
226240
function sortBadges(badges: Badge[]): Badge[] {
227-
return [...badges].sort((a, b) => {
228-
if (a.priority !== b.priority) {
229-
return b.priority - a.priority;
230-
}
231-
return b.badgeid < a.badgeid ? -1 : b.badgeid > a.badgeid ? 1 : 0;
232-
});
241+
return [...badges].sort((a, b) => cmpBadge(b, a));
233242
}
234243

235244
function sortBadgesForTab(badges: Badge[]): Badge[] {

pkg/wcore/badge.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,56 @@ func handleBadgeEvent(event *wps.WaveEvent) {
7272
setBadge(oref, data)
7373
}
7474

75+
// cmpBadge compares two badges by priority then by badgeid (both descending).
76+
// Returns 1 if a > b, -1 if a < b, 0 if equal.
77+
func cmpBadge(a, b baseds.Badge) int {
78+
if a.Priority != b.Priority {
79+
if a.Priority > b.Priority {
80+
return 1
81+
}
82+
return -1
83+
}
84+
if a.BadgeId != b.BadgeId {
85+
if a.BadgeId > b.BadgeId {
86+
return 1
87+
}
88+
return -1
89+
}
90+
return 0
91+
}
92+
7593
// setBadge updates the in-memory transient map.
7694
func setBadge(oref waveobj.ORef, data baseds.BadgeEvent) {
7795
globalBadgeStore.lock.Lock()
7896
defer globalBadgeStore.lock.Unlock()
7997

8098
orefStr := oref.String()
99+
if orefStr == "" {
100+
return
101+
}
81102

82-
shouldClear := data.Clear
83103
if data.ClearById != "" {
84104
existing, ok := globalBadgeStore.transient[orefStr]
85105
if !ok || existing.BadgeId != data.ClearById {
86106
return
87107
}
88-
shouldClear = true
89-
} else if !data.Clear {
90-
shouldClear = data.Badge == nil
108+
delete(globalBadgeStore.transient, orefStr)
109+
log.Printf("badge store: badge cleared by id: oref=%s id=%s\n", orefStr, data.ClearById)
110+
return
91111
}
92-
93-
if shouldClear {
112+
if data.Clear {
94113
delete(globalBadgeStore.transient, orefStr)
95114
log.Printf("badge store: badge cleared: oref=%s\n", orefStr)
96-
} else {
97-
incoming := *data.Badge
98-
existing, hasExisting := globalBadgeStore.transient[orefStr]
99-
if !hasExisting || incoming.Priority > existing.Priority || (incoming.Priority == existing.Priority && incoming.BadgeId > existing.BadgeId) {
100-
globalBadgeStore.transient[orefStr] = incoming
101-
log.Printf("badge store: badge set: oref=%s badge=%+v\n", orefStr, incoming)
102-
}
115+
return
116+
}
117+
if data.Badge == nil {
118+
return
119+
}
120+
incoming := *data.Badge
121+
existing, hasExisting := globalBadgeStore.transient[orefStr]
122+
if !hasExisting || cmpBadge(incoming, existing) > 0 {
123+
globalBadgeStore.transient[orefStr] = incoming
124+
log.Printf("badge store: badge set: oref=%s badge=%+v\n", orefStr, incoming)
103125
}
104126
}
105127

0 commit comments

Comments
 (0)