|
1 | | -import { containerKey, identifierKey, Service } from 'reactant'; |
| 1 | +/* eslint-disable consistent-return */ |
| 2 | +import { containerKey, identifierKey, Service, watch } from 'reactant'; |
2 | 3 | import { proxyClientActionName, proxyExecutorKey } from './constants'; |
3 | 4 | import type { ProxyExec, ProxyExecutor } from './interfaces'; |
4 | 5 | import { PortDetector } from './modules/portDetector'; |
@@ -92,18 +93,46 @@ export const delegate = ((module, key, args, options = {}) => { |
92 | 93 | ) |
93 | 94 | ); |
94 | 95 | } |
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 | + }); |
107 | 136 | } |
108 | 137 | } |
109 | 138 | return (method as Function).apply(target, _args); |
|
0 commit comments