@bikky/replication 1.0.13 → 1.0.14

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/Networking.js DELETED
@@ -1,768 +0,0 @@
1
- import { SmartArray } from "@bikky/smart-collections";
2
- import { Replicable } from "./Replicatable.js";
3
- import { IDPool } from "./IDPool.js";
4
- import { isPrimitiveValue } from "./Constants/SerialisationTypes.js";
5
- var InstanceByProxy = new Map();
6
- var LocalTime;
7
- let lastUpdate = Date.now();
8
- var Connections;
9
- (function (Connections) {
10
- Connections.RemotesByID = new Map();
11
- Connections.Remotes = new SmartArray();
12
- Connections.Interfaces = new Map();
13
- Connections.disconnectHandlers = new Map();
14
- /**
15
- * There's one RemoteConnection per remote PC. All instances reference the RemoteConnection.
16
- * The RemoteConnection handles batching as well as keeping track of the ping of clients and
17
- * how far behind they are.
18
- */
19
- class RemoteConnection {
20
- constructor(id, connection) {
21
- this.ping = -1;
22
- this.pingHistory = [];
23
- this.time = -1;
24
- this.messages = [];
25
- this.id = id;
26
- this.socket = connection;
27
- if ("on" in this.socket) {
28
- this.socket.on("message", (body) => {
29
- var data = JSON.parse(body);
30
- if ("request" in data && data.request == "id") {
31
- //Ignore connection requests, they're handled in AddRemoteConnection.
32
- return;
33
- }
34
- this.recieveMessages(data, id);
35
- });
36
- this.socket.on("close", (code, reason) => {
37
- this.handleDisconnect();
38
- });
39
- }
40
- else {
41
- this.socket.onmessage = (event) => {
42
- var body = event.data;
43
- var data = JSON.parse(body);
44
- if ("request" in data && data.request == "id") {
45
- //Ignore connection requests, they're handled in AddRemoteConnection.
46
- return;
47
- }
48
- this.recieveMessages(data, id);
49
- };
50
- this.socket.onclose = (event) => {
51
- this.handleDisconnect();
52
- };
53
- }
54
- Connections.disconnectHandlers.set(id, []);
55
- Connections.RemotesByID.set(id, this);
56
- Connections.Remotes.push(this);
57
- }
58
- handleDisconnect() {
59
- for (var handler of Connections.disconnectHandlers.get(this.id)) {
60
- handler();
61
- }
62
- Connections.RemotesByID.delete(this.id);
63
- Connections.Remotes.remove(this);
64
- for (var remoteInterface of Connections.Interfaces.values()) {
65
- remoteInterface.removeConnection(this.id, this);
66
- }
67
- Connections.disconnectHandlers.delete(this.id);
68
- }
69
- recieveMessages(data, remoteID) {
70
- this.ping = data.time - this.time;
71
- this.pingHistory.unshift(this.ping);
72
- if (this.pingHistory.length > 5) {
73
- this.pingHistory.pop();
74
- }
75
- this.time = data.time;
76
- if (!Messenger.isServer) {
77
- //TODO: Add ping to this calculation, but ping should be calculated based on the messages' clientTime not serverTime.
78
- LocalTime = data.time; // + this.ping / 2;
79
- lastUpdate = Date.now();
80
- }
81
- for (var message of data.messages) {
82
- var target = Connections.Interfaces.get(message.id);
83
- if (target) {
84
- target.recieveMessage(message, remoteID);
85
- }
86
- else {
87
- console.error("Remote tried to send a message to", message.id, "but no interface by that id exists");
88
- }
89
- }
90
- }
91
- queueMessage(obj) {
92
- this.messages.push(obj);
93
- }
94
- sendMessages() {
95
- try {
96
- if ("send" in this.socket) {
97
- this.socket.send(JSON.stringify({
98
- time: LocalTime,
99
- messages: this.messages
100
- }));
101
- }
102
- this.messages = [];
103
- }
104
- catch (e) {
105
- console.log("------------------------------------------------");
106
- console.log(e);
107
- console.log("Failed to send message batch");
108
- console.log("------------------------------------------------");
109
- }
110
- }
111
- }
112
- Connections.RemoteConnection = RemoteConnection;
113
- //This has the traps for the proxy to send messages over the network.
114
- class RemoteInstance {
115
- constructor(remoteID, connection, remoteInterface) {
116
- this.propertyMap = new Map();
117
- this.remoteID = remoteID;
118
- this.connection = connection;
119
- this.interface = remoteInterface;
120
- }
121
- send(message) {
122
- try {
123
- this.connection.queueMessage(message);
124
- }
125
- catch (e) {
126
- console.log("------------------------------------------------");
127
- console.log(e);
128
- console.log(message);
129
- console.log("------------------------------------------------");
130
- }
131
- }
132
- get(target, property) {
133
- if (typeof property === "symbol") {
134
- //this can be triggered by console logging a proxy so we should handle it nicely.
135
- console.error("Can't invoke symbol on remote!");
136
- return "This is a proxy";
137
- }
138
- var send = (property, ...args) => {
139
- var key;
140
- var promise = new Promise((resolve, reject) => {
141
- key = this.interface.addCompletion(resolve);
142
- });
143
- var data = {
144
- id: this.interface.objectID,
145
- property: property,
146
- args: args.map((e) => {
147
- if (Replicable.CanReference(e) && !isPrimitiveValue(e)) {
148
- return Replicable.Reference(e);
149
- }
150
- return ["Plain", e];
151
- }),
152
- key: key,
153
- from: Connections.LocalID,
154
- tick: LocalTime
155
- };
156
- this.send(data);
157
- return promise;
158
- };
159
- if (typeof property === "string") {
160
- var result = this.propertyMap.get(property);
161
- if (result) {
162
- return result;
163
- }
164
- //Proxy has to wrap a function to be callable.
165
- var proxy = new Proxy(() => { }, new PropertyProxy([property], send));
166
- this.propertyMap.set(property, proxy);
167
- return proxy;
168
- }
169
- else {
170
- return (...args) => send(property, ...args);
171
- }
172
- }
173
- //A trap for setting property values.
174
- set(target, property, value, receiver) {
175
- throw new Error("Don't set properties on remote objects directly, use the set function!");
176
- //return false;
177
- }
178
- //A trap for a direct function call (not a method).
179
- apply(...args) {
180
- var key;
181
- var promise = new Promise((resolve, reject) => {
182
- key = this.interface.addCompletion(resolve);
183
- });
184
- var data = {
185
- id: this.interface.objectID,
186
- function: true,
187
- args: args.map((e) => {
188
- if (Replicable.CanReference(e) && !isPrimitiveValue(e)) {
189
- return Replicable.Reference(e);
190
- }
191
- return ["Plain", e];
192
- }),
193
- key: key,
194
- from: Connections.LocalID,
195
- tick: LocalTime
196
- };
197
- this.send(data);
198
- return promise;
199
- }
200
- //A trap for the new operator.
201
- construct(target, args, newTarget) {
202
- throw new Error("Construct Trap Not Implemented");
203
- //return {};
204
- }
205
- }
206
- Connections.RemoteInstance = RemoteInstance;
207
- //This class allows you to send messages to property's functions. E.g. remote.property.foo.
208
- //It was coded at midnight and in a rush, so it's poorly typed and named.
209
- class PropertyProxy {
210
- constructor(propertyPath, send) {
211
- this.propertyMap = new Map();
212
- this.propertyPath = propertyPath;
213
- this.parentSend = send;
214
- }
215
- //A trap for getting property values.
216
- get(target, property) {
217
- if (typeof property === "symbol") {
218
- //this can be triggered by console logging a proxy so we should handle it nicely.
219
- console.error("Can't invoke symbol on remote!");
220
- return "This is a proxy";
221
- }
222
- else {
223
- if (typeof property === "string") {
224
- var result = this.propertyMap.get(property);
225
- if (result) {
226
- return result;
227
- }
228
- //Proxy has to wrap a function to be callable.
229
- var proxy = new Proxy(() => { }, new PropertyProxy([...this.propertyPath, property], this.parentSend));
230
- this.propertyMap.set(property, proxy);
231
- return proxy;
232
- }
233
- else {
234
- //property can be a number here, but I didn't want to update the typings everywhere.
235
- return (...args) => {
236
- return this.parentSend([...this.propertyPath, property], ...args);
237
- };
238
- }
239
- }
240
- }
241
- //A trap for setting property values.
242
- set(target, property, value, receiver) {
243
- throw new Error("Don't set properties on remote objects directly, use the set function!");
244
- //return false;
245
- }
246
- //A trap for a direct function call (not a method).
247
- apply(target, thisArg, args) {
248
- return this.parentSend(this.propertyPath, ...args);
249
- }
250
- }
251
- //This can hold one remote for client stuff (the server) or many remotes for server stuff (one for each client)
252
- // and the user won't know the difference.
253
- class RemoteInterface {
254
- constructor(objectID, local) {
255
- this.remotes = new Map();
256
- this.nextKey = 0;
257
- this.completions = new Map();
258
- //Allows users to access sub-properties via getters.
259
- this.propertyMap = new Map();
260
- this.objectID = objectID;
261
- this.local = local;
262
- Connections.Interfaces.set(this.objectID, this);
263
- }
264
- divertMessages(newLocal) {
265
- if (!this.oldLocal) {
266
- this.oldLocal = this.local;
267
- }
268
- this.local = newLocal;
269
- }
270
- restoreMessageRecipient() {
271
- if (this.oldLocal) {
272
- this.local = this.oldLocal;
273
- delete this.oldLocal;
274
- }
275
- }
276
- getNewKey() {
277
- var key = this.nextKey;
278
- this.nextKey++;
279
- return key;
280
- }
281
- addCompletion(resolver) {
282
- var key = this.getNewKey();
283
- this.completions.set(key, resolver);
284
- return key;
285
- }
286
- addConnection(remoteID, connection) {
287
- var remoteInstance = new RemoteInstance(remoteID, connection, this);
288
- remoteInstance.proxy = new Proxy({}, remoteInstance);
289
- InstanceByProxy.set(remoteInstance.proxy, remoteInstance);
290
- this.remotes.set(remoteID, remoteInstance);
291
- }
292
- removeConnection(remoteID, connection) {
293
- var instance = this.remotes.get(remoteID);
294
- this.remotes.delete(remoteID);
295
- return instance;
296
- }
297
- recieveMessage(data, remoteID) {
298
- var remote = this.remotes.get(remoteID);
299
- if (!remote) {
300
- return;
301
- }
302
- if ("completion" in data) {
303
- var completion = this.completions.get(data.key);
304
- if (completion) {
305
- this.completions.delete(data.key);
306
- completion(data.result);
307
- return;
308
- }
309
- else {
310
- //throw new Error("Got a message for a completion that's already been completed");
311
- console.error("Got a message for a completion that's already been completed", data);
312
- return;
313
- }
314
- }
315
- if (data.property) {
316
- var objectID = this.objectID;
317
- function sendResponse(result) {
318
- var response = {
319
- id: objectID,
320
- property: data.property,
321
- completion: true, //the function has finished being called.
322
- key: data.key,
323
- result: result,
324
- from: Connections.LocalID,
325
- tick: LocalTime
326
- };
327
- remote.send(response);
328
- }
329
- let args = data.args.map((e) => {
330
- if (Array.isArray(e) && e[0] === "Plain") {
331
- return e[1];
332
- }
333
- if (Replicable.IsReference(e)) {
334
- return Replicable.Dereference(e);
335
- }
336
- return e;
337
- });
338
- args.push({ connection: remote.proxy, id: data.from, tick: data.tick });
339
- var target = this.local;
340
- var prop = data.property;
341
- if (Array.isArray(prop)) {
342
- for (var i = 0; i < prop.length - 1; i++) {
343
- target = target[prop[i]];
344
- }
345
- //Store the last one.
346
- prop = prop[i];
347
- }
348
- var returned = target[prop].apply(target, args);
349
- if (returned instanceof Promise) {
350
- returned.then(sendResponse.bind(this));
351
- }
352
- else {
353
- sendResponse.call(this, returned);
354
- }
355
- }
356
- if (data.function) {
357
- let args = data.args.map((e) => {
358
- if (Array.isArray(e) && e[0] === "Plain") {
359
- return e[1];
360
- }
361
- if (Replicable.IsReference(e)) {
362
- return Replicable.Dereference(e);
363
- }
364
- return e;
365
- });
366
- args.push({ connection: remote.proxy, id: data.from, tick: data.tick });
367
- this.local
368
- .apply(this.local, args)
369
- .then((result) => {
370
- var response = {
371
- id: this.objectID,
372
- property: data.property,
373
- completion: true, //the function has finished being called.
374
- key: data.key,
375
- result: result,
376
- from: Connections.LocalID,
377
- tick: LocalTime
378
- };
379
- remote.send(response);
380
- });
381
- }
382
- }
383
- //A trap for getting property values.
384
- get(target, property) {
385
- if (typeof property === "symbol") {
386
- //this can be triggered by console logging a proxy so we should handle it nicely.
387
- console.error("Can't invoke symbol on remote!");
388
- return "This is a proxy";
389
- }
390
- else {
391
- var send = (property, ...args) => {
392
- if (Messenger.isServer) {
393
- var promises = [];
394
- for (var remote of this.remotes.values()) {
395
- let remoteFunction = remote.get(target, property);
396
- promises.push(remoteFunction.apply(remote, args));
397
- }
398
- //Don't return results from every client - as this is probably unintentional.
399
- return new Promise((res, rej) => {
400
- Promise.all(promises)
401
- .then(() => res())
402
- .catch(rej);
403
- });
404
- }
405
- else {
406
- for (var remote of this.remotes.values()) {
407
- let remoteFunction = remote.get(target, property);
408
- return remoteFunction.apply(remote, args);
409
- }
410
- }
411
- };
412
- if (typeof property === "string") {
413
- var result = this.propertyMap.get(property);
414
- if (result) {
415
- return result;
416
- }
417
- //Proxy has to wrap a function to be callable.
418
- var proxy = new Proxy(() => { }, new PropertyProxy([property], send));
419
- this.propertyMap.set(property, proxy);
420
- return proxy;
421
- }
422
- else {
423
- return (...args) => send(property, ...args);
424
- }
425
- }
426
- }
427
- //A trap for setting property values.
428
- set(target, property, value, receiver) {
429
- throw new Error("Don't set properties on remote objects directly, use the set function!");
430
- //return false;
431
- }
432
- //A trap for a direct function call (not a method).
433
- apply(...args) {
434
- return (...args) => {
435
- if (Messenger.isServer) {
436
- var promises = [];
437
- for (var remote of this.remotes.values()) {
438
- promises.push(remote.apply(remote, args));
439
- }
440
- //Don't return results from every client - as this is probably unintentional.
441
- return new Promise((res, rej) => {
442
- Promise.all(promises)
443
- .then(() => res())
444
- .catch(rej);
445
- });
446
- }
447
- else {
448
- for (var remote of this.remotes.values()) {
449
- return remote.apply(remote, args);
450
- }
451
- }
452
- };
453
- }
454
- //A trap for the new operator.
455
- construct(target, args, newTarget) {
456
- throw new Error("Construct Trap Not Implemented");
457
- //return {};
458
- }
459
- }
460
- Connections.RemoteInterface = RemoteInterface;
461
- })(Connections || (Connections = {}));
462
- export var Messenger;
463
- (function (Messenger) {
464
- var InterfaceByProxy = new Map();
465
- Messenger.isConnected = false;
466
- var resolveConnectPromise;
467
- Messenger.connect = new Promise((resolve) => (resolveConnectPromise = resolve));
468
- let serverID = null;
469
- function prepareIPCLikeWebsocket(connectionToServer) {
470
- if ("ipc" in connectionToServer) {
471
- // IPCRenderer.
472
- let socket = connectionToServer;
473
- connectionToServer = {
474
- send: (data) => {
475
- socket.send("message", data);
476
- },
477
- on: (channel, handler) => {
478
- socket.ipc.on(channel, (...args) => {
479
- if (args[1]) {
480
- return handler(args[1]);
481
- }
482
- else {
483
- return handler(args[0]);
484
- }
485
- });
486
- },
487
- once: (channel, handler) => {
488
- socket.ipc.once(channel, (...args) => {
489
- if (args[1]) {
490
- return handler(args[1]);
491
- }
492
- else {
493
- return handler(args[0]);
494
- }
495
- });
496
- }
497
- };
498
- }
499
- if ("emit" in connectionToServer) {
500
- // IPCMain for a specific window.
501
- let socket = connectionToServer;
502
- connectionToServer = {
503
- send: (data) => {
504
- socket.send("message", data);
505
- },
506
- on: (channel, handler) => {
507
- socket.on(channel, (...args) => {
508
- if (args[1]) {
509
- return handler(args[1]);
510
- }
511
- else {
512
- return handler(args[0]);
513
- }
514
- });
515
- },
516
- once: (channel, handler) => {
517
- socket.once(channel, (...args) => {
518
- if (args[1]) {
519
- return handler(args[1]);
520
- }
521
- else {
522
- return handler(args[0]);
523
- }
524
- });
525
- }
526
- };
527
- }
528
- return connectionToServer;
529
- }
530
- function initialise(isTheServer, connectionToServer) {
531
- Messenger.isServer = isTheServer;
532
- Messenger.isConnected = Messenger.isServer;
533
- if (Messenger.isServer) {
534
- LocalTime = 0;
535
- lastUpdate = Date.now();
536
- serverID = IDPool.useUUID();
537
- Connections.LocalID = serverID;
538
- resolveConnectPromise();
539
- }
540
- else {
541
- if (!connectionToServer) {
542
- throw new Error("Can't initialise client without a connection to the server");
543
- }
544
- connectionToServer = prepareIPCLikeWebsocket(connectionToServer);
545
- let res;
546
- let promise = new Promise((resolve) => res = resolve);
547
- var initialised = false;
548
- /**
549
- * Because websockets don't connect until the client sends a message, we start up the client
550
- * and send a pre-emptive message to the server. The return message should initialise the
551
- * client with its own unique ID as well as the server's time.
552
- */
553
- function initClient(data) {
554
- var message = JSON.parse(data);
555
- initialised = true;
556
- LocalTime = message.time;
557
- Connections.LocalID = message.id;
558
- Messenger.addRemoteConnection(message.serverID, connectionToServer);
559
- setTimeout(async () => {
560
- res();
561
- }, 1);
562
- }
563
- if ("once" in connectionToServer && connectionToServer.once) {
564
- connectionToServer.once("message", initClient);
565
- }
566
- else if ("on" in connectionToServer) {
567
- connectionToServer.on("message", (data) => {
568
- if (!initialised) {
569
- initClient(data);
570
- }
571
- });
572
- }
573
- else {
574
- connectionToServer.onmessage = (event) => {
575
- if (!initialised) {
576
- initClient(event.data);
577
- }
578
- };
579
- }
580
- connectionToServer.send(JSON.stringify({ request: "id" }));
581
- lastUpdate = Date.now();
582
- return promise;
583
- }
584
- }
585
- Messenger.initialise = initialise;
586
- //var lastMessage = 0;
587
- function tick() {
588
- let nextUpdate = Date.now();
589
- LocalTime += nextUpdate - lastUpdate;
590
- lastUpdate = nextUpdate;
591
- sendMessages();
592
- }
593
- Messenger.tick = tick;
594
- function sendMessages() {
595
- for (var conn of Connections.Remotes) {
596
- conn.sendMessages();
597
- }
598
- }
599
- Messenger.sendMessages = sendMessages;
600
- function getLocalTime() {
601
- return LocalTime;
602
- }
603
- Messenger.getLocalTime = getLocalTime;
604
- function onDisconnect(id, handler) {
605
- if (Connections.disconnectHandlers.has(id)) {
606
- Connections.disconnectHandlers.get(id).push(handler);
607
- return;
608
- }
609
- throw new Error("Can't add handler for connection that doesn't exist");
610
- }
611
- Messenger.onDisconnect = onDisconnect;
612
- //Connection is any because it throws an error during compile on the server - WebSocket the type isn't defined on the server.
613
- function addRemoteConnection(id, connection) {
614
- let websocket = prepareIPCLikeWebsocket(connection);
615
- let resolve;
616
- let ready = new Promise((res) => resolve = res);
617
- if (!Messenger.isServer) {
618
- serverID = id;
619
- var remote = new Connections.RemoteConnection(id, websocket);
620
- for (var remoteInterface of Connections.Interfaces.values()) {
621
- remoteInterface.addConnection(id, remote);
622
- }
623
- resolveConnectPromise();
624
- resolve(remote);
625
- }
626
- else {
627
- if ("on" in websocket) {
628
- websocket.on("message", (data) => {
629
- var message = JSON.parse(data);
630
- if (message.request == "id") {
631
- var remote = new Connections.RemoteConnection(id, websocket);
632
- for (var remoteInterface of Connections.Interfaces.values()) {
633
- remoteInterface.addConnection(id, remote);
634
- }
635
- websocket.send(JSON.stringify({ response: "id", id: id, serverID: serverID, time: Messenger.getLocalTime() }));
636
- resolve(remote);
637
- }
638
- });
639
- }
640
- else {
641
- websocket.onmessage = (event) => {
642
- var message = JSON.parse(event.data);
643
- if (message.request == "id") {
644
- var remote = new Connections.RemoteConnection(id, websocket);
645
- for (var remoteInterface of Connections.Interfaces.values()) {
646
- remoteInterface.addConnection(id, remote);
647
- }
648
- websocket.send(JSON.stringify({ response: "id", id: id, serverID: serverID, time: Messenger.getLocalTime() }));
649
- resolve(remote);
650
- }
651
- };
652
- }
653
- }
654
- return ready;
655
- }
656
- Messenger.addRemoteConnection = addRemoteConnection;
657
- //Local will usually be different to type even though they represent the same object/entity,
658
- // because usually the server will have different functions to the client.
659
- function newRemoteInterface(name, object) {
660
- var remoteInterface = new Connections.RemoteInterface(name, object);
661
- Connections.Interfaces.set(name, remoteInterface);
662
- var proxy = new Proxy({}, remoteInterface);
663
- InterfaceByProxy.set(proxy, remoteInterface);
664
- for (var [id, connection] of Connections.RemotesByID.entries()) {
665
- remoteInterface.addConnection(id, connection);
666
- }
667
- console.info("Made new interface", name, "for", object.__proto__.constructor.name, object.name || "");
668
- return proxy;
669
- }
670
- Messenger.newRemoteInterface = newRemoteInterface;
671
- function getInstanceForID(remoteInterface, id) {
672
- var trueRemoteInterface = InterfaceByProxy.get(remoteInterface);
673
- if (trueRemoteInterface) {
674
- var remote = trueRemoteInterface.remotes.get(id);
675
- if (remote) {
676
- return remote.proxy;
677
- }
678
- }
679
- return null;
680
- }
681
- Messenger.getInstanceForID = getInstanceForID;
682
- function getAllInstancesExceptID(remoteInterface, id) {
683
- var trueRemoteInterface = InterfaceByProxy.get(remoteInterface);
684
- if (trueRemoteInterface) {
685
- var instances = [];
686
- for (var remote of trueRemoteInterface.remotes.values()) {
687
- if (remote.remoteID !== id) {
688
- instances.push(remote.proxy);
689
- }
690
- }
691
- return instances;
692
- }
693
- return null;
694
- }
695
- Messenger.getAllInstancesExceptID = getAllInstancesExceptID;
696
- function getAllInstancesExcept(remoteInterface, remoteInstance) {
697
- var trueRemoteInterface = InterfaceByProxy.get(remoteInterface);
698
- if (trueRemoteInterface) {
699
- var instances = [];
700
- for (var remote of trueRemoteInterface.remotes.values()) {
701
- if (remote.proxy != remoteInstance) {
702
- instances.push(remote.proxy);
703
- }
704
- }
705
- return instances;
706
- }
707
- return null;
708
- }
709
- Messenger.getAllInstancesExcept = getAllInstancesExcept;
710
- function getRemoteTime(id) {
711
- return Connections.RemotesByID.get(id)?.time ?? null;
712
- }
713
- Messenger.getRemoteTime = getRemoteTime;
714
- function getRemotePing(id) {
715
- return Connections.RemotesByID.get(id)?.ping ?? null;
716
- }
717
- Messenger.getRemotePing = getRemotePing;
718
- function getRemoteAvgPing(id) {
719
- var remote = Connections.RemotesByID.get(id);
720
- if (!remote)
721
- return null;
722
- return remote.pingHistory.reduce((a, b) => a + b) / remote.pingHistory.length;
723
- }
724
- Messenger.getRemoteAvgPing = getRemoteAvgPing;
725
- function getIDForInstance(remoteInterface, remoteInstance) {
726
- var trueRemoteInterface = InterfaceByProxy.get(remoteInterface);
727
- if (trueRemoteInterface) {
728
- for (var remote of trueRemoteInterface.remotes.values()) {
729
- if (remote.proxy == remoteInstance) {
730
- return remote.remoteID;
731
- }
732
- }
733
- }
734
- return null;
735
- }
736
- Messenger.getIDForInstance = getIDForInstance;
737
- function isRemoteProxy(remote) {
738
- return InterfaceByProxy.has(remote) || InstanceByProxy.has(remote);
739
- }
740
- Messenger.isRemoteProxy = isRemoteProxy;
741
- function getLocalID() {
742
- return Connections.LocalID;
743
- }
744
- Messenger.getLocalID = getLocalID;
745
- /**
746
- * Will return the local id if on the server. Assumes that only the server connects to clients.
747
- */
748
- function getServerID() {
749
- if (serverID === null) {
750
- throw new Error(`Cannot get remote ID before server has connected/initialised.`);
751
- }
752
- return serverID;
753
- }
754
- Messenger.getServerID = getServerID;
755
- //Diverts messages from their original target to a new target. Good for proxies.
756
- function divertMessages(remote, newTarget) {
757
- var rInterface = InterfaceByProxy.get(remote);
758
- rInterface.divertMessages(newTarget);
759
- }
760
- Messenger.divertMessages = divertMessages;
761
- //Restores diverted messages to their original target.
762
- function restoreMessages(remote) {
763
- var rInterface = InterfaceByProxy.get(remote);
764
- rInterface.restoreMessageRecipient();
765
- }
766
- Messenger.restoreMessages = restoreMessages;
767
- })(Messenger || (Messenger = {}));
768
- //# sourceMappingURL=Networking.js.map