Skip to content

Commit

Permalink
[Flight] Allow a Server Reference to be registered twice (facebook#28343
Browse files Browse the repository at this point in the history
)

It's possible for the same function instance to appear more than once in
the same graph or even the same file.

Currently this errors on trying to reconfigure the property but it
really doesn't matter which one wins. First or last.

Regardless there will be an entry point generated that can get them.
  • Loading branch information
sebmarkbage authored and AndyPengc12 committed Apr 15, 2024
1 parent 95f4929 commit a104683
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/react-server-dom-esm/src/ReactFlightESMReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export function registerServerReference<T: Function>(
): ServerReference<T> {
return Object.defineProperties((reference: any), {
$$typeof: {value: SERVER_REFERENCE_TAG},
$$id: {value: id + '#' + exportName},
$$bound: {value: null},
bind: {value: bind},
$$id: {value: id + '#' + exportName, configurable: true},
$$bound: {value: null, configurable: true},
bind: {value: bind, configurable: true},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
): ServerReference<T> {
return Object.defineProperties((reference: any), {
$$typeof: {value: SERVER_REFERENCE_TAG},
$$id: {value: exportName === null ? id : id + '#' + exportName},
$$bound: {value: null},
bind: {value: bind},
$$id: {
value: exportName === null ? id : id + '#' + exportName,
configurable: true,
},
$$bound: {value: null, configurable: true},
bind: {value: bind, configurable: true},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
): ServerReference<T> {
return Object.defineProperties((reference: any), {
$$typeof: {value: SERVER_REFERENCE_TAG},
$$id: {value: exportName === null ? id : id + '#' + exportName},
$$bound: {value: null},
bind: {value: bind},
$$id: {
value: exportName === null ? id : id + '#' + exportName,
configurable: true,
},
$$bound: {value: null, configurable: true},
bind: {value: bind, configurable: true},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,58 @@ describe('ReactFlightDOMBrowser', () => {
}
});

it('can use the same function twice as a server action', async () => {
let actionProxy1;
let actionProxy2;

function Client({action1, action2}) {
actionProxy1 = action1;
actionProxy2 = action2;
return 'Click Me';
}

function greet(text) {
return 'Hello ' + text;
}

const ServerModule = serverExports({
greet,
greet2: greet,
});
const ClientRef = clientExports(Client);

const stream = ReactServerDOMServer.renderToReadableStream(
<ClientRef action1={ServerModule.greet} action2={ServerModule.greet2} />,
webpackMap,
);

const response = ReactServerDOMClient.createFromReadableStream(stream, {
async callServer(ref, args) {
const body = await ReactServerDOMClient.encodeReply(args);
return callServer(ref, body);
},
});

function App() {
return use(response);
}

const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<App />);
});
expect(container.innerHTML).toBe('Click Me');
expect(typeof actionProxy1).toBe('function');
expect(actionProxy1).not.toBe(greet);

// TODO: Ideally flight would be encoding this the same.
expect(actionProxy1).not.toBe(actionProxy2);

const result = await actionProxy1('world');
expect(result).toBe('Hello world');
});

it('supports Float hints before the first await in server components in Fiber', async () => {
function Component() {
return <p>hello world</p>;
Expand Down

0 comments on commit a104683

Please sign in to comment.