Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Preserve location of bound server actions #30778

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions packages/react-server-dom-esm/src/ReactFlightESMReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,38 @@ function bind(this: ServerReference<any>): any {
// $FlowFixMe[unsupported-syntax]
const newFn = FunctionBind.apply(this, arguments);
if (this.$$typeof === SERVER_REFERENCE_TAG) {
// $FlowFixMe[method-unbinding]
if (__DEV__) {
const thisBind = arguments[0];
if (thisBind != null) {
console.error(
'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
);
}
}
const args = ArraySlice.call(arguments, 1);
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},
});
const $$typeof = {value: SERVER_REFERENCE_TAG};
const $$id = {value: this.$$id};
const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
return Object.defineProperties(
(newFn: any),
__DEV__
? {
$$typeof,
$$id,
$$bound,
$$location: {
value: this.$$location,
configurable: true,
},
bind: {value: bind, configurable: true},
}
: {
$$typeof,
$$id,
$$bound,
bind: {value: bind, configurable: true},
},
);
}
return newFn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,38 @@ function bind(this: ServerReference<any>): any {
// $FlowFixMe[unsupported-syntax]
const newFn = FunctionBind.apply(this, arguments);
if (this.$$typeof === SERVER_REFERENCE_TAG) {
if (__DEV__) {
const thisBind = arguments[0];
if (thisBind != null) {
console.error(
'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
);
}
}
const args = ArraySlice.call(arguments, 1);
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},
});
const $$typeof = {value: SERVER_REFERENCE_TAG};
const $$id = {value: this.$$id};
const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
return Object.defineProperties(
(newFn: any),
__DEV__
? {
$$typeof,
$$id,
$$bound,
$$location: {
value: this.$$location,
configurable: true,
},
bind: {value: bind, configurable: true},
}
: {
$$typeof,
$$id,
$$bound,
bind: {value: bind, configurable: true},
},
);
}
return newFn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,29 @@ function bind(this: ServerReference<any>): any {
}
}
const args = ArraySlice.call(arguments, 1);
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},
});
const $$typeof = {value: SERVER_REFERENCE_TAG};
const $$id = {value: this.$$id};
const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
return Object.defineProperties(
(newFn: any),
__DEV__
? {
$$typeof,
$$id,
$$bound,
$$location: {
value: this.$$location,
configurable: true,
},
bind: {value: bind, configurable: true},
}
: {
$$typeof,
$$id,
$$bound,
bind: {value: bind, configurable: true},
},
);
}
return newFn;
}
Expand Down
Loading