forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Flight] Client and Server Reference Creation into Runtime (facebook#…
…27033) We already did this for Server References on the Client so this brings us parity with that. This gives us some more flexibility with changing the runtime implementation without having to affect the loaders. We can also do more in the runtime such as adding `.bind()` support to Server References. I also moved the CommonJS Proxy creation into the runtime helper from the register so that it can be handled in one place. This lets us remove the forks from Next.js since the loaders can be simplified there to just use these helpers. This PR doesn't change the protocol or shape of the objects. They're still specific to each bundler but ideally we should probably move this to shared helpers that can be used by multiple bundler implementations.
- Loading branch information
1 parent
10dfca5
commit e13059a
Showing
19 changed files
with
455 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/react-server-dom-esm/src/ReactFlightESMReferences.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {ReactClientValue} from 'react-server/src/ReactFlightServer'; | ||
|
||
export type ServerReference<T: Function> = T & { | ||
$$typeof: symbol, | ||
$$id: string, | ||
$$bound: null | Array<ReactClientValue>, | ||
}; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
export type ClientReference<T> = { | ||
$$typeof: symbol, | ||
$$id: string, | ||
}; | ||
|
||
const CLIENT_REFERENCE_TAG = Symbol.for('react.client.reference'); | ||
const SERVER_REFERENCE_TAG = Symbol.for('react.server.reference'); | ||
|
||
export function isClientReference(reference: Object): boolean { | ||
return reference.$$typeof === CLIENT_REFERENCE_TAG; | ||
} | ||
|
||
export function isServerReference(reference: Object): boolean { | ||
return reference.$$typeof === SERVER_REFERENCE_TAG; | ||
} | ||
|
||
export function registerClientReference<T>( | ||
proxyImplementation: any, | ||
id: string, | ||
exportName: string, | ||
): ClientReference<T> { | ||
return Object.defineProperties(proxyImplementation, { | ||
$$typeof: {value: CLIENT_REFERENCE_TAG}, | ||
$$id: {value: id + '#' + exportName}, | ||
}); | ||
} | ||
|
||
// $FlowFixMe[method-unbinding] | ||
const FunctionBind = Function.prototype.bind; | ||
// $FlowFixMe[method-unbinding] | ||
const ArraySlice = Array.prototype.slice; | ||
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 newFn; | ||
} | ||
|
||
export function registerServerReference<T>( | ||
reference: ServerReference<T>, | ||
id: string, | ||
exportName: string, | ||
): ServerReference<T> { | ||
return Object.defineProperties((reference: any), { | ||
$$typeof: {value: SERVER_REFERENCE_TAG}, | ||
$$id: {value: id + '#' + exportName}, | ||
$$bound: {value: null}, | ||
bind: {value: bind}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.