@@ -94,6 +94,9 @@ const registeredActivityPushTokens = new Map<string, number>(); |
94 | 94 | // A failed read keeps the current account's last rows, but must not leave the |
95 | 95 | // widget claiming the user is signed out or showing a previous account. |
96 | 96 | let widgetShowsCurrentAccount = false; |
| 97 | +// Refreshes can overlap (sign-in, foreground, retry); only the newest read may |
| 98 | +// repaint, so a slow older response never overwrites newer rows. |
| 99 | +let widgetSnapshotReads = 0; |
97 | 100 | let androidDeviceReplayedAt: number | null = null; |
98 | 101 | let pushTokenSubscription: { remove: () => void } | null = null; |
99 | 102 | let appStateSubscription: { remove: () => void } | null = null; |
@@ -917,6 +920,7 @@ export function __resetAgentAwarenessRemoteRegistrationForTest(): void { |
917 | 920 | registeredActivityPushTokens.clear(); |
918 | 921 | activityPushTokenLocks.clear(); |
919 | 922 | widgetShowsCurrentAccount = false; |
| 923 | + widgetSnapshotReads = 0; |
920 | 924 | } |
921 | 925 |
|
922 | 926 | export function unregisterAgentAwarenessDeviceForCurrentUser( |
@@ -1059,21 +1063,28 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect< |
1059 | 1063 | } |
1060 | 1064 |
|
1061 | 1065 | // The home-screen widget has no push path of its own, so every foreground |
1062 | | - // refresh repaints it from one snapshot read. Returns false once the account |
1063 | | - // changed during the read, so a stale result is never shown or primed. |
| 1066 | + // refresh repaints it from one snapshot read. Yields undefined once the |
| 1067 | + // account changed or a newer read started, so a stale result is never |
| 1068 | + // shown or primed. |
1064 | 1069 | const identity = relayTokenProviderIdentity; |
1065 | | - const repaintWidget = (snapshot: RelayAgentActivitySnapshotResponse | null): boolean => { |
1066 | | - if (!relayTokenProvider || relayTokenProviderIdentity !== identity) { |
1067 | | - return false; |
| 1070 | + const refreshWidget = Effect.gen(function* () { |
| 1071 | + const read = ++widgetSnapshotReads; |
| 1072 | + const snapshot = yield* readAgentActivitySnapshot(); |
| 1073 | + if ( |
| 1074 | + read !== widgetSnapshotReads || |
| 1075 | + !relayTokenProvider || |
| 1076 | + relayTokenProviderIdentity !== identity |
| 1077 | + ) { |
| 1078 | + return undefined; |
1068 | 1079 | } |
1069 | 1080 | if (snapshot) { |
1070 | 1081 | widgetShowsCurrentAccount = true; |
1071 | 1082 | publishWidget({ kind: "snapshot", aggregate: snapshot.aggregate }); |
1072 | 1083 | } else if (!widgetShowsCurrentAccount) { |
1073 | 1084 | publishWidget({ kind: "unavailable" }); |
1074 | 1085 | } |
1075 | | - return true; |
1076 | | - }; |
| 1086 | + return { snapshot }; |
| 1087 | + }); |
1077 | 1088 |
|
1078 | 1089 | let activities = yield* Effect.try({ |
1079 | 1090 | try: () => getAgentLiveActivities(), |
@@ -1108,10 +1119,11 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect< |
1108 | 1119 | // remote-starts one (background push-to-start wakes proved too unreliable |
1109 | 1120 | // to hand the token over). |
1110 | 1121 | if (!hadActivities) { |
1111 | | - const snapshot = yield* readAgentActivitySnapshot(); |
1112 | | - if (!repaintWidget(snapshot)) { |
| 1122 | + const widgetRead = yield* refreshWidget; |
| 1123 | + if (!widgetRead) { |
1113 | 1124 | return; |
1114 | 1125 | } |
| 1126 | + const { snapshot } = widgetRead; |
1115 | 1127 | const preferences = yield* Effect.tryPromise({ |
1116 | 1128 | try: () => loadPreferences(), |
1117 | 1129 | catch: (cause) => |
@@ -1185,7 +1197,7 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect< |
1185 | 1197 | // An existing card keeps its push token registered without waiting on the |
1186 | 1198 | // relay; the widget still repaints afterwards. |
1187 | 1199 | if (hadActivities) { |
1188 | | - repaintWidget(yield* readAgentActivitySnapshot()); |
| 1200 | + yield* refreshWidget; |
1189 | 1201 | } |
1190 | 1202 | }); |
1191 | 1203 | } |