Skip to content

Commit 7e5769c

Browse files
committed
Fix tests
1 parent 2b01003 commit 7e5769c

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

  • packages/core/src/reducers/activities/sort

packages/core/src/reducers/activities/sort/upsert.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,43 @@ function upsert(ponyfill: Pick<GlobalScopePonyfill, 'Date'>, state: State, activ
133133
nextLivestreamSessionMap.set(sessionId, Object.freeze(nextSessionEntry));
134134

135135
// 5. sortedActivities: insert the new revision into the session's block.
136-
// Find where the session's last activity lives in the sorted array and splice after it.
137-
// eslint-disable-next-line no-magic-numbers
138-
const prevLastSessionActivity = existingSession.activities.at(-1);
136+
// The session's activities are sorted by sequence number via insertSorted.
137+
// Find where the new activity landed in that list and locate the correct
138+
// insertion point in sortedActivities relative to its session neighbors.
139+
const newIndexInSession = nextSessionEntry.activities.findIndex(
140+
entry => entry.activityLocalId === activityLocalId
141+
);
142+
143+
const successorInSession =
144+
newIndexInSession + 1 < nextSessionEntry.activities.length
145+
? nextSessionEntry.activities[newIndexInSession + 1]
146+
: undefined;
147+
139148
let insertIndex = state.sortedActivities.length;
140149

141-
if (prevLastSessionActivity) {
142-
for (let i = state.sortedActivities.length - 1; i >= 0; i--) {
150+
if (successorInSession) {
151+
// Insert before the successor activity in sortedActivities.
152+
for (let i = 0; i < state.sortedActivities.length; i++) {
143153
// eslint-disable-next-line security/detect-object-injection
144-
if (getLocalIdFromActivity(state.sortedActivities[i]!) === prevLastSessionActivity.activityLocalId) {
145-
insertIndex = i + 1;
154+
if (getLocalIdFromActivity(state.sortedActivities[i]!) === successorInSession.activityLocalId) {
155+
insertIndex = i;
146156
break;
147157
}
148158
}
159+
} else {
160+
// New activity is last in the session; insert after the previous last activity.
161+
// eslint-disable-next-line no-magic-numbers
162+
const prevLastSessionActivity = existingSession.activities.at(-1);
163+
164+
if (prevLastSessionActivity) {
165+
for (let i = state.sortedActivities.length - 1; i >= 0; i--) {
166+
// eslint-disable-next-line security/detect-object-injection
167+
if (getLocalIdFromActivity(state.sortedActivities[i]!) === prevLastSessionActivity.activityLocalId) {
168+
insertIndex = i + 1;
169+
break;
170+
}
171+
}
172+
}
149173
}
150174

151175
// 6. Position: assign the new activity a position based on its neighbors.

0 commit comments

Comments
 (0)