Skip to content

Commit 6cd04f4

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

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
@@ -1128,8 +1128,9 @@ function createMap(
11281128
if ((model as any).$$consumed === true) {
11291129
throw new Error('Already initialized Map.');
11301130
}
1131-
const map = new Map(model);
1131+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
11321132
(model as any).$$consumed = true;
1133+
const map = new Map(model);
11331134
return map;
11341135
}
11351136

@@ -1140,8 +1141,9 @@ function createSet(response: Response, model: Array<any>): Set<any> {
11401141
if ((model as any).$$consumed === true) {
11411142
throw new Error('Already initialized Set.');
11421143
}
1143-
const set = new Set(model);
1144+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
11441145
(model as any).$$consumed = true;
1146+
const set = new Set(model);
11451147
return set;
11461148
}
11471149

@@ -1152,9 +1154,10 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
11521154
if ((model as any).$$consumed === true) {
11531155
throw new Error('Already initialized Iterator.');
11541156
}
1157+
// This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
1158+
(model as any).$$consumed = true;
11551159
// $FlowFixMe[incompatible-use]: This uses raw Symbols because we're extracting from a native array.
11561160
const iterator = model[Symbol.iterator]();
1157-
(model as any).$$consumed = true;
11581161
return iterator;
11591162
}
11601163

0 commit comments

Comments
 (0)