@iyio/obj-sync 0.7.50 → 0.8.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/package.json +10 -16
- package/src/index.d.ts +6 -6
- package/src/index.js +6 -9
- package/src/index.js.map +1 -1
- package/src/lib/ObjSyncBroadcastChannelClient.d.ts +2 -2
- package/src/lib/ObjSyncBroadcastChannelClient.js +2 -6
- package/src/lib/ObjSyncBroadcastChannelClient.js.map +1 -1
- package/src/lib/ObjSyncClient.d.ts +1 -1
- package/src/lib/ObjSyncClient.js +39 -43
- package/src/lib/ObjSyncClient.js.map +1 -1
- package/src/lib/ObjSyncWebsocketClient.d.ts +2 -2
- package/src/lib/ObjSyncWebsocketClient.js +6 -10
- package/src/lib/ObjSyncWebsocketClient.js.map +1 -1
- package/src/lib/obj-sync-tables.d.ts +1 -1
- package/src/lib/obj-sync-tables.js +10 -13
- package/src/lib/obj-sync-tables.js.map +1 -1
- package/src/lib/obj-sync-types.js +64 -68
- package/src/lib/obj-sync-types.js.map +1 -1
- package/src/lib/obj-sync.deps.js +7 -10
- package/src/lib/obj-sync.deps.js.map +1 -1
- package/src/package.json +1 -4
- package/tsconfig.tsbuildinfo +1 -0
- package/esm/package.json +0 -3
- package/esm/src/index.d.ts +0 -6
- package/esm/src/index.js +0 -7
- package/esm/src/index.js.map +0 -1
- package/esm/src/lib/ObjSyncBroadcastChannelClient.d.ts +0 -14
- package/esm/src/lib/ObjSyncBroadcastChannelClient.js +0 -41
- package/esm/src/lib/ObjSyncBroadcastChannelClient.js.map +0 -1
- package/esm/src/lib/ObjSyncClient.d.ts +0 -111
- package/esm/src/lib/ObjSyncClient.js +0 -658
- package/esm/src/lib/ObjSyncClient.js.map +0 -1
- package/esm/src/lib/ObjSyncWebsocketClient.d.ts +0 -20
- package/esm/src/lib/ObjSyncWebsocketClient.js +0 -71
- package/esm/src/lib/ObjSyncWebsocketClient.js.map +0 -1
- package/esm/src/lib/obj-sync-tables.d.ts +0 -7
- package/esm/src/lib/obj-sync-tables.js +0 -25
- package/esm/src/lib/obj-sync-tables.js.map +0 -1
- package/esm/src/lib/obj-sync-types.d.ts +0 -925
- package/esm/src/lib/obj-sync-types.js +0 -125
- package/esm/src/lib/obj-sync-types.js.map +0 -1
- package/esm/src/lib/obj-sync.deps.d.ts +0 -9
- package/esm/src/lib/obj-sync.deps.js +0 -11
- package/esm/src/lib/obj-sync.deps.js.map +0 -1
|
@@ -1,658 +0,0 @@
|
|
|
1
|
-
import { ObjMirror, addObjMirroringPauseCallback, asArray, createPromiseSource, deepClone, delayAsync, getValueByAryPath, isObjPathMirroringPaused, objWatchEvtSourceKey, objWatchEvtToRecursiveObjWatchEvt, stopWatchingObj, watchObj } from "@iyio/common";
|
|
2
|
-
import { BehaviorSubject, Subject } from "rxjs";
|
|
3
|
-
import { ObjSyncClientCommandScheme, ObjSyncRemoteCommandScheme, isObjSyncClientCommand, objSyncBroadcastClientId } from "./obj-sync-types";
|
|
4
|
-
export var ObjSyncClientLogLevel;
|
|
5
|
-
(function (ObjSyncClientLogLevel) {
|
|
6
|
-
ObjSyncClientLogLevel[ObjSyncClientLogLevel["none"] = 0] = "none";
|
|
7
|
-
ObjSyncClientLogLevel[ObjSyncClientLogLevel["all"] = 1] = "all";
|
|
8
|
-
ObjSyncClientLogLevel[ObjSyncClientLogLevel["commands"] = 2] = "commands";
|
|
9
|
-
ObjSyncClientLogLevel[ObjSyncClientLogLevel["queue"] = 4] = "queue";
|
|
10
|
-
ObjSyncClientLogLevel[ObjSyncClientLogLevel["ping"] = 8] = "ping";
|
|
11
|
-
})(ObjSyncClientLogLevel || (ObjSyncClientLogLevel = {}));
|
|
12
|
-
const logAllOrCommands = ObjSyncClientLogLevel.all | ObjSyncClientLogLevel.commands;
|
|
13
|
-
const logAllOrQueue = ObjSyncClientLogLevel.all | ObjSyncClientLogLevel.queue;
|
|
14
|
-
export class ObjSyncClient {
|
|
15
|
-
get changeIndex() { return this._changeIndex; }
|
|
16
|
-
get isReadySubject() { return this._isReady; }
|
|
17
|
-
get isReady() { return this._isReady.value; }
|
|
18
|
-
get connectionStateSubject() { return this._connectionState; }
|
|
19
|
-
get connectionState() { return this._connectionState.value; }
|
|
20
|
-
get onReconnected() { return this._onReconnected; }
|
|
21
|
-
get onDisposed() { return this._onDisposed; }
|
|
22
|
-
constructor({ objId, clientId, state, maxCommandQueueSeconds = 20, maxCommandQueueSize = 300, reconnectTimeoutMs = 30000, disabledAutoReconnect = false, clientMapProp, autoDeleteClientObjects, pingIntervalMs, sendDelayMs = 1, mergeEvents = false, isHost = false, }) {
|
|
23
|
-
this._changeIndex = null;
|
|
24
|
-
this.logCommands = ObjSyncClientLogLevel.none;
|
|
25
|
-
this._isReady = new BehaviorSubject(false);
|
|
26
|
-
this._connectionState = new BehaviorSubject('waiting');
|
|
27
|
-
this._onReconnected = new Subject();
|
|
28
|
-
this._onDisposed = new Subject();
|
|
29
|
-
this.readySource = createPromiseSource();
|
|
30
|
-
this._isDisposed = false;
|
|
31
|
-
this.onWatcherEvent = (obj, evt, path) => {
|
|
32
|
-
if (evt[objWatchEvtSourceKey] === this.objEvtSource ||
|
|
33
|
-
evt.type === 'load' ||
|
|
34
|
-
this._isDisposed) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const rEvt = objWatchEvtToRecursiveObjWatchEvt(evt, path);
|
|
38
|
-
if (rEvt.path && isObjPathMirroringPaused(this.state, rEvt.path)) {
|
|
39
|
-
const target = getValueByAryPath(this.state, rEvt.path);
|
|
40
|
-
this.queueObjCmd(target, rEvt);
|
|
41
|
-
addObjMirroringPauseCallback(target, this.onMirroringResume);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
this.send({ type: 'evt', evts: [rEvt] });
|
|
45
|
-
};
|
|
46
|
-
this.onMirroringResume = (obj) => {
|
|
47
|
-
const queue = this.dequeueObjCmdQueue(obj);
|
|
48
|
-
if (!queue) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
this.send({ type: 'evt', evts: queue });
|
|
52
|
-
};
|
|
53
|
-
this.sendQueue = [];
|
|
54
|
-
this._sendCount = 0;
|
|
55
|
-
this.skipApply = Symbol('skipApply');
|
|
56
|
-
this.connectPromise = null;
|
|
57
|
-
this.finishReconnectOnNextSet = false;
|
|
58
|
-
this.lastPing = 0;
|
|
59
|
-
this.lastPong = 0;
|
|
60
|
-
this.cmdQueue = [];
|
|
61
|
-
this.cmdQueueTTL = null;
|
|
62
|
-
this.queueKey = Symbol('queueKey');
|
|
63
|
-
this.objEvtSource = Symbol('objEvtSource');
|
|
64
|
-
this.objId = objId;
|
|
65
|
-
this.clientId = clientId;
|
|
66
|
-
this.state = state;
|
|
67
|
-
this.maxCommandQueueSeconds = maxCommandQueueSeconds;
|
|
68
|
-
this.maxCommandQueueSize = maxCommandQueueSize;
|
|
69
|
-
this.reconnectTimeoutMs = reconnectTimeoutMs;
|
|
70
|
-
this.disabledAutoReconnect = disabledAutoReconnect;
|
|
71
|
-
this.clientMapProp = clientMapProp;
|
|
72
|
-
this.autoDeleteClientObjects = autoDeleteClientObjects;
|
|
73
|
-
this.pingIntervalMs = pingIntervalMs;
|
|
74
|
-
this.sendDelayMs = Math.max(0, sendDelayMs);
|
|
75
|
-
this.mergeEvents = mergeEvents;
|
|
76
|
-
this.isHost = isHost;
|
|
77
|
-
this.hostState = {
|
|
78
|
-
objId,
|
|
79
|
-
changeIndex: 0,
|
|
80
|
-
state,
|
|
81
|
-
log: [],
|
|
82
|
-
};
|
|
83
|
-
const watcher = watchObj(state);
|
|
84
|
-
if (!watcher) {
|
|
85
|
-
throw new Error('Unable to get or create watcher for target state object');
|
|
86
|
-
}
|
|
87
|
-
this.watcher = watcher;
|
|
88
|
-
this.mirror = new ObjMirror(state, true);
|
|
89
|
-
watcher.addRecursiveListener(this.onWatcherEvent);
|
|
90
|
-
}
|
|
91
|
-
get isDisposed() { return this._isDisposed; }
|
|
92
|
-
dispose() {
|
|
93
|
-
if (this._isDisposed) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
this._isDisposed = true;
|
|
97
|
-
this.watcher.removeRecursiveListener(this.onWatcherEvent);
|
|
98
|
-
stopWatchingObj(this.state);
|
|
99
|
-
this.mirror.dispose();
|
|
100
|
-
this._dispose();
|
|
101
|
-
this._connectionState.next('closed');
|
|
102
|
-
this._onDisposed.next();
|
|
103
|
-
}
|
|
104
|
-
_dispose() {
|
|
105
|
-
// do nothing
|
|
106
|
-
}
|
|
107
|
-
_pingLost() {
|
|
108
|
-
// do nothing
|
|
109
|
-
}
|
|
110
|
-
send(cmd) {
|
|
111
|
-
if ((this.logCommands & logAllOrCommands) &&
|
|
112
|
-
((this.logCommands & ObjSyncClientLogLevel.ping) || !areAllPings(cmd))) {
|
|
113
|
-
console.info('>> obj-sync SND', ...asArray(cmd));
|
|
114
|
-
}
|
|
115
|
-
if (this._isDisposed) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (!Array.isArray(cmd)) {
|
|
119
|
-
cmd = [cmd];
|
|
120
|
-
}
|
|
121
|
-
if (!cmd.length) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
this._sendCount += cmd.length;
|
|
125
|
-
const initSend = this.sendQueue.length === 0;
|
|
126
|
-
for (const c of cmd) {
|
|
127
|
-
const last = this.sendQueue[this.sendQueue.length - 1];
|
|
128
|
-
if (last?.type === 'evt' && c.type === 'evt' && last.evts && c.evts) {
|
|
129
|
-
if (this.logCommands & logAllOrQueue) {
|
|
130
|
-
console.info('>> merge');
|
|
131
|
-
}
|
|
132
|
-
if (this.mergeEvents) {
|
|
133
|
-
mergeEvents(c.evts, last.evts);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
last.evts.push(...c.evts);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
this.sendQueue.push({
|
|
141
|
-
...c,
|
|
142
|
-
clientId: this.clientId,
|
|
143
|
-
objId: this.objId,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (initSend) {
|
|
148
|
-
setTimeout(() => {
|
|
149
|
-
if (this.isDisposed && !this.sendQueue.length) {
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
const q = this.sendQueue;
|
|
153
|
-
this.sendQueue = [];
|
|
154
|
-
if (this.logCommands & logAllOrQueue) {
|
|
155
|
-
console.info('>> send');
|
|
156
|
-
}
|
|
157
|
-
this.sendRemoteCommands(q);
|
|
158
|
-
}, this.sendDelayMs);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
get sendCount() { return this._sendCount; }
|
|
162
|
-
sendClientCommands(cmds, handled = false) {
|
|
163
|
-
if (this.isHost) {
|
|
164
|
-
cmds = deepClone(cmds);
|
|
165
|
-
const selfCommands = [];
|
|
166
|
-
const clientCommands = [];
|
|
167
|
-
for (const c of cmds) {
|
|
168
|
-
if (c.clientId === this.clientId) {
|
|
169
|
-
if (handled) {
|
|
170
|
-
c[this.skipApply] = true;
|
|
171
|
-
}
|
|
172
|
-
selfCommands.push(c);
|
|
173
|
-
}
|
|
174
|
-
else if (c.clientId === objSyncBroadcastClientId) {
|
|
175
|
-
if (handled) {
|
|
176
|
-
c[this.skipApply] = true;
|
|
177
|
-
}
|
|
178
|
-
selfCommands.push(c);
|
|
179
|
-
clientCommands.push(c);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
clientCommands.push(c);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
if (selfCommands.length) {
|
|
186
|
-
this.handleCommand(selfCommands, true);
|
|
187
|
-
}
|
|
188
|
-
if (clientCommands.length) {
|
|
189
|
-
this._send(clientCommands);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
this._send(cmds);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
sendRemoteCommands(cmds) {
|
|
197
|
-
if (this.isHost) {
|
|
198
|
-
cmds = deepClone(cmds);
|
|
199
|
-
setTimeout(() => {
|
|
200
|
-
this.handleCommand(cmds, true);
|
|
201
|
-
}, 0);
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
this._send(cmds);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
async connectAsync(connectWithDefaultState) {
|
|
208
|
-
if (this._isDisposed) {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (!this.connectPromise) {
|
|
212
|
-
this.connectPromise = this.connectInitAsync(connectWithDefaultState);
|
|
213
|
-
}
|
|
214
|
-
await this.connectPromise;
|
|
215
|
-
await this.readySource.promise;
|
|
216
|
-
}
|
|
217
|
-
async connectInitAsync(connectWithDefaultState) {
|
|
218
|
-
this._connectionState.next('connecting');
|
|
219
|
-
await this._connectAsync();
|
|
220
|
-
this._connectionState.next('connected');
|
|
221
|
-
this.send([{ type: 'createClient' }, {
|
|
222
|
-
type: 'get',
|
|
223
|
-
defaultState: connectWithDefaultState ? this.state : undefined,
|
|
224
|
-
clientMapProp: this.clientMapProp,
|
|
225
|
-
autoDeleteClientObjects: this.autoDeleteClientObjects,
|
|
226
|
-
}]);
|
|
227
|
-
this.pingLoop();
|
|
228
|
-
}
|
|
229
|
-
onDisconnected() {
|
|
230
|
-
this.tryReconnectAsync();
|
|
231
|
-
}
|
|
232
|
-
async tryReconnectAsync() {
|
|
233
|
-
if (this.disabledAutoReconnect) {
|
|
234
|
-
this.dispose();
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
if (this._connectionState.value === 'reconnecting') {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
this._connectionState.next('reconnecting');
|
|
241
|
-
await delayAsync(1000);
|
|
242
|
-
if (this.isDisposed) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
let connected = false;
|
|
246
|
-
let end = false;
|
|
247
|
-
await Promise.race([
|
|
248
|
-
(async () => {
|
|
249
|
-
while (!end) {
|
|
250
|
-
try {
|
|
251
|
-
await this._connectAsync();
|
|
252
|
-
connected = true;
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
catch (ex) {
|
|
256
|
-
console.warn('ObjSyncClient reconnect failed', ex);
|
|
257
|
-
await delayAsync(500);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
})(),
|
|
261
|
-
delayAsync(this.reconnectTimeoutMs),
|
|
262
|
-
]);
|
|
263
|
-
end = true;
|
|
264
|
-
if (this.isDisposed) {
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
if (!connected) {
|
|
268
|
-
this.dispose();
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
this._connectionState.next('connected');
|
|
272
|
-
this.lastPing = 0;
|
|
273
|
-
this.lastPong = 0;
|
|
274
|
-
this.finishReconnectOnNextSet = true;
|
|
275
|
-
this.send([{ type: 'createClient' }, {
|
|
276
|
-
type: 'get',
|
|
277
|
-
}]);
|
|
278
|
-
}
|
|
279
|
-
pingLoop() {
|
|
280
|
-
if (this.pingIntervalMs === undefined || this.isHost) {
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
this.lastPing = 0;
|
|
284
|
-
this.lastPong = 0;
|
|
285
|
-
const iv = setInterval(() => {
|
|
286
|
-
if (this.isDisposed) {
|
|
287
|
-
clearInterval(iv);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
if (this._connectionState.value === 'reconnecting') {
|
|
291
|
-
this.lastPing = 0;
|
|
292
|
-
this.lastPong = 0;
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
if (this.lastPong && this.lastPing && this.lastPing > this.lastPong) {
|
|
296
|
-
console.warn('ObjSyncClient ping lost');
|
|
297
|
-
this.lastPing = 0;
|
|
298
|
-
this.lastPong = 0;
|
|
299
|
-
this._pingLost();
|
|
300
|
-
this.tryReconnectAsync();
|
|
301
|
-
}
|
|
302
|
-
this.lastPing = Date.now();
|
|
303
|
-
this.send({ type: 'ping', pc: true });
|
|
304
|
-
}, this.pingIntervalMs);
|
|
305
|
-
}
|
|
306
|
-
handleError(error) {
|
|
307
|
-
console.error('ObjSyncClient error', error);
|
|
308
|
-
this.dispose();
|
|
309
|
-
}
|
|
310
|
-
handleCommand(command, handled = false) {
|
|
311
|
-
if ((this.logCommands & logAllOrCommands) &&
|
|
312
|
-
((this.logCommands & ObjSyncClientLogLevel.ping) || !areAllPings(command))) {
|
|
313
|
-
console.info('<< obj-sync RCV', ...asArray(command));
|
|
314
|
-
}
|
|
315
|
-
if (this._isDisposed) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
let remoteCommands = null;
|
|
319
|
-
if (Array.isArray(command)) {
|
|
320
|
-
let handleCount = 0;
|
|
321
|
-
for (const cmd of command) {
|
|
322
|
-
if (!isObjSyncClientCommand(cmd)) {
|
|
323
|
-
if (!remoteCommands) {
|
|
324
|
-
remoteCommands = [];
|
|
325
|
-
}
|
|
326
|
-
remoteCommands.push(cmd);
|
|
327
|
-
continue;
|
|
328
|
-
}
|
|
329
|
-
const parseResult = ObjSyncClientCommandScheme.safeParse(cmd);
|
|
330
|
-
if (parseResult.success === false) {
|
|
331
|
-
this.handleError(parseResult.error);
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
if (cmd.clientId === this.clientId || cmd.clientId === objSyncBroadcastClientId) {
|
|
335
|
-
handleCount++;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (handleCount) {
|
|
339
|
-
this.watcher.requestQueueChanges();
|
|
340
|
-
try {
|
|
341
|
-
for (const cmd of command) {
|
|
342
|
-
if (isObjSyncClientCommand(cmd) &&
|
|
343
|
-
(cmd.clientId === this.clientId || cmd.clientId === objSyncBroadcastClientId)) {
|
|
344
|
-
this._handleCommand(cmd);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
finally {
|
|
349
|
-
this.watcher.requestDequeueChanges();
|
|
350
|
-
}
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
if (isObjSyncClientCommand(command)) {
|
|
356
|
-
const parseResult = ObjSyncClientCommandScheme.safeParse(command);
|
|
357
|
-
if (parseResult.success === false) {
|
|
358
|
-
this.handleError(parseResult.error);
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
if (command.clientId === this.clientId || command.clientId === objSyncBroadcastClientId) {
|
|
362
|
-
this.watcher.requestQueueChanges();
|
|
363
|
-
try {
|
|
364
|
-
this._handleCommand(command);
|
|
365
|
-
}
|
|
366
|
-
finally {
|
|
367
|
-
this.watcher.requestDequeueChanges();
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
else {
|
|
372
|
-
remoteCommands = [command];
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
if (remoteCommands) {
|
|
376
|
-
this.handleRemoteCommands(remoteCommands, handled);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
handleRemoteCommands(commands, handled = false) {
|
|
380
|
-
if (!this.isHost) {
|
|
381
|
-
this.handleError('Remote command received by non-host client');
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
for (const cmd of commands) {
|
|
385
|
-
const parseResult = ObjSyncRemoteCommandScheme.safeParse(cmd);
|
|
386
|
-
if (parseResult.success === false) {
|
|
387
|
-
console.error('Invalid remote command', cmd);
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
switch (cmd.type) {
|
|
391
|
-
case 'createClient':
|
|
392
|
-
//ignore
|
|
393
|
-
break;
|
|
394
|
-
case 'get': {
|
|
395
|
-
const clientCmd = {
|
|
396
|
-
type: 'set',
|
|
397
|
-
clientId: cmd.clientId,
|
|
398
|
-
objId: this.hostState.objId,
|
|
399
|
-
changeIndex: this.hostState.changeIndex,
|
|
400
|
-
state: this.hostState,
|
|
401
|
-
};
|
|
402
|
-
this.sendClientCommands([clientCmd], handled);
|
|
403
|
-
break;
|
|
404
|
-
}
|
|
405
|
-
case 'evt': {
|
|
406
|
-
if (!cmd.evts) {
|
|
407
|
-
console.error('Evt command must define the evt prop');
|
|
408
|
-
break;
|
|
409
|
-
}
|
|
410
|
-
this.hostState.changeIndex++;
|
|
411
|
-
const clientCmd = {
|
|
412
|
-
type: 'evt',
|
|
413
|
-
clientId: objSyncBroadcastClientId,
|
|
414
|
-
objId: this.hostState.objId,
|
|
415
|
-
changeIndex: this.hostState.changeIndex,
|
|
416
|
-
evts: cmd.evts
|
|
417
|
-
};
|
|
418
|
-
this.sendClientCommands([clientCmd], handled);
|
|
419
|
-
break;
|
|
420
|
-
}
|
|
421
|
-
case 'ping': {
|
|
422
|
-
const clientCmd = {
|
|
423
|
-
type: 'pong',
|
|
424
|
-
clientId: cmd.clientId,
|
|
425
|
-
objId: cmd.objId,
|
|
426
|
-
changeIndex: 0,
|
|
427
|
-
};
|
|
428
|
-
this.sendClientCommands([clientCmd], handled);
|
|
429
|
-
break;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
broadcastState() {
|
|
435
|
-
if (!this.isHost) {
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
const clientCmd = {
|
|
439
|
-
type: 'set',
|
|
440
|
-
clientId: objSyncBroadcastClientId,
|
|
441
|
-
objId: this.hostState.objId,
|
|
442
|
-
changeIndex: this.hostState.changeIndex,
|
|
443
|
-
state: this.hostState,
|
|
444
|
-
};
|
|
445
|
-
this.sendClientCommands([clientCmd]);
|
|
446
|
-
}
|
|
447
|
-
queueCmd(command) {
|
|
448
|
-
if (this.cmdQueueTTL === null) {
|
|
449
|
-
this.cmdQueueTTL = Date.now() + (this.maxCommandQueueSeconds * 1000);
|
|
450
|
-
}
|
|
451
|
-
else if (this.cmdQueueTTL < Date.now()) {
|
|
452
|
-
this.handleError('command queue ttl reached');
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
455
|
-
else if (this.cmdQueue.length > this.maxCommandQueueSize) {
|
|
456
|
-
this.handleError('command queue size limit reached');
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
this.cmdQueue.push({ ...command });
|
|
460
|
-
}
|
|
461
|
-
flushCommandQueue() {
|
|
462
|
-
if (!this.cmdQueue.length) {
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
const queue = this.cmdQueue;
|
|
466
|
-
this.cmdQueue = [];
|
|
467
|
-
this.cmdQueueTTL = null;
|
|
468
|
-
queue.sort((a, b) => a.changeIndex - b.changeIndex);
|
|
469
|
-
if (queue[0]?.changeIndex !== (this._changeIndex ?? 0) + 1) {
|
|
470
|
-
this.handleError('Flushed command queue change index invalid');
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
for (const cmd of queue) {
|
|
474
|
-
this._handleCommand(cmd);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
_handleCommand(command) {
|
|
478
|
-
if (command.type === 'reset') {
|
|
479
|
-
this.dispose();
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
else if (command.type === 'pong') {
|
|
483
|
-
this.lastPong = Date.now();
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
else if (command.type === 'reconnect') {
|
|
487
|
-
this.tryReconnectAsync();
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
if (this._changeIndex === null) {
|
|
491
|
-
if (command.type !== 'set') {
|
|
492
|
-
this.queueCmd(command);
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
else {
|
|
497
|
-
if (command.changeIndex <= this._changeIndex) {
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
if (command.changeIndex !== this._changeIndex + 1) {
|
|
501
|
-
this.queueCmd(command);
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
if (this.cmdQueue.length) {
|
|
506
|
-
this.cmdQueue.push(command);
|
|
507
|
-
this.flushCommandQueue();
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
this._changeIndex = command.changeIndex;
|
|
511
|
-
const skipApply = command[this.skipApply] === true;
|
|
512
|
-
switch (command.type) {
|
|
513
|
-
case 'evt':
|
|
514
|
-
if (command.evts) {
|
|
515
|
-
if (!skipApply) {
|
|
516
|
-
for (const evt of command.evts) {
|
|
517
|
-
this.mirror.handleEvent(evt, this.objEvtSource);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
else {
|
|
522
|
-
this.handleError('command.evts missing');
|
|
523
|
-
break;
|
|
524
|
-
}
|
|
525
|
-
break;
|
|
526
|
-
case 'delete':
|
|
527
|
-
if (!skipApply) {
|
|
528
|
-
this.deleteState();
|
|
529
|
-
}
|
|
530
|
-
break;
|
|
531
|
-
case 'set':
|
|
532
|
-
if (command.state) {
|
|
533
|
-
if (command.state.objId !== this.objId) {
|
|
534
|
-
this.handleError('command.state.objId mismatch');
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
this._changeIndex = command.changeIndex;
|
|
538
|
-
if (!this.isHost && !skipApply) {
|
|
539
|
-
this.setStateWithLogs(command.state.state, command.state.log);
|
|
540
|
-
}
|
|
541
|
-
if (this.finishReconnectOnNextSet) {
|
|
542
|
-
this.finishReconnectOnNextSet = false;
|
|
543
|
-
this._onReconnected.next();
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
break;
|
|
547
|
-
}
|
|
548
|
-
if (command.type === 'set' && !this._isReady.value) {
|
|
549
|
-
this._isReady.next(true);
|
|
550
|
-
this.readySource.resolve();
|
|
551
|
-
if (this.isHost) {
|
|
552
|
-
this.broadcastState();
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
setState(obj) {
|
|
557
|
-
this.setStateWithLogs(obj, []);
|
|
558
|
-
}
|
|
559
|
-
setStateWithLogs(obj, log) {
|
|
560
|
-
this.watcher.requestQueueChanges();
|
|
561
|
-
try {
|
|
562
|
-
for (const e in this.state) {
|
|
563
|
-
if (obj[e] === undefined) {
|
|
564
|
-
this.watcher.deleteProp(e, this.objEvtSource);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
for (const e in obj) {
|
|
568
|
-
this.watcher.setOrMergeProp(e, obj[e], this.objEvtSource);
|
|
569
|
-
}
|
|
570
|
-
for (const e of log) {
|
|
571
|
-
this.mirror.handleEvent(e, this.objEvtSource);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
finally {
|
|
575
|
-
this.watcher.requestDequeueChanges();
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
deleteState() {
|
|
579
|
-
this.watcher.requestQueueChanges();
|
|
580
|
-
try {
|
|
581
|
-
for (const e in this.state) {
|
|
582
|
-
this.watcher.deleteProp(e, this.objEvtSource);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
finally {
|
|
586
|
-
this.watcher.requestDequeueChanges();
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
queueObjCmd(obj, cmd) {
|
|
590
|
-
if (!obj) {
|
|
591
|
-
return;
|
|
592
|
-
}
|
|
593
|
-
const ary = obj[this.queueKey] ?? (obj[this.queueKey] = []);
|
|
594
|
-
mergeEvents([deepClone(cmd)], ary);
|
|
595
|
-
}
|
|
596
|
-
dequeueObjCmdQueue(obj) {
|
|
597
|
-
if (!obj) {
|
|
598
|
-
return undefined;
|
|
599
|
-
}
|
|
600
|
-
const ary = obj[this.queueKey];
|
|
601
|
-
if (ary) {
|
|
602
|
-
delete obj[this.queueKey];
|
|
603
|
-
}
|
|
604
|
-
return ary;
|
|
605
|
-
}
|
|
606
|
-
simDisconnect() {
|
|
607
|
-
this.send({
|
|
608
|
-
type: 'simCleanUp'
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
const mergeEvents = (src, dest) => {
|
|
613
|
-
for (let s = 0; s < src.length; s++) {
|
|
614
|
-
const evt = src[s];
|
|
615
|
-
if (!evt) {
|
|
616
|
-
continue;
|
|
617
|
-
}
|
|
618
|
-
destLoop: for (let d = dest.length - 1; d >= 0; d--) {
|
|
619
|
-
const dEvt = dest[d];
|
|
620
|
-
if (dEvt &&
|
|
621
|
-
evt.type == 'set' &&
|
|
622
|
-
dEvt.type === 'set' &&
|
|
623
|
-
evt.prop === dEvt.prop &&
|
|
624
|
-
evt.path &&
|
|
625
|
-
dEvt.path &&
|
|
626
|
-
evt.path.length === dEvt.path.length) {
|
|
627
|
-
for (let l = 0; l < evt.path.length; l++) {
|
|
628
|
-
if (evt.path[l] !== dEvt.path[l]) {
|
|
629
|
-
continue destLoop;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
dest.splice(d, 1);
|
|
633
|
-
break;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
dest.push(evt);
|
|
637
|
-
}
|
|
638
|
-
};
|
|
639
|
-
const getPingCount = (cmd) => {
|
|
640
|
-
if (Array.isArray(cmd)) {
|
|
641
|
-
let count = 0;
|
|
642
|
-
for (let index = 0; index < cmd.length; index++) {
|
|
643
|
-
const c = cmd[index];
|
|
644
|
-
if (c && (c.type === 'pong' || c.type === 'ping')) {
|
|
645
|
-
count++;
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
return count;
|
|
649
|
-
}
|
|
650
|
-
else {
|
|
651
|
-
return (cmd.type === 'pong' || cmd.type === 'ping') ? 1 : 0;
|
|
652
|
-
}
|
|
653
|
-
};
|
|
654
|
-
const areAllPings = (cmd) => {
|
|
655
|
-
const count = getPingCount(cmd);
|
|
656
|
-
return Array.isArray(cmd) ? count === cmd.length : count === 1;
|
|
657
|
-
};
|
|
658
|
-
//# sourceMappingURL=ObjSyncClient.js.map
|