@@ -120,14 +120,14 @@ function patchActivity(activity: WebChatActivity, { Date }: GlobalScopePonyfill)
120120
121121function upsertActivityWithSort (
122122 activities : WebChatActivity [ ] ,
123- incomingActivity : WebChatActivity ,
123+ upsertingActivity : WebChatActivity ,
124124 ponyfill : GlobalScopePonyfill
125125) : WebChatActivity [ ] {
126- const incomingLivestreamingMetadata = getActivityLivestreamingMetadata ( incomingActivity ) ;
126+ const upsertingLivestreamingMetadata = getActivityLivestreamingMetadata ( upsertingActivity ) ;
127127
128128 // TODO: [P1] To support time-travelling, we should not drop obsoleted livestreaming activities.
129- if ( incomingLivestreamingMetadata ) {
130- const { sessionId } = incomingLivestreamingMetadata ;
129+ if ( upsertingLivestreamingMetadata ) {
130+ const { sessionId } = upsertingLivestreamingMetadata ;
131131
132132 // If the upserting activity is going upsert into a concluded livestream, skip the activity.
133133 const isLivestreamConcluded = activities . find ( targetActivity => {
@@ -141,21 +141,21 @@ function upsertActivityWithSort(
141141 }
142142 }
143143
144- incomingActivity = patchActivity ( incomingActivity , ponyfill ) ;
144+ upsertingActivity = patchActivity ( upsertingActivity , ponyfill ) ;
145145
146- const { channelData : { clientActivityID : incomingClientActivityID } = { } } = incomingActivity ;
146+ const { channelData : { clientActivityID : upsertingClientActivityID } = { } } = upsertingActivity ;
147147
148148 const nextActivities = activities . filter (
149149 ( { channelData : { clientActivityID } = { } , id } ) =>
150150 // We will remove all "sending messages" activities and activities with same ID
151151 // "clientActivityID" is unique and used to track if the message has been sent and echoed back from the server
152- ! ( incomingClientActivityID && clientActivityID === incomingClientActivityID ) &&
153- ! ( id && id === incomingActivity . id )
152+ ! ( upsertingClientActivityID && clientActivityID === upsertingClientActivityID ) &&
153+ ! ( id && id === upsertingActivity . id )
154154 ) ;
155155
156- const incomingEntityPosition = incomingActivity . channelData ?. [ 'webchat:entity-position' ] ;
157- const incomingPartOf = incomingActivity . channelData ?. [ 'webchat:entity-part-of' ] ;
158- const incomingSequenceId = getSequenceIdOrDeriveFromTimestamp ( incomingActivity , ponyfill ) ;
156+ const upsertingEntityPosition = upsertingActivity . channelData ?. [ 'webchat:entity-position' ] ;
157+ const upsertingPartOf = upsertingActivity . channelData ?. [ 'webchat:entity-part-of' ] ;
158+ const upsertingSequenceId = getSequenceIdOrDeriveFromTimestamp ( upsertingActivity , ponyfill ) ;
159159
160160 // TODO: [P2] Turn (activity) => boolean into comparer (x, y) => number.
161161 // It is not trivial to write in current form.
@@ -175,12 +175,12 @@ function upsertActivityWithSort(
175175 const currentEntityPosition = channelData [ 'webchat:entity-position' ] ;
176176 const currentEntityPartOf = channelData [ 'webchat:entity-part-of' ] ;
177177
178- const bothHavePosition = typeof currentEntityPosition === 'number' && typeof incomingEntityPosition === 'number' ;
179- const bothArePartOf = typeof currentEntityPartOf === 'string' && currentEntityPartOf === incomingPartOf ;
178+ const bothHavePosition = typeof currentEntityPosition === 'number' && typeof upsertingEntityPosition === 'number' ;
179+ const bothArePartOf = typeof currentEntityPartOf === 'string' && currentEntityPartOf === upsertingPartOf ;
180180
181181 // For activities in the same creative work part, position is primary sort key
182182 if ( bothHavePosition && bothArePartOf ) {
183- return currentEntityPosition > incomingEntityPosition ;
183+ return currentEntityPosition > upsertingEntityPosition ;
184184 }
185185
186186 return false ;
@@ -191,19 +191,19 @@ function upsertActivityWithSort(
191191 const currentLivestreamingMetadata = getActivityLivestreamingMetadata ( activity ) ;
192192
193193 if (
194- incomingLivestreamingMetadata &&
194+ upsertingLivestreamingMetadata &&
195195 currentLivestreamingMetadata &&
196- incomingLivestreamingMetadata . sessionId === currentLivestreamingMetadata . sessionId
196+ upsertingLivestreamingMetadata . sessionId === currentLivestreamingMetadata . sessionId
197197 ) {
198- return currentLivestreamingMetadata . sequenceNumber > incomingLivestreamingMetadata . sequenceNumber ;
198+ return currentLivestreamingMetadata . sequenceNumber > upsertingLivestreamingMetadata . sequenceNumber ;
199199 }
200200
201201 return false ;
202202 } ) ;
203203 }
204204
205- // If the incoming activity does not have sequence ID or timestamp, always append it.
206- if ( ! ~ indexToInsert && typeof incomingSequenceId === 'number' ) {
205+ // If the upserting activity does not have sequence ID or timestamp, always append it.
206+ if ( ! ~ indexToInsert && typeof upsertingSequenceId === 'number' ) {
207207 indexToInsert = nextActivities . findIndex ( activity => {
208208 const currentSequenceId = getSequenceIdOrDeriveFromTimestamp ( activity , ponyfill ) ;
209209
@@ -213,7 +213,7 @@ function upsertActivityWithSort(
213213 return false ;
214214 }
215215
216- return currentSequenceId > incomingSequenceId ;
216+ return currentSequenceId > upsertingSequenceId ;
217217 } ) ;
218218 }
219219
@@ -222,32 +222,36 @@ function upsertActivityWithSort(
222222 indexToInsert = nextActivities . length ;
223223 }
224224
225- const prevActivity : WebChatActivity = indexToInsert === 0 ? undefined : nextActivities . at ( indexToInsert - 1 ) ;
226- const nextActivity : WebChatActivity = nextActivities . at ( indexToInsert ) ;
227- let incomingPosition : number ;
225+ const prevSibling : WebChatActivity = indexToInsert === 0 ? undefined : nextActivities . at ( indexToInsert - 1 ) ;
226+ const nextSibling : WebChatActivity = nextActivities . at ( indexToInsert ) ;
227+ let upsertingPosition : number ;
228228
229- if ( prevActivity ) {
230- const prevPosition = prevActivity . channelData [ 'webchat:internal:position' ] ;
229+ if ( prevSibling ) {
230+ const prevPosition = prevSibling . channelData [ 'webchat:internal:position' ] ;
231231
232- if ( nextActivity ) {
233- const nextSequenceId = nextActivity . channelData [ 'webchat:internal:position' ] ;
232+ if ( nextSibling ) {
233+ const nextSequenceId = nextSibling . channelData [ 'webchat:internal:position' ] ;
234234
235235 // eslint-disable-next-line no-magic-numbers
236- incomingPosition = ( prevPosition + nextSequenceId ) / 2 ;
236+ upsertingPosition = ( prevPosition + nextSequenceId ) / 2 ;
237237 } else {
238- incomingPosition = prevPosition + 1 ;
238+ upsertingPosition = prevPosition + 1 ;
239239 }
240- } else if ( nextActivity ) {
241- const nextSequenceId = nextActivity . channelData [ 'webchat:internal:position' ] ;
240+ } else if ( nextSibling ) {
241+ const nextSequenceId = nextSibling . channelData [ 'webchat:internal:position' ] ;
242242
243- incomingPosition = nextSequenceId - 1 ;
243+ upsertingPosition = nextSequenceId - 1 ;
244244 } else {
245- incomingPosition = 1 ;
245+ upsertingPosition = 1 ;
246246 }
247247
248- incomingActivity = updateIn ( incomingActivity , [ 'channelData' , 'webchat:internal:position' ] , ( ) => incomingPosition ) ;
248+ upsertingActivity = updateIn (
249+ upsertingActivity ,
250+ [ 'channelData' , 'webchat:internal:position' ] ,
251+ ( ) => upsertingPosition
252+ ) ;
249253
250- nextActivities . splice ( indexToInsert , 0 , incomingActivity ) ;
254+ nextActivities . splice ( indexToInsert , 0 , upsertingActivity ) ;
251255
252256 return nextActivities ;
253257}
0 commit comments