@actualwave/deferred-data-access 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +400 -27
- package/SKILL.md +106 -0
- package/command/index.d.ts.map +1 -0
- package/command/index.es.js +98 -0
- package/command/index.es.js.map +1 -0
- package/command/index.js +102 -4
- package/command/index.js.map +1 -1
- package/command/package.json +4 -1
- package/command/src/command-chain.d.ts +8 -1
- package/command/src/command-chain.d.ts.map +1 -0
- package/command/src/command-chain.js +9 -0
- package/command/src/command-chain.js.map +1 -1
- package/command/src/command-handler.d.ts.map +1 -0
- package/command/src/command.d.ts +2 -2
- package/command/src/command.d.ts.map +1 -0
- package/command/src/command.js +5 -6
- package/command/src/command.js.map +1 -1
- package/core/core.d.ts.map +1 -0
- package/core/core.js +23 -25
- package/core/core.js.map +1 -1
- package/core/index.d.ts.map +1 -0
- package/deferred-data-access.umd.js +2 -0
- package/deferred-data-access.umd.js.map +1 -0
- package/index.d.ts.map +1 -0
- package/{dist/deferred-data-access.js → index.es.js} +199 -138
- package/index.es.js.map +1 -0
- package/index.js +627 -2
- package/index.js.map +1 -1
- package/interface/index.d.ts.map +1 -0
- package/interface/index.es.js +380 -0
- package/interface/index.es.js.map +1 -0
- package/interface/index.js +396 -7
- package/interface/index.js.map +1 -1
- package/interface/package.json +4 -1
- package/interface/src/handshake.d.ts.map +1 -0
- package/interface/src/handshake.js +6 -4
- package/interface/src/handshake.js.map +1 -1
- package/interface/src/helpers.d.ts.map +1 -0
- package/interface/src/intialize.d.ts +5 -2
- package/interface/src/intialize.d.ts.map +1 -0
- package/interface/src/intialize.js +37 -57
- package/interface/src/intialize.js.map +1 -1
- package/interface/src/request.d.ts +3 -2
- package/interface/src/request.d.ts.map +1 -0
- package/interface/src/request.js +39 -29
- package/interface/src/request.js.map +1 -1
- package/interface/src/types.d.ts.map +1 -0
- package/interface/src/utils.d.ts +2 -2
- package/interface/src/utils.d.ts.map +1 -0
- package/interface/src/utils.js +44 -29
- package/interface/src/utils.js.map +1 -1
- package/package.json +7 -6
- package/proxy/index.d.ts.map +1 -0
- package/proxy/index.es.js +144 -0
- package/proxy/index.es.js.map +1 -0
- package/proxy/index.js +155 -5
- package/proxy/index.js.map +1 -1
- package/proxy/package.json +4 -1
- package/proxy/src/command.d.ts.map +1 -0
- package/proxy/src/proxy.d.ts +2 -2
- package/proxy/src/proxy.d.ts.map +1 -0
- package/proxy/src/proxy.js +13 -3
- package/proxy/src/proxy.js.map +1 -1
- package/proxy/src/traps.d.ts +1 -1
- package/proxy/src/traps.d.ts.map +1 -0
- package/proxy/src/traps.js +4 -14
- package/proxy/src/traps.js.map +1 -1
- package/proxy/src/types.d.ts.map +1 -0
- package/proxy/src/utils.d.ts +6 -0
- package/proxy/src/utils.d.ts.map +1 -0
- package/proxy/src/utils.js +11 -5
- package/proxy/src/utils.js.map +1 -1
- package/record/index.d.ts.map +1 -0
- package/record/index.es.js +26 -0
- package/record/index.es.js.map +1 -0
- package/record/index.js +31 -2
- package/record/index.js.map +1 -1
- package/record/package.json +4 -1
- package/record/record.d.ts +2 -2
- package/record/record.d.ts.map +1 -0
- package/record/record.js +9 -3
- package/record/record.js.map +1 -1
- package/resource/index.d.ts +1 -0
- package/resource/index.d.ts.map +1 -0
- package/resource/index.es.js +191 -0
- package/resource/index.es.js.map +1 -0
- package/resource/index.js +206 -6
- package/resource/index.js.map +1 -1
- package/resource/package.json +4 -1
- package/resource/src/default-resource-pool.d.ts +1 -0
- package/resource/src/default-resource-pool.d.ts.map +1 -0
- package/resource/src/default-resource-pool.js +8 -5
- package/resource/src/default-resource-pool.js.map +1 -1
- package/resource/src/finalization-registry.d.ts +13 -0
- package/resource/src/finalization-registry.d.ts.map +1 -0
- package/resource/src/finalization-registry.js +18 -0
- package/resource/src/finalization-registry.js.map +1 -0
- package/resource/src/resource-pool-registry.d.ts +1 -0
- package/resource/src/resource-pool-registry.d.ts.map +1 -0
- package/resource/src/resource-pool-registry.js +10 -8
- package/resource/src/resource-pool-registry.js.map +1 -1
- package/resource/src/resource-pool.d.ts +8 -1
- package/resource/src/resource-pool.d.ts.map +1 -0
- package/resource/src/resource-pool.js +29 -17
- package/resource/src/resource-pool.js.map +1 -1
- package/resource/src/resource.d.ts +1 -1
- package/resource/src/resource.d.ts.map +1 -0
- package/resource/src/resource.js +3 -2
- package/resource/src/resource.js.map +1 -1
- package/resource/src/utils.d.ts +1 -1
- package/resource/src/utils.d.ts.map +1 -0
- package/resource/src/utils.js +9 -1
- package/resource/src/utils.js.map +1 -1
- package/utils/index.d.ts.map +1 -0
- package/utils/index.es.js +48 -0
- package/utils/index.es.js.map +1 -0
- package/utils/index.js +54 -3
- package/utils/index.js.map +1 -1
- package/utils/package.json +4 -1
- package/utils/src/types.d.ts +3 -3
- package/utils/src/types.d.ts.map +1 -0
- package/utils/src/utils.d.ts +18 -2
- package/utils/src/utils.d.ts.map +1 -0
- package/utils/src/utils.js +28 -4
- package/utils/src/utils.js.map +1 -1
- package/dist/deferred-data-access.js.map +0 -1
- package/dist/deferred-data-access.umd.js +0 -2
- package/dist/deferred-data-access.umd.js.map +0 -1
package/interface/index.js
CHANGED
|
@@ -1,7 +1,396 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@actualwave/deferred-data-access/utils');
|
|
4
|
+
var proxy = require('@actualwave/deferred-data-access/proxy');
|
|
5
|
+
var resource = require('@actualwave/deferred-data-access/resource');
|
|
6
|
+
var deferredDataAccess = require('@actualwave/deferred-data-access');
|
|
7
|
+
|
|
8
|
+
let pool;
|
|
9
|
+
const getPool = () => {
|
|
10
|
+
if (!pool) {
|
|
11
|
+
pool = resource.getRegistry().createPool();
|
|
12
|
+
}
|
|
13
|
+
return pool;
|
|
14
|
+
};
|
|
15
|
+
const extractResourceFrom = (value) => {
|
|
16
|
+
if (!resource.isResourceObject(value)) {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
const { poolId, id } = value;
|
|
20
|
+
const resourcePool = resource.getRegistry().get(poolId);
|
|
21
|
+
if (!resourcePool) {
|
|
22
|
+
throw new Error(`Resource Pool "${poolId}" does not exist.`);
|
|
23
|
+
}
|
|
24
|
+
const target = resourcePool.getById(id);
|
|
25
|
+
if (target === undefined) {
|
|
26
|
+
throw new Error(`Resource "${id}" does not exist, pool "${poolId}".`);
|
|
27
|
+
}
|
|
28
|
+
return target;
|
|
29
|
+
};
|
|
30
|
+
const commandDispatch = {
|
|
31
|
+
[proxy.ProxyCommand.GET]: (target, name) => target[name],
|
|
32
|
+
[proxy.ProxyCommand.SET]: (target, name, value) => {
|
|
33
|
+
target[name] = extractResourceFrom(value);
|
|
34
|
+
return target[name];
|
|
35
|
+
},
|
|
36
|
+
[proxy.ProxyCommand.DELETE_PROPERTY]: (target, name) => delete target[name],
|
|
37
|
+
[proxy.ProxyCommand.APPLY]: (target, _name, value) => {
|
|
38
|
+
const [exeContext, args] = value;
|
|
39
|
+
return target.apply(extractResourceFrom(exeContext), args.map(extractResourceFrom));
|
|
40
|
+
},
|
|
41
|
+
[proxy.ProxyCommand.METHOD_CALL]: (target, name, value) => target[name](...value.map(extractResourceFrom)),
|
|
42
|
+
};
|
|
43
|
+
const applyRemoteRequest = ({ command, command: { type, value }, context, }) => {
|
|
44
|
+
const target = extractResourceFrom(context);
|
|
45
|
+
const name = command.name;
|
|
46
|
+
if (type !== proxy.ProxyCommand.APPLY && !target) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
// Fix typo: "excute" → "execute"
|
|
49
|
+
`Cannot execute command ${type}/${String(name)} on non-existent target (${target}).`);
|
|
50
|
+
}
|
|
51
|
+
const dispatch = commandDispatch[type];
|
|
52
|
+
if (!dispatch) {
|
|
53
|
+
throw new Error(`Unknown command type "${type}" cannot be applied remotely.`);
|
|
54
|
+
}
|
|
55
|
+
const result = dispatch(target, name, value);
|
|
56
|
+
// For SET and DELETE, return the result directly without wrapping
|
|
57
|
+
if (type === proxy.ProxyCommand.SET || type === proxy.ProxyCommand.DELETE_PROPERTY) {
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
if (result !== null && result !== undefined && typeof result === 'function') {
|
|
61
|
+
const resource = getPool().set(result);
|
|
62
|
+
return resource.toObject();
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
exports.InterfaceType = void 0;
|
|
68
|
+
(function (InterfaceType) {
|
|
69
|
+
InterfaceType["HOST"] = "host";
|
|
70
|
+
InterfaceType["GUEST"] = "guest";
|
|
71
|
+
})(exports.InterfaceType || (exports.InterfaceType = {}));
|
|
72
|
+
exports.MessageType = void 0;
|
|
73
|
+
(function (MessageType) {
|
|
74
|
+
MessageType["REQUEST"] = "request";
|
|
75
|
+
MessageType["RESPONSE"] = "response";
|
|
76
|
+
})(exports.MessageType || (exports.MessageType = {}));
|
|
77
|
+
const generateId = utils.createUIDGenerator('wi');
|
|
78
|
+
const generateMessageId = utils.createUIDGenerator('m');
|
|
79
|
+
const lookupForResource = async (value) => {
|
|
80
|
+
if (value === null || value === undefined) {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
const list = [];
|
|
85
|
+
for (const item of value) {
|
|
86
|
+
list.push(await lookupForResource(item));
|
|
87
|
+
}
|
|
88
|
+
return list;
|
|
89
|
+
}
|
|
90
|
+
if (proxy.isWrappedWithProxy(value)) {
|
|
91
|
+
const target = await proxy.unwrapProxy(value);
|
|
92
|
+
return lookupForResource(target);
|
|
93
|
+
}
|
|
94
|
+
if (typeof value === 'function') {
|
|
95
|
+
const resource = getPool().set(value);
|
|
96
|
+
return resource.toObject();
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
};
|
|
100
|
+
/*
|
|
101
|
+
Message signature
|
|
102
|
+
|
|
103
|
+
{
|
|
104
|
+
id, // every sent message has id, request and its response have same id
|
|
105
|
+
type, // message type -- "request" or "response"
|
|
106
|
+
source, // sender worker interface id
|
|
107
|
+
target, // receiver worker interface id
|
|
108
|
+
command, // request proxy command
|
|
109
|
+
context, // request command context
|
|
110
|
+
value, // response value
|
|
111
|
+
error, // response error
|
|
112
|
+
}
|
|
113
|
+
*/
|
|
114
|
+
const createRequestMessage = (source, target) => async (commandChain, context, id = generateMessageId()) => {
|
|
115
|
+
const command = commandChain.toObject();
|
|
116
|
+
const contextTarget = await context;
|
|
117
|
+
// apply function to owner object
|
|
118
|
+
if (command.type === proxy.ProxyCommand.APPLY) {
|
|
119
|
+
let exeContext;
|
|
120
|
+
if (commandChain.prev) {
|
|
121
|
+
exeContext = await commandChain.prev.context;
|
|
122
|
+
}
|
|
123
|
+
// prepare arguments for Function.apply()
|
|
124
|
+
command.value = [exeContext, await lookupForResource(command.value)];
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
command.value = await lookupForResource(command.value);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
id,
|
|
131
|
+
type: exports.MessageType.REQUEST,
|
|
132
|
+
source,
|
|
133
|
+
target,
|
|
134
|
+
command,
|
|
135
|
+
context: contextTarget,
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
const createResponseMessage = (source) =>
|
|
139
|
+
// with "undefined" value, "error" key will not be packed into JSON, less bytes sent
|
|
140
|
+
({ id, source: target }, value, error) => ({
|
|
141
|
+
id,
|
|
142
|
+
type: exports.MessageType.RESPONSE,
|
|
143
|
+
source,
|
|
144
|
+
target,
|
|
145
|
+
value,
|
|
146
|
+
error,
|
|
147
|
+
});
|
|
148
|
+
const createIsHandshakeMessage = (id = '') => (data) => {
|
|
149
|
+
if (data == null || typeof data !== 'object')
|
|
150
|
+
return false;
|
|
151
|
+
const dataId = data.id;
|
|
152
|
+
if (typeof dataId !== 'string')
|
|
153
|
+
return false;
|
|
154
|
+
return id ? id === dataId : /^wi/.test(dataId);
|
|
155
|
+
};
|
|
156
|
+
const createIsMessage = (target) => (data) => {
|
|
157
|
+
if (data == null || typeof data !== 'object')
|
|
158
|
+
return false;
|
|
159
|
+
const { id, target: dataTarget } = data;
|
|
160
|
+
return typeof id === 'string' && target === dataTarget;
|
|
161
|
+
};
|
|
162
|
+
const resolveOrTimeout = ({ handler, timeout, timeoutError = `Async operation didn't complete in ${timeout}ms.`, onTimeout, }) => {
|
|
163
|
+
const promise = typeof handler === 'function' ? new Promise(handler) : handler;
|
|
164
|
+
if (!timeout) {
|
|
165
|
+
return promise;
|
|
166
|
+
}
|
|
167
|
+
let timeoutHandle;
|
|
168
|
+
const timeoutPromise = new Promise((_, rej) => {
|
|
169
|
+
timeoutHandle = setTimeout(() => {
|
|
170
|
+
rej(timeoutError);
|
|
171
|
+
// Call onTimeout AFTER rejecting so the race is already settled.
|
|
172
|
+
// This prevents the double-rejection issue in initialize's onTimeout handler.
|
|
173
|
+
onTimeout && onTimeout(timeoutError);
|
|
174
|
+
}, timeout);
|
|
175
|
+
});
|
|
176
|
+
// Clear the timeout if the main promise wins the race
|
|
177
|
+
return Promise.race([
|
|
178
|
+
promise.then((v) => {
|
|
179
|
+
clearTimeout(timeoutHandle);
|
|
180
|
+
return v;
|
|
181
|
+
}, (e) => {
|
|
182
|
+
clearTimeout(timeoutHandle);
|
|
183
|
+
throw e;
|
|
184
|
+
}),
|
|
185
|
+
timeoutPromise,
|
|
186
|
+
]);
|
|
187
|
+
};
|
|
188
|
+
const getMessageEventData = (event) => event != null && typeof event === 'object' && 'data' in event ? event.data : event;
|
|
189
|
+
|
|
190
|
+
// leader (HOST): waits for guest to initiate, then replies
|
|
191
|
+
const handshakeHost = ({ id, root, isMessage, subscribe, unsubscribe, sendMessage, preprocessResponse = (data) => data, }) => (resolve) => {
|
|
192
|
+
const handshakeHandler = (event) => {
|
|
193
|
+
const data = getMessageEventData(preprocessResponse(event));
|
|
194
|
+
if (!isMessage(data)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
unsubscribe(handshakeHandler);
|
|
198
|
+
sendMessage({ id, root });
|
|
199
|
+
resolve(data);
|
|
200
|
+
};
|
|
201
|
+
subscribe(handshakeHandler);
|
|
202
|
+
};
|
|
203
|
+
// follower (GUEST): initiates handshake by sending its identity, waits for host reply
|
|
204
|
+
const handshakeGuest = ({ id, root, isMessage, subscribe, unsubscribe, sendMessage, handshakeInterval, preprocessResponse = (data) => data, }) => (resolve) => {
|
|
205
|
+
// Use ReturnType to avoid the TS2322 Timer/number mismatch across environments
|
|
206
|
+
let intervalId;
|
|
207
|
+
const handshakeHandler = (event) => {
|
|
208
|
+
const data = getMessageEventData(preprocessResponse(event));
|
|
209
|
+
if (!isMessage(data)) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
unsubscribe(handshakeHandler);
|
|
213
|
+
if (intervalId !== undefined) {
|
|
214
|
+
clearInterval(intervalId);
|
|
215
|
+
}
|
|
216
|
+
resolve(data);
|
|
217
|
+
};
|
|
218
|
+
subscribe(handshakeHandler);
|
|
219
|
+
const intervalFn = () => sendMessage({ id, root });
|
|
220
|
+
if (handshakeInterval) {
|
|
221
|
+
intervalId = setInterval(intervalFn, handshakeInterval);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
intervalFn();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const handshake = ({ type, remoteId, handshakeTimeout, ...params }) => {
|
|
228
|
+
const data = {
|
|
229
|
+
...params,
|
|
230
|
+
isMessage: createIsHandshakeMessage(remoteId),
|
|
231
|
+
};
|
|
232
|
+
const handler = type === exports.InterfaceType.HOST
|
|
233
|
+
? handshakeHost(data)
|
|
234
|
+
: handshakeGuest(data);
|
|
235
|
+
return resolveOrTimeout({
|
|
236
|
+
handler,
|
|
237
|
+
timeout: handshakeTimeout,
|
|
238
|
+
timeoutError: `Handshake sequence could not complete in ${handshakeTimeout}ms.`,
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const EVENT_TYPE = 'message';
|
|
243
|
+
const findEventEmitter = (worker) => {
|
|
244
|
+
if (worker) {
|
|
245
|
+
return worker;
|
|
246
|
+
}
|
|
247
|
+
if (typeof self === 'object') {
|
|
248
|
+
return self;
|
|
249
|
+
}
|
|
250
|
+
throw new Error('EventEmitter is not defined, please provide EventEmitter interface via "worker" or "eventEmitter" property.');
|
|
251
|
+
};
|
|
252
|
+
const findMessagePort = (worker) => {
|
|
253
|
+
if (worker) {
|
|
254
|
+
return worker;
|
|
255
|
+
}
|
|
256
|
+
if (typeof self === 'object') {
|
|
257
|
+
return self;
|
|
258
|
+
}
|
|
259
|
+
throw new Error('MessagePort is not defined, please provide MessagePort interface via "worker" or "messagePort" property.');
|
|
260
|
+
};
|
|
261
|
+
const createSubscriberFns = (instance, eventType = EVENT_TYPE) => {
|
|
262
|
+
if (instance.addEventListener) {
|
|
263
|
+
return {
|
|
264
|
+
subscribe: (listener) => instance.addEventListener(eventType, listener),
|
|
265
|
+
unsubscribe: (listener) => instance.removeEventListener(eventType, listener),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
if (instance.addListener) {
|
|
269
|
+
return {
|
|
270
|
+
subscribe: (listener) => instance.addListener(eventType, listener),
|
|
271
|
+
unsubscribe: (listener) => instance.removeListener(eventType, listener),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if (instance.on) {
|
|
275
|
+
return {
|
|
276
|
+
subscribe: (listener) => instance.on(eventType, listener),
|
|
277
|
+
unsubscribe: (listener) => instance.off(eventType, listener),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
throw new Error('Target does not implement EventEmitter insterface, ' +
|
|
281
|
+
'it must expose "addEventListener"/"removeEventListener", ' +
|
|
282
|
+
'"addListener"/"removeListener" or ' +
|
|
283
|
+
'"on"/"off" method pair.');
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
287
|
+
const id = initId || generateId();
|
|
288
|
+
const root = apiRoot
|
|
289
|
+
? getPool().set(apiRoot).toObject()
|
|
290
|
+
: undefined;
|
|
291
|
+
const { subscribe, unsubscribe, sendMessage, preprocessResponse = (data) => data, } = params;
|
|
292
|
+
const { id: remoteId, root: remoteRoot } = await handshake({
|
|
293
|
+
id,
|
|
294
|
+
root,
|
|
295
|
+
...params,
|
|
296
|
+
});
|
|
297
|
+
// Tracks in-flight requests: messageId → { resolve, reject }
|
|
298
|
+
const pendingRequests = new Map();
|
|
299
|
+
const isMessage = createIsMessage(id);
|
|
300
|
+
const createRequest = createRequestMessage(id, remoteId);
|
|
301
|
+
const createResponse = createResponseMessage(id);
|
|
302
|
+
const messageHandler = async (event) => {
|
|
303
|
+
const data = getMessageEventData(preprocessResponse(event));
|
|
304
|
+
if (!isMessage(data)) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
switch (data.type) {
|
|
308
|
+
case exports.MessageType.REQUEST: {
|
|
309
|
+
const request = data;
|
|
310
|
+
try {
|
|
311
|
+
const value = await applyRemoteRequest(request);
|
|
312
|
+
sendMessage(createResponse(request, value));
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
sendMessage(createResponse(request, undefined, { message: error.message }));
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case exports.MessageType.RESPONSE: {
|
|
320
|
+
const { id, value, error } = data;
|
|
321
|
+
const pending = pendingRequests.get(id);
|
|
322
|
+
if (!pending) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
pendingRequests.delete(id);
|
|
326
|
+
// Settle exactly once — delete before calling to prevent double-settle
|
|
327
|
+
if (error) {
|
|
328
|
+
pending.reject(error);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
pending.resolve(value);
|
|
332
|
+
}
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
subscribe(messageHandler);
|
|
338
|
+
const stop = () => unsubscribe(messageHandler);
|
|
339
|
+
if (!remoteRoot) {
|
|
340
|
+
return { stop, pool: getPool() };
|
|
341
|
+
}
|
|
342
|
+
const wrap = deferredDataAccess.handle(async (command, context, wrap) => {
|
|
343
|
+
const { responseTimeout } = params;
|
|
344
|
+
const msgId = generateMessageId();
|
|
345
|
+
const timeoutError = `Could not receive command ${command.type}/${String(command.name)} response in ${responseTimeout}ms.`;
|
|
346
|
+
const resultPromise = resolveOrTimeout({
|
|
347
|
+
handler: async (resolve, reject) => {
|
|
348
|
+
try {
|
|
349
|
+
const request = await createRequest(command, context, msgId);
|
|
350
|
+
// Register before sending to eliminate the race between send and response arrival
|
|
351
|
+
pendingRequests.set(msgId, { resolve, reject });
|
|
352
|
+
sendMessage(request);
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
reject(error);
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
timeout: responseTimeout || 0,
|
|
359
|
+
timeoutError,
|
|
360
|
+
onTimeout: () => {
|
|
361
|
+
// resolveOrTimeout already rejected the race promise.
|
|
362
|
+
// We only need to clean up the pending entry here.
|
|
363
|
+
pendingRequests.delete(msgId);
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
const result = await resultPromise;
|
|
367
|
+
if (resource.isResourceObject(result)) {
|
|
368
|
+
return wrap(resultPromise, command);
|
|
369
|
+
}
|
|
370
|
+
return resultPromise;
|
|
371
|
+
}, false);
|
|
372
|
+
return {
|
|
373
|
+
stop,
|
|
374
|
+
wrap,
|
|
375
|
+
pendingRequests,
|
|
376
|
+
pool: getPool(),
|
|
377
|
+
root: remoteRoot ? wrap(remoteRoot) : null,
|
|
378
|
+
};
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
exports.applyRemoteRequest = applyRemoteRequest;
|
|
382
|
+
exports.createIsHandshakeMessage = createIsHandshakeMessage;
|
|
383
|
+
exports.createIsMessage = createIsMessage;
|
|
384
|
+
exports.createRequestMessage = createRequestMessage;
|
|
385
|
+
exports.createResponseMessage = createResponseMessage;
|
|
386
|
+
exports.createSubscriberFns = createSubscriberFns;
|
|
387
|
+
exports.findEventEmitter = findEventEmitter;
|
|
388
|
+
exports.findMessagePort = findMessagePort;
|
|
389
|
+
exports.generateId = generateId;
|
|
390
|
+
exports.generateMessageId = generateMessageId;
|
|
391
|
+
exports.getMessageEventData = getMessageEventData;
|
|
392
|
+
exports.getPool = getPool;
|
|
393
|
+
exports.handshake = handshake;
|
|
394
|
+
exports.initialize = initialize;
|
|
395
|
+
exports.resolveOrTimeout = resolveOrTimeout;
|
|
396
|
+
//# sourceMappingURL=index.js.map
|
package/interface/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../packages/deferred-data-access/interface/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../../packages/deferred-data-access/interface/src/request.ts","../../../../../../packages/deferred-data-access/interface/src/utils.ts","../../../../../../packages/deferred-data-access/interface/src/handshake.ts","../../../../../../packages/deferred-data-access/interface/src/helpers.ts","../../../../../../packages/deferred-data-access/interface/src/intialize.ts"],"sourcesContent":[null,null,null,null,null],"names":["getRegistry","isResourceObject","ProxyCommand","InterfaceType","MessageType","createUIDGenerator","isWrappedWithProxy","unwrapProxy","handle"],"mappings":";;;;;;;AAWA,IAAI,IAAkB;AAEf,MAAM,OAAO,GAAG,MAAK;IAC1B,IAAI,CAAC,IAAI,EAAE;AACT,QAAA,IAAI,GAAGA,oBAAW,EAAE,CAAC,UAAU,EAAE;IACnC;AAEA,IAAA,OAAO,IAAI;AACb;AAEA,MAAM,mBAAmB,GAAG,C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AAC,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;oBAC/C,WAAW,CAAC,OAAO,CAAC;gBACtB;gBAAE,OAAO,KAAK,EAAE;oBACd,MAAM,CAAC,KAAK,CAAC;gBACf;YACF,CAAC;YACD,OAAO,EAAE,eAAe,IAAI,CAAC;YAC7B,YAAY;YACZ,SAAS,EAAE,MAAK;;;AAGd,gBAAA,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC;YAC/B,CAAC;AACF,SAAA,CAAC;AAEF,QAAA,MAAM,MAAM,GAAG,MAAM,aAAa;AAElC,QAAA,IAAIP,yBAAgB,CAAC,MAAM,CAAC,EAAE;AAC5B,YAAA,OAAO,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC;QACrC;AAEA,QAAA,OAAO,aAAa;IACtB,CAAC,EACD,KAAK,CACN;IAED,OAAO;QACL,IAAI;QACJ,IAAI;QACJ,eAAe;QACf,IAAI,EAAE,OAAO,EAAE;AACf,QAAA,IAAI,EAAE,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI;KAC3C;AACH;;;;;;;;;;;;;;;;;;"}
|
package/interface/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.d.ts","sourceRoot":"","sources":["../../../../packages/deferred-data-access/interface/src/handshake.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,iBAAiB,EAElB,MAAM,SAAS,CAAC;AA8EjB,eAAO,MAAM,SAAS,GAAI,iDAKvB,aAAa,KAAG,OAAO,CAAC,iBAAiB,CAgB3C,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createIsHandshakeMessage, getMessageEventData, InterfaceType, resolveOrTimeout, } from './utils';
|
|
2
|
-
// leader
|
|
2
|
+
// leader (HOST): waits for guest to initiate, then replies
|
|
3
3
|
const handshakeHost = ({ id, root, isMessage, subscribe, unsubscribe, sendMessage, preprocessResponse = (data) => data, }) => (resolve) => {
|
|
4
4
|
const handshakeHandler = (event) => {
|
|
5
5
|
const data = getMessageEventData(preprocessResponse(event));
|
|
@@ -12,8 +12,9 @@ const handshakeHost = ({ id, root, isMessage, subscribe, unsubscribe, sendMessag
|
|
|
12
12
|
};
|
|
13
13
|
subscribe(handshakeHandler);
|
|
14
14
|
};
|
|
15
|
-
// follower
|
|
15
|
+
// follower (GUEST): initiates handshake by sending its identity, waits for host reply
|
|
16
16
|
const handshakeGuest = ({ id, root, isMessage, subscribe, unsubscribe, sendMessage, handshakeInterval, preprocessResponse = (data) => data, }) => (resolve) => {
|
|
17
|
+
// Use ReturnType to avoid the TS2322 Timer/number mismatch across environments
|
|
17
18
|
let intervalId;
|
|
18
19
|
const handshakeHandler = (event) => {
|
|
19
20
|
const data = getMessageEventData(preprocessResponse(event));
|
|
@@ -21,13 +22,14 @@ const handshakeGuest = ({ id, root, isMessage, subscribe, unsubscribe, sendMessa
|
|
|
21
22
|
return;
|
|
22
23
|
}
|
|
23
24
|
unsubscribe(handshakeHandler);
|
|
24
|
-
|
|
25
|
+
if (intervalId !== undefined) {
|
|
26
|
+
clearInterval(intervalId);
|
|
27
|
+
}
|
|
25
28
|
resolve(data);
|
|
26
29
|
};
|
|
27
30
|
subscribe(handshakeHandler);
|
|
28
31
|
const intervalFn = () => sendMessage({ id, root });
|
|
29
32
|
if (handshakeInterval) {
|
|
30
|
-
// FIXME TS2322: Type 'Timer' is not assignable to type 'number'.
|
|
31
33
|
intervalId = setInterval(intervalFn, handshakeInterval);
|
|
32
34
|
}
|
|
33
35
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../../../packages/deferred-data-access/interface/src/handshake.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,aAAa,EACb,gBAAgB,GACjB,MAAM,SAAS,CAAC;AAEjB,
|
|
1
|
+
{"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../../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ACrB,OAAO;QACT,CAAC;QAED,WAAW,CAAC,gBAAgB,CAAC,CAAC;QAE9B,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC7B,aAAa,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC,CAAC;IAEF,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAE5B,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAEnD,IAAI,iBAAiB,EAAE,CAAC;QACtB,UAAU,GAAG,WAAW,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;IAC1D,CAAC;SAAM,CAAC;QACN,UAAU,EAAE,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAEJ,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,EACxB,IAAI,EACJ,QAAQ,EACR,gBAAgB,EAChB,GAAG,MAAM,EACK,EAA8B,EAAE;IAC9C,MAAM,IAAI,GAAG;QACX,GAAG,MAAM;QACT,SAAS,EAAE,wBAAwB,CAAC,QAAQ,CAAC;KAC9C,CAAC;IAEF,MAAM,OAAO,GACX,IAAI,KAAK,aAAa,CAAC,IAAI;QACzB,CAAC,CAAC,aAAa,CAAC,IAA6B,CAAC;QAC9C,CAAC,CAAC,cAAc,CAAC,IAA2B,CAAC,CAAC;IAElD,OAAO,gBAAgB,CAAoB;QACzC,OAAO;QACP,OAAO,EAAE,gBAAgB;QACzB,YAAY,EAAE,4CAA4C,gBAAgB,KAAK;KAChF,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../../packages/deferred-data-access/interface/src/helpers.ts"],"names":[],"mappings":"AAEA,KAAK,YAAY,GAAG,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;AAE5C,eAAO,MAAM,gBAAgB,GAAI,QAAQ,OAAO,KAAG,OAYlD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,QAAQ,OAAO,KAAG,OAYjD,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAC9B,UAAU,GAAG,EACb,kBAAsB,KACrB;IACD,SAAS,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,IAAI,CAAC;IACtC,WAAW,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,IAAI,CAAC;CAkCzC,CAAC"}
|
|
@@ -7,8 +7,11 @@ export declare const initialize: ({ id: initId, root: apiRoot, ...params }: Init
|
|
|
7
7
|
root?: undefined;
|
|
8
8
|
} | {
|
|
9
9
|
stop: () => void;
|
|
10
|
-
pool: import("@actualwave/deferred-data-access/resource").ResourcePool;
|
|
11
10
|
wrap: (context?: unknown, command?: import("@actualwave/deferred-data-access/command").Command) => unknown;
|
|
12
|
-
pendingRequests: Map<
|
|
11
|
+
pendingRequests: Map<string, {
|
|
12
|
+
resolve: (v: unknown) => void;
|
|
13
|
+
reject: (e: unknown) => void;
|
|
14
|
+
}>;
|
|
15
|
+
pool: import("@actualwave/deferred-data-access/resource").ResourcePool;
|
|
13
16
|
root: unknown;
|
|
14
17
|
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intialize.d.ts","sourceRoot":"","sources":["../../../../packages/deferred-data-access/interface/src/intialize.ts"],"names":[],"mappings":"AAYA,OAAO,EAKL,UAAU,EACX,MAAM,SAAS,CAAC;AAYjB,eAAO,MAAM,UAAU,GAAU,0CAI9B,UAAU;;;;;;;;;;iBAsBE,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI;gBAAU,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI;;;;EA0GhE,CAAC"}
|
|
@@ -1,27 +1,12 @@
|
|
|
1
1
|
import { handle } from '@actualwave/deferred-data-access';
|
|
2
2
|
import { isResourceObject, } from '@actualwave/deferred-data-access/resource';
|
|
3
3
|
import { handshake } from './handshake';
|
|
4
|
-
import {
|
|
4
|
+
import { getPool, applyRemoteRequest } from './request';
|
|
5
5
|
import { createIsMessage, createRequestMessage, createResponseMessage, generateId, generateMessageId, getMessageEventData, MessageType, resolveOrTimeout, } from './utils';
|
|
6
|
-
/*
|
|
7
|
-
{
|
|
8
|
-
id,
|
|
9
|
-
type,
|
|
10
|
-
root,
|
|
11
|
-
remoteId, // we might expect a connection with matching ID
|
|
12
|
-
subscribe,
|
|
13
|
-
unsubscribe,
|
|
14
|
-
sendMessage,
|
|
15
|
-
handshakeTimeout,
|
|
16
|
-
responseTimeout,
|
|
17
|
-
handshakeInterval,
|
|
18
|
-
preprocessResponse,
|
|
19
|
-
}
|
|
20
|
-
*/
|
|
21
6
|
export const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
22
7
|
const id = initId || generateId();
|
|
23
8
|
const root = apiRoot
|
|
24
|
-
?
|
|
9
|
+
? getPool().set(apiRoot).toObject()
|
|
25
10
|
: undefined;
|
|
26
11
|
const { subscribe, unsubscribe, sendMessage, preprocessResponse = (data) => data, } = params;
|
|
27
12
|
const { id: remoteId, root: remoteRoot } = await handshake({
|
|
@@ -29,7 +14,8 @@ export const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
|
29
14
|
root,
|
|
30
15
|
...params,
|
|
31
16
|
});
|
|
32
|
-
|
|
17
|
+
// Tracks in-flight requests: messageId → { resolve, reject }
|
|
18
|
+
const pendingRequests = new Map();
|
|
33
19
|
const isMessage = createIsMessage(id);
|
|
34
20
|
const createRequest = createRequestMessage(id, remoteId);
|
|
35
21
|
const createResponse = createResponseMessage(id);
|
|
@@ -39,54 +25,51 @@ export const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
|
39
25
|
return;
|
|
40
26
|
}
|
|
41
27
|
switch (data.type) {
|
|
42
|
-
case MessageType.REQUEST:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
sendMessage(createResponse(request, undefined, { message: error.message }));
|
|
51
|
-
}
|
|
28
|
+
case MessageType.REQUEST: {
|
|
29
|
+
const request = data;
|
|
30
|
+
try {
|
|
31
|
+
const value = await applyRemoteRequest(request);
|
|
32
|
+
sendMessage(createResponse(request, value));
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
sendMessage(createResponse(request, undefined, { message: error.message }));
|
|
52
36
|
}
|
|
53
37
|
break;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
38
|
+
}
|
|
39
|
+
case MessageType.RESPONSE: {
|
|
40
|
+
const { id, value, error } = data;
|
|
41
|
+
const pending = pendingRequests.get(id);
|
|
42
|
+
if (!pending) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
pendingRequests.delete(id);
|
|
46
|
+
// Settle exactly once — delete before calling to prevent double-settle
|
|
47
|
+
if (error) {
|
|
48
|
+
pending.reject(error);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
pending.resolve(value);
|
|
65
52
|
}
|
|
66
53
|
break;
|
|
54
|
+
}
|
|
67
55
|
}
|
|
68
56
|
};
|
|
69
57
|
subscribe(messageHandler);
|
|
70
58
|
const stop = () => unsubscribe(messageHandler);
|
|
71
59
|
if (!remoteRoot) {
|
|
72
|
-
return { stop, pool };
|
|
60
|
+
return { stop, pool: getPool() };
|
|
73
61
|
}
|
|
74
|
-
const wrap = handle(async (command, context,
|
|
75
|
-
// wrap() is a partially applied handle(), so it makes possible to apply same command handlers to other objects
|
|
76
|
-
wrap) => {
|
|
62
|
+
const wrap = handle(async (command, context, wrap) => {
|
|
77
63
|
const { responseTimeout } = params;
|
|
78
|
-
|
|
79
|
-
If target is not a resource, there are no need to send command to remote, it can be resolved immediately.
|
|
80
|
-
Need to know if there will be such cases.
|
|
81
|
-
*/
|
|
82
|
-
const id = generateMessageId();
|
|
64
|
+
const msgId = generateMessageId();
|
|
83
65
|
const timeoutError = `Could not receive command ${command.type}/${String(command.name)} response in ${responseTimeout}ms.`;
|
|
84
66
|
const resultPromise = resolveOrTimeout({
|
|
85
67
|
handler: async (resolve, reject) => {
|
|
86
68
|
try {
|
|
87
|
-
const request = await createRequest(command, context,
|
|
69
|
+
const request = await createRequest(command, context, msgId);
|
|
70
|
+
// Register before sending to eliminate the race between send and response arrival
|
|
71
|
+
pendingRequests.set(msgId, { resolve, reject });
|
|
88
72
|
sendMessage(request);
|
|
89
|
-
pendingRequests.set(id, { resolve, reject });
|
|
90
73
|
}
|
|
91
74
|
catch (error) {
|
|
92
75
|
reject(error);
|
|
@@ -95,12 +78,9 @@ export const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
|
95
78
|
timeout: responseTimeout || 0,
|
|
96
79
|
timeoutError,
|
|
97
80
|
onTimeout: () => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
rq.reject(new Error(timeoutError));
|
|
103
|
-
pendingRequests.delete(id);
|
|
81
|
+
// resolveOrTimeout already rejected the race promise.
|
|
82
|
+
// We only need to clean up the pending entry here.
|
|
83
|
+
pendingRequests.delete(msgId);
|
|
104
84
|
},
|
|
105
85
|
});
|
|
106
86
|
const result = await resultPromise;
|
|
@@ -111,9 +91,9 @@ export const initialize = async ({ id: initId, root: apiRoot, ...params }) => {
|
|
|
111
91
|
}, false);
|
|
112
92
|
return {
|
|
113
93
|
stop,
|
|
114
|
-
pool,
|
|
115
94
|
wrap,
|
|
116
95
|
pendingRequests,
|
|
96
|
+
pool: getPool(),
|
|
117
97
|
root: remoteRoot ? wrap(remoteRoot) : null,
|
|
118
98
|
};
|
|
119
99
|
};
|