Skip to content

Commit 02ce2c5

Browse files
sebmarkbageAndyPengc12
authored andcommitted
[Flight] Bound server references should be able to be bound again (facebook#27695)
Wasn't consistent. Probably should use a shared helper maybe.
1 parent eef28e1 commit 02ce2c5

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

packages/react-server-dom-esm/src/ReactFlightESMReferences.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ export function registerClientReference<T>(
4747
const FunctionBind = Function.prototype.bind;
4848
// $FlowFixMe[method-unbinding]
4949
const ArraySlice = Array.prototype.slice;
50-
function bind(this: ServerReference<any>) {
50+
function bind(this: ServerReference<any>): any {
5151
// $FlowFixMe[unsupported-syntax]
5252
const newFn = FunctionBind.apply(this, arguments);
5353
if (this.$$typeof === SERVER_REFERENCE_TAG) {
5454
// $FlowFixMe[method-unbinding]
5555
const args = ArraySlice.call(arguments, 1);
56-
newFn.$$typeof = SERVER_REFERENCE_TAG;
57-
newFn.$$id = this.$$id;
58-
newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args;
56+
return Object.defineProperties((newFn: any), {
57+
$$typeof: {value: SERVER_REFERENCE_TAG},
58+
$$id: {value: this.$$id},
59+
$$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
60+
bind: {value: bind},
61+
});
5962
}
6063
return newFn;
6164
}

packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
6161
const FunctionBind = Function.prototype.bind;
6262
// $FlowFixMe[method-unbinding]
6363
const ArraySlice = Array.prototype.slice;
64-
function bind(this: ServerReference<any>) {
64+
function bind(this: ServerReference<any>): any {
6565
// $FlowFixMe[unsupported-syntax]
6666
const newFn = FunctionBind.apply(this, arguments);
6767
if (this.$$typeof === SERVER_REFERENCE_TAG) {
6868
const args = ArraySlice.call(arguments, 1);
69-
newFn.$$typeof = SERVER_REFERENCE_TAG;
70-
newFn.$$id = this.$$id;
71-
newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args;
69+
return Object.defineProperties((newFn: any), {
70+
$$typeof: {value: SERVER_REFERENCE_TAG},
71+
$$id: {value: this.$$id},
72+
$$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
73+
bind: {value: bind},
74+
});
7275
}
7376
return newFn;
7477
}

packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
6161
const FunctionBind = Function.prototype.bind;
6262
// $FlowFixMe[method-unbinding]
6363
const ArraySlice = Array.prototype.slice;
64-
function bind(this: ServerReference<any>) {
64+
function bind(this: ServerReference<any>): any {
6565
// $FlowFixMe[unsupported-syntax]
6666
const newFn = FunctionBind.apply(this, arguments);
6767
if (this.$$typeof === SERVER_REFERENCE_TAG) {
6868
const args = ArraySlice.call(arguments, 1);
69-
newFn.$$typeof = SERVER_REFERENCE_TAG;
70-
newFn.$$id = this.$$id;
71-
newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args;
69+
return Object.defineProperties((newFn: any), {
70+
$$typeof: {value: SERVER_REFERENCE_TAG},
71+
$$id: {value: this.$$id},
72+
$$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
73+
bind: {value: bind},
74+
});
7275
}
7376
return newFn;
7477
}

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ describe('ReactFlightDOMBrowser', () => {
10111011
const ClientRef = clientExports(Client);
10121012

10131013
const stream = ReactServerDOMServer.renderToReadableStream(
1014-
<ClientRef action={greet.bind(null, 'Hello', 'World')} />,
1014+
<ClientRef action={greet.bind(null, 'Hello').bind(null, 'World')} />,
10151015
webpackMap,
10161016
);
10171017

0 commit comments

Comments
 (0)