File size: 1,152 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// @flow
// used to update feed caches with new threads in real time
// takes an array of existing threads in the cache and figures out how to insert the newly updated thread
export const parseRealtimeThreads = (
  prevThreads: Array<any>,
  updatedThread: Object
) => {
  // get an array of thread ids based on the threads already in cache
  const prevThreadIds = prevThreads.map(thread => thread.node.id);

  // determine if the newly updated thread exists in the cache or not
  // if it doesn't exist in cache, it's a new thread! if it does exist in cache, it just means its
  // been updated with a new message
  const hasNewThread = prevThreadIds.indexOf(updatedThread.id) < 0;

  return hasNewThread
    ? [
        {
          node: updatedThread,
          cursor: '__this-is-a-cursor__',
          __typename: 'Thread',
        },
        ...prevThreads,
      ]
    : [
        ...prevThreads.map(thread => {
          if (thread.node.id !== updatedThread.id) return thread;
          return {
            node: updatedThread,
            cursor: '__this-is-a-cursor__',
            __typename: 'Thread',
          };
        }),
      ];
};