[Flight] Bound server references should be able to be bound again (#27695)
Wasn't consistent. Probably should use a shared helper maybe.
Sebastian Markbåge committed
Nov 13, 2023 at 22:45 UTC
07cc4a0002d0bbaaf8910404895eeb9c199831c4
4 files changed
+22
-13
packages/react-server-dom-esm/src/ReactFlightESMReferences.js
+7
-4
@@ -47,15 +47,18 @@ export function registerClientReference<T>(
47
const FunctionBind = Function.prototype.bind;
48
// $FlowFixMe[method-unbinding]
49
const ArraySlice = Array.prototype.slice;
50
-function bind(this: ServerReference<any>) {
50
+function bind(this: ServerReference<any>): any {
51
// $FlowFixMe[unsupported-syntax]
52
const newFn = FunctionBind.apply(this, arguments);
53
if (this.$$typeof === SERVER_REFERENCE_TAG) {
54
// $FlowFixMe[method-unbinding]
55
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
+ });
62
}
63
return newFn;
64
}
packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js
+7
-4
@@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
61
const FunctionBind = Function.prototype.bind;
62
// $FlowFixMe[method-unbinding]
63
const ArraySlice = Array.prototype.slice;
64
-function bind(this: ServerReference<any>) {
64
+function bind(this: ServerReference<any>): any {
65
// $FlowFixMe[unsupported-syntax]
66
const newFn = FunctionBind.apply(this, arguments);
67
if (this.$$typeof === SERVER_REFERENCE_TAG) {
68
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
+ });
75
}
76
return newFn;
77
}
packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js
+7
-4
@@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
61
const FunctionBind = Function.prototype.bind;
62
// $FlowFixMe[method-unbinding]
63
const ArraySlice = Array.prototype.slice;
64
-function bind(this: ServerReference<any>) {
64
+function bind(this: ServerReference<any>): any {
65
// $FlowFixMe[unsupported-syntax]
66
const newFn = FunctionBind.apply(this, arguments);
67
if (this.$$typeof === SERVER_REFERENCE_TAG) {
68
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
+ });
75
}
76
return newFn;
77
}
packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js
+1
-1
@@ -1011,7 +1011,7 @@ describe('ReactFlightDOMBrowser', () => {
1011
const ClientRef = clientExports(Client);
1012
1013
const stream = ReactServerDOMServer.renderToReadableStream(
1014
- <ClientRef action={greet.bind(null, 'Hello', 'World')} />,
1014
+ <ClientRef action={greet.bind(null, 'Hello').bind(null, 'World')} />,
1015
webpackMap,
1016
);
1017