Skip to content

Commit 672b242

Browse files
unstubbableeps1lon
andcommitted
[Flight] Avoid consuming cyclic models multiple times
Co-authored-by: "Sebastian \"Sebbie\" Silbermann" <sebastian.silbermann@vercel.com>
1 parent 90ab3f8 commit 672b242

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

packages/react-server/src/ReactFlightReplyServer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,9 @@ function createMap(
11221122
if ((model as any).$$consumed === true) {
11231123
throw new Error('Already initialized Map.');
11241124
}
1125-
const map = new Map(model);
1125+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
11261126
(model as any).$$consumed = true;
1127+
const map = new Map(model);
11271128
return map;
11281129
}
11291130

@@ -1134,8 +1135,9 @@ function createSet(response: Response, model: Array<any>): Set<any> {
11341135
if ((model as any).$$consumed === true) {
11351136
throw new Error('Already initialized Set.');
11361137
}
1137-
const set = new Set(model);
1138+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
11381139
(model as any).$$consumed = true;
1140+
const set = new Set(model);
11391141
return set;
11401142
}
11411143

@@ -1146,9 +1148,10 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
11461148
if ((model as any).$$consumed === true) {
11471149
throw new Error('Already initialized Iterator.');
11481150
}
1151+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
1152+
(model as any).$$consumed = true;
11491153
// $FlowFixMe[incompatible-use]: This uses raw Symbols because we're extracting from a native array.
11501154
const iterator = model[Symbol.iterator]();
1151-
(model as any).$$consumed = true;
11521155
return iterator;
11531156
}
11541157

0 commit comments

Comments
 (0)