Skip to content

Commit 689070d

Browse files
authored
fix(reactant-share): fix delegate action sequence issue (#142)
1 parent c464a0f commit 689070d

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

packages/reactant-share/src/delegate.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { containerKey, identifierKey, Service } from 'reactant';
1+
/* eslint-disable consistent-return */
2+
import { containerKey, identifierKey, Service, watch } from 'reactant';
23
import { proxyClientActionName, proxyExecutorKey } from './constants';
34
import type { ProxyExec, ProxyExecutor } from './interfaces';
45
import { PortDetector } from './modules/portDetector';
@@ -92,18 +93,46 @@ export const delegate = ((module, key, args, options = {}) => {
9293
)
9394
);
9495
}
95-
return portDetector.transports.client.emit(
96-
{
97-
...options,
98-
name: proxyClientActionName,
99-
},
100-
{
101-
module: target[identifierKey]!,
102-
method: key,
103-
args: _args,
104-
hook: options._extra?.serverHook,
105-
}
106-
);
96+
return portDetector.transports.client
97+
.emit(
98+
{
99+
...options,
100+
name: proxyClientActionName,
101+
},
102+
{
103+
module: target[identifierKey]!,
104+
method: key,
105+
args: _args,
106+
hook: options._extra?.serverHook,
107+
}
108+
)
109+
.then((response) => {
110+
// If the response is undefined, it means that the method is not executed.
111+
if (!response) return;
112+
const [sequence, result] = response;
113+
if (portDetector.lastAction.sequence >= sequence) {
114+
return result;
115+
}
116+
if (__DEV__) {
117+
console.warn(
118+
`The sequence of the action is not consistent.`,
119+
sequence,
120+
portDetector.lastAction.sequence
121+
);
122+
}
123+
return new Promise((resolve) => {
124+
const unwatch = watch(
125+
portDetector,
126+
() => portDetector.lastAction.sequence,
127+
() => {
128+
if (portDetector.lastAction.sequence >= sequence) {
129+
unwatch();
130+
resolve(result);
131+
}
132+
}
133+
);
134+
});
135+
});
107136
}
108137
}
109138
return (method as Function).apply(target, _args);

packages/reactant-share/src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface ProxyExecParams {
155155
}
156156

157157
export type ClientEvents = {
158-
[proxyClientActionName](options: ProxyExecParams): Promise<any>;
158+
[proxyClientActionName](options: ProxyExecParams): Promise<[number, any]>;
159159
[preloadedStateActionName](): Promise<Record<string, any>>;
160160
[loadFullStateActionName](
161161
sequence: number

packages/reactant-share/src/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ export const handleServer = ({
5757
if (options.hook) {
5858
const hook = portDetector.serverHooks[options.hook];
5959
if (typeof hook === 'function') {
60-
return hook(options);
60+
const result = await hook(options);
61+
const { sequence } = portDetector.lastAction;
62+
return [sequence, result];
6163
}
6264
}
6365
const result = await applyMethod(app, options);
64-
return result;
66+
const { sequence } = portDetector.lastAction;
67+
return [sequence, result];
6568
})
6669
);
6770
disposeListeners.push(() => transport.dispose());

0 commit comments

Comments
 (0)