File size: 3,827 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type {
  FlightRouterState,
  FlightSegmentPath,
} from '../../../server/app-render/types'
import { DEFAULT_SEGMENT_KEY } from '../../../shared/lib/segment'
import { getNextFlightSegmentPath } from '../../flight-data-helpers'
import { matchSegment } from '../match-segments'
import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments'

/**
 * Deep merge of the two router states. Parallel route keys are preserved if the patch doesn't have them.
 */
function applyPatch(
  initialTree: FlightRouterState,
  patchTree: FlightRouterState
): FlightRouterState {
  const [initialSegment, initialParallelRoutes] = initialTree
  const [patchSegment, patchParallelRoutes] = patchTree

  // if the applied patch segment is __DEFAULT__ then it can be ignored in favor of the initial tree
  // this is because the __DEFAULT__ segment is used as a placeholder on navigation
  if (
    patchSegment === DEFAULT_SEGMENT_KEY &&
    initialSegment !== DEFAULT_SEGMENT_KEY
  ) {
    return initialTree
  }

  if (matchSegment(initialSegment, patchSegment)) {
    const newParallelRoutes: FlightRouterState[1] = {}
    for (const key in initialParallelRoutes) {
      const isInPatchTreeParallelRoutes =
        typeof patchParallelRoutes[key] !== 'undefined'
      if (isInPatchTreeParallelRoutes) {
        newParallelRoutes[key] = applyPatch(
          initialParallelRoutes[key],
          patchParallelRoutes[key]
        )
      } else {
        newParallelRoutes[key] = initialParallelRoutes[key]
      }
    }

    for (const key in patchParallelRoutes) {
      if (newParallelRoutes[key]) {
        continue
      }

      newParallelRoutes[key] = patchParallelRoutes[key]
    }

    const tree: FlightRouterState = [initialSegment, newParallelRoutes]

    // Copy over the existing tree
    if (initialTree[2]) {
      tree[2] = initialTree[2]
    }

    if (initialTree[3]) {
      tree[3] = initialTree[3]
    }

    if (initialTree[4]) {
      tree[4] = initialTree[4]
    }

    return tree
  }

  return patchTree
}

/**
 * Apply the router state from the Flight response, but skip patching default segments.
 * Useful for patching the router cache when navigating, where we persist the existing default segment if there isn't a new one.
 * Creates a new router state tree.
 */
export function applyRouterStatePatchToTree(
  flightSegmentPath: FlightSegmentPath,
  flightRouterState: FlightRouterState,
  treePatch: FlightRouterState,
  path: string
): FlightRouterState | null {
  const [segment, parallelRoutes, url, refetch, isRootLayout] =
    flightRouterState

  // Root refresh
  if (flightSegmentPath.length === 1) {
    const tree: FlightRouterState = applyPatch(flightRouterState, treePatch)

    addRefreshMarkerToActiveParallelSegments(tree, path)

    return tree
  }

  const [currentSegment, parallelRouteKey] = flightSegmentPath

  // Tree path returned from the server should always match up with the current tree in the browser
  if (!matchSegment(currentSegment, segment)) {
    return null
  }

  const lastSegment = flightSegmentPath.length === 2

  let parallelRoutePatch
  if (lastSegment) {
    parallelRoutePatch = applyPatch(parallelRoutes[parallelRouteKey], treePatch)
  } else {
    parallelRoutePatch = applyRouterStatePatchToTree(
      getNextFlightSegmentPath(flightSegmentPath),
      parallelRoutes[parallelRouteKey],
      treePatch,
      path
    )

    if (parallelRoutePatch === null) {
      return null
    }
  }

  const tree: FlightRouterState = [
    flightSegmentPath[0],
    {
      ...parallelRoutes,
      [parallelRouteKey]: parallelRoutePatch,
    },
    url,
    refetch,
  ]

  // Current segment is the root layout
  if (isRootLayout) {
    tree[4] = true
  }

  addRefreshMarkerToActiveParallelSegments(tree, path)

  return tree
}