stream-chat 8.47.0 → 8.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -311,14 +311,26 @@ export function formatMessage<StreamChatGenerics extends ExtendableGenerics = De
311
311
  export const findIndexInSortedArray = <T, L>({
312
312
  needle,
313
313
  sortedArray,
314
+ selectKey,
314
315
  selectValueToCompare = (e) => e,
315
316
  sortDirection = 'ascending',
316
317
  }: {
317
318
  needle: T;
318
319
  sortedArray: readonly T[];
319
320
  /**
320
- * In array of objects (like messages), pick a specific
321
- * property to compare needle value to.
321
+ * In an array of objects (like messages), pick a unique property identifying
322
+ * an element. It will be used to find a direct match for the needle element
323
+ * in case compare values are not unique.
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * selectKey: (message) => message.id
328
+ * ```
329
+ */
330
+ selectKey?: (arrayElement: T) => string;
331
+ /**
332
+ * In an array of objects (like messages), pick a specific
333
+ * property to compare the needle value to.
322
334
  *
323
335
  * @example
324
336
  * ```ts
@@ -353,11 +365,9 @@ export const findIndexInSortedArray = <T, L>({
353
365
 
354
366
  const comparableMiddle = selectValueToCompare(sortedArray[middle]);
355
367
 
356
- // if (comparableNeedle === comparableMiddle) return middle;
357
-
358
368
  if (
359
369
  (sortDirection === 'ascending' && comparableNeedle < comparableMiddle) ||
360
- (sortDirection === 'descending' && comparableNeedle > comparableMiddle)
370
+ (sortDirection === 'descending' && comparableNeedle >= comparableMiddle)
361
371
  ) {
362
372
  right = middle - 1;
363
373
  } else {
@@ -365,6 +375,23 @@ export const findIndexInSortedArray = <T, L>({
365
375
  }
366
376
  }
367
377
 
378
+ // In case there are several array elements with the same comparable value, search around the insertion
379
+ // point to possibly find an element with the same key. If found, prefer it.
380
+ // This, for example, prevents duplication of messages with the same creation date.
381
+ if (selectKey) {
382
+ const needleKey = selectKey(needle);
383
+ const step = sortDirection === 'ascending' ? -1 : +1;
384
+ for (
385
+ let i = left + step;
386
+ 0 <= i && i < sortedArray.length && selectValueToCompare(sortedArray[i]) === comparableNeedle;
387
+ i += step
388
+ ) {
389
+ if (selectKey(sortedArray[i]) === needleKey) {
390
+ return i;
391
+ }
392
+ }
393
+ }
394
+
368
395
  return left;
369
396
  };
370
397
 
@@ -410,19 +437,18 @@ export function addToMessageList<T extends FormatMessageResponse>(
410
437
  sortDirection: 'ascending',
411
438
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
412
439
  selectValueToCompare: (m) => m[sortBy]!.getTime(),
440
+ selectKey: (m) => m.id,
413
441
  });
414
442
 
415
443
  // message already exists and not filtered with timestampChanged, update and return
416
- if (!timestampChanged && newMessage.id) {
417
- if (newMessages[insertionIndex] && newMessage.id === newMessages[insertionIndex].id) {
418
- newMessages[insertionIndex] = newMessage;
419
- return newMessages;
420
- }
421
-
422
- if (newMessages[insertionIndex - 1] && newMessage.id === newMessages[insertionIndex - 1].id) {
423
- newMessages[insertionIndex - 1] = newMessage;
424
- return newMessages;
425
- }
444
+ if (
445
+ !timestampChanged &&
446
+ newMessage.id &&
447
+ newMessages[insertionIndex] &&
448
+ newMessage.id === newMessages[insertionIndex].id
449
+ ) {
450
+ newMessages[insertionIndex] = newMessage;
451
+ return newMessages;
426
452
  }
427
453
 
428
454
  // do not add updated or deleted messages to the list if they already exist or come with a timestamp change