Skip to content

Commit

Permalink
[Flight] Bound server references should be able to be bound again (fa…
Browse files Browse the repository at this point in the history
…cebook#27695)

Wasn't consistent. Probably should use a shared helper maybe.
  • Loading branch information
sebmarkbage authored and AndyPengc12 committed Apr 15, 2024
1 parent eef28e1 commit 02ce2c5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
11 changes: 7 additions & 4 deletions packages/react-server-dom-esm/src/ReactFlightESMReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ export function registerClientReference<T>(
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;
function bind(this: ServerReference<any>) {
function bind(this: ServerReference<any>): 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;
function bind(this: ServerReference<any>) {
function bind(this: ServerReference<any>): 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ function registerClientReferenceImpl<T>(
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;
function bind(this: ServerReference<any>) {
function bind(this: ServerReference<any>): 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ describe('ReactFlightDOMBrowser', () => {
const ClientRef = clientExports(Client);

const stream = ReactServerDOMServer.renderToReadableStream(
<ClientRef action={greet.bind(null, 'Hello', 'World')} />,
<ClientRef action={greet.bind(null, 'Hello').bind(null, 'World')} />,
webpackMap,
);

Expand Down

0 comments on commit 02ce2c5

Please sign in to comment.