From 02ce2c58816f3ca097a420e82c74f94d843b34e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Mon, 13 Nov 2023 22:45:40 -0500 Subject: [PATCH] [Flight] Bound server references should be able to be bound again (#27695) Wasn't consistent. Probably should use a shared helper maybe. --- .../src/ReactFlightESMReferences.js | 11 +++++++---- .../src/ReactFlightTurbopackReferences.js | 11 +++++++---- .../src/ReactFlightWebpackReferences.js | 11 +++++++---- .../src/__tests__/ReactFlightDOMBrowser-test.js | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/react-server-dom-esm/src/ReactFlightESMReferences.js b/packages/react-server-dom-esm/src/ReactFlightESMReferences.js index 57bbdd121cee8..2b051e72f987d 100644 --- a/packages/react-server-dom-esm/src/ReactFlightESMReferences.js +++ b/packages/react-server-dom-esm/src/ReactFlightESMReferences.js @@ -47,15 +47,18 @@ export function registerClientReference( const FunctionBind = Function.prototype.bind; // $FlowFixMe[method-unbinding] const ArraySlice = Array.prototype.slice; -function bind(this: ServerReference) { +function bind(this: ServerReference): any { // $FlowFixMe[unsupported-syntax] const newFn = FunctionBind.apply(this, arguments); if (this.$$typeof === SERVER_REFERENCE_TAG) { // $FlowFixMe[method-unbinding] const args = ArraySlice.call(arguments, 1); - newFn.$$typeof = SERVER_REFERENCE_TAG; - newFn.$$id = this.$$id; - newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args; + return Object.defineProperties((newFn: any), { + $$typeof: {value: SERVER_REFERENCE_TAG}, + $$id: {value: this.$$id}, + $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args}, + bind: {value: bind}, + }); } return newFn; } diff --git a/packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js b/packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js index 9df0e43bd75e1..39b178ed9f561 100644 --- a/packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js +++ b/packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js @@ -61,14 +61,17 @@ function registerClientReferenceImpl( const FunctionBind = Function.prototype.bind; // $FlowFixMe[method-unbinding] const ArraySlice = Array.prototype.slice; -function bind(this: ServerReference) { +function bind(this: ServerReference): any { // $FlowFixMe[unsupported-syntax] const newFn = FunctionBind.apply(this, arguments); if (this.$$typeof === SERVER_REFERENCE_TAG) { const args = ArraySlice.call(arguments, 1); - newFn.$$typeof = SERVER_REFERENCE_TAG; - newFn.$$id = this.$$id; - newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args; + return Object.defineProperties((newFn: any), { + $$typeof: {value: SERVER_REFERENCE_TAG}, + $$id: {value: this.$$id}, + $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args}, + bind: {value: bind}, + }); } return newFn; } diff --git a/packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js b/packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js index 6952dad11134a..059925a3cfa1a 100644 --- a/packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js +++ b/packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js @@ -61,14 +61,17 @@ function registerClientReferenceImpl( const FunctionBind = Function.prototype.bind; // $FlowFixMe[method-unbinding] const ArraySlice = Array.prototype.slice; -function bind(this: ServerReference) { +function bind(this: ServerReference): any { // $FlowFixMe[unsupported-syntax] const newFn = FunctionBind.apply(this, arguments); if (this.$$typeof === SERVER_REFERENCE_TAG) { const args = ArraySlice.call(arguments, 1); - newFn.$$typeof = SERVER_REFERENCE_TAG; - newFn.$$id = this.$$id; - newFn.$$bound = this.$$bound ? this.$$bound.concat(args) : args; + return Object.defineProperties((newFn: any), { + $$typeof: {value: SERVER_REFERENCE_TAG}, + $$id: {value: this.$$id}, + $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args}, + bind: {value: bind}, + }); } return newFn; } diff --git a/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js b/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js index cc4f0b5331b72..7a1fa32cd863d 100644 --- a/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js +++ b/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js @@ -1011,7 +1011,7 @@ describe('ReactFlightDOMBrowser', () => { const ClientRef = clientExports(Client); const stream = ReactServerDOMServer.renderToReadableStream( - , + , webpackMap, );