-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
PartyMessageHelper.js
687 lines (633 loc) · 26.5 KB
/
PartyMessageHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import global from '/scripts/core/global.js';
import AssetTypes from '/scripts/core/enums/AssetTypes.js';
import InternalMessageIds from '/scripts/core/enums/InternalMessageIds.js';
import PubSubTopics from '/scripts/core/enums/PubSubTopics.js';
import LibraryHandler from '/scripts/core/handlers/LibraryHandler.js';
import ProjectHandler from '/scripts/core/handlers/ProjectHandler.js';
import PubSub from '/scripts/core/handlers/PubSub.js';
import SettingsHandler from '/scripts/core/handlers/SettingsHandler.js';
import { uuidv4, uuidFromBytes, uuidToBytes, capitalizeFirstLetter, concatenateArrayBuffers, concatenateArrayBuffersFromList, Queue } from '/scripts/core/helpers/utils.module.js';
const SIXTEEN_KB = 1024 * 16;
const HANDLERS = {
asset_added: '_handleAssetAdded',
user_controller: '_handleUserController',
username: '_handleUsername',
};
const BUFFER_HANDLERS = {
[InternalMessageIds.ASSET_ADDED_PART]: '_handleBufferAssetAdded',
[InternalMessageIds.USER_PERSPECTIVE]: '_handleUserPerspective',
};
const BLOCKABLE_HANDLERS = {
instance_added: '_handleInstanceAdded',
instance_deleted: '_handleInstanceDeleted',
instance_updated: '_handleInstanceUpdated',
instance_attached: '_handleInstanceAttached',
instance_detached: '_handleInstanceDetached',
loaded_diff: '_handleLoadedDiff',
sanitize_internals: '_handleSanitizeInternals',
settings_updated: '_handleSettingsUpdated',
};
const BLOCKABLE_BUFFER_HANDLERS = {
[InternalMessageIds.ENTITY_POSITION]: '_handleEntityPosition',
[InternalMessageIds.ENTITY_ROTATION]: '_handleEntityRotation',
[InternalMessageIds.ENTITY_SCALE]: '_handleEntityScale',
[InternalMessageIds.ENTITY_TRANSFORMATION]: '_handleEntityTransformation',
[InternalMessageIds.COMPONENT_ATTACHED]: '_handleComponentAttached',
[InternalMessageIds.COMPONENT_DETACHED]: '_handleComponentDetached',
[InternalMessageIds.ENTITY_ADDED]: '_handleEntityAdded',
[InternalMessageIds.ENTITY_ATTACHED]: '_handleEntityAttached',
};
class PartyMessageHelper {
constructor() {
this._id = '44a9d6b3-2bf7-4e36-b8d1-10bb69de95cc';
this._idBytes = uuidToBytes(this._id);
this._handlingLocks = new Set();
this._handleQueue = new Queue();
this._publishQueue = new Queue();
}
init(PartyHandler) {
this._partyHandler = PartyHandler;
for(let id in HANDLERS) {
let handler = (p, m) => this[HANDLERS[id]](p, m);
this._partyHandler.addInternalMessageHandler(id, handler, true);
}
for(let id in BUFFER_HANDLERS) {
let handler = (p, m) => this[BUFFER_HANDLERS[id]](p, m);
this._partyHandler.addInternalBufferMessageHandler(id,handler,true);
}
for(let id in BLOCKABLE_HANDLERS) {
let handler = (p, m) => this[BLOCKABLE_HANDLERS[id]](p, m);
this._partyHandler.addInternalMessageHandler(id, handler);
}
for(let id in BLOCKABLE_BUFFER_HANDLERS) {
let handler = (p, m) => this[BLOCKABLE_BUFFER_HANDLERS[id]](p, m);
this._partyHandler.addInternalBufferMessageHandler(id, handler);
}
}
registerHandler(topic, handler) {
console.warn('registerHandler(...) is deprecated');
this._partyHandler.addMessageHandler(topic, handler);
}
registerBlockableHandler(topic, handler) {
console.warn('registerBlockableHandler(...) is deprecated');
let blockableHandler = (p, m) => { this._handleBlockable(handler,p,m);};
this._partyHandler.addMessageHandler(topic, blockableHandler);
}
publish(message) {
this._partyHandler._sendToAllPeers(message);
}
queuePublish(message) {
if(typeof message == 'function') {
this._publishQueue.enqueue(f);
} else {
this._publishQueue.enqueue(() => {
this._partyHandler._sendToAllPeers(message);
return Promise.resolve();
});
}
}
notifyDiffError() {
PubSub.publish(this._id, PubSubTopics.MENU_NOTIFICATION, {
text: "An unexpected error occured when loading the host's state",
});
}
_handleUserController(peer, message) {
if(!peer.controller) {
let params = message.controllerParams;
peer.username = params.username;
let controller = ProjectHandler.getSessionAsset(params.id);
if(controller) {
ProjectHandler.addAsset(controller);
controller.setDisplayingUsername(this._partyHandler
.getDisplayingUsernames());
} else {
params['displayingUsername'] = this._partyHandler
.getDisplayingUsernames();
controller = ProjectHandler.addNewAsset(params.assetId, params,
true, true);
}
if(controller.peer && controller.peer != peer)
controller.peer.controller = null;
peer.controller = controller;
controller.peer = peer;
}
}
_handleAssetAdded(peer, message) {
let lock = uuidv4();
message.lock = this._partyHandler.addMessageHandlerLock();
message.parts = [];
peer.assetAddedDetails = message;
}
_handleBufferAssetAdded(peer, message) {
if(!peer.assetAddedDetails) {
console.error('Unexpected call to _handleBufferAssetAdded() before _handleAssetAdded()');
return;
}
let {parts,partsLength,assetId,name,type,lock} = peer.assetAddedDetails;
parts.push(message);
if(parts.length == partsLength) {
let blob = new Blob(parts, { type: 'application/javascript' });
let assetDetails = {
Name: name,
Type: type,
};
LibraryHandler.loadLibraryAsset(assetId, assetDetails, blob)
.then(() => {
PubSub.publish(this._id, PubSubTopics.ASSET_ADDED,
assetId);
this._partyHandler.removeMessageHandlerLock(lock);
});
}
}
_handleBlockable(handler, peer, message) {
if(this._handlingLocks.size > 0) {
this._handleQueue.enqueue(() => {
handler(peer, message)
});
return;
}
handler(peer, message);
}
_handleComponentAttached(peer, message) {
let messageBytes = new Uint8Array(message);
message = {
id: uuidFromBytes(messageBytes.subarray(0, 16)),
componentId: uuidFromBytes(messageBytes.subarray(16, 32)),
componentAssetId: uuidFromBytes(messageBytes.subarray(32, 48)),
};
let asset = ProjectHandler.getSessionAsset(message.id);
if(asset) {
if(asset.editorHelper) {
asset.editorHelper.addComponent(message.componentId, true,true);
} else {
asset.addComponent(message.componentId, true);
}
PubSub.publish(this._id, PubSubTopics.COMPONENT_ATTACHED + ':'
+ message.componentAssetId, message);
}
}
_handleComponentDetached(peer, message) {
let messageBytes = new Uint8Array(message);
message = {
id: uuidFromBytes(messageBytes.subarray(0, 16)),
componentId: uuidFromBytes(messageBytes.subarray(16, 32)),
componentAssetId: uuidFromBytes(messageBytes.subarray(32, 48)),
};
let asset = ProjectHandler.getSessionAsset(message.id);
if(asset) {
if(asset.editorHelper) {
asset.editorHelper.removeComponent(message.componentId, true,
true);
} else {
asset.removeComponent(message.componentId, true);
}
PubSub.publish(this._id, PubSubTopics.COMPONENT_DETACHED + ':'
+ message.componentAssetId, message);
}
}
_handleEntityAdded(peer, message) {
let messageBytes = new Uint8Array(message);
message = {
parentId: uuidFromBytes(messageBytes.subarray(0, 16)),
childId: uuidFromBytes(messageBytes.subarray(16, 32)),
position: Array.from(new Float64Array(message, 32, 3)),
rotation: Array.from(new Float64Array(message, 56, 3)),
};
let parentAsset = ProjectHandler.getSessionAsset(message.parentId);
let childAsset = ProjectHandler.getSessionAsset(message.childId);
if(childAsset) {
if(childAsset.editorHelper) {
childAsset.editorHelper.addTo(parentAsset, true, true);
} else {
childAsset.addTo(parentAsset, true);
}
let object = childAsset.getObject();
object.position.fromArray(message.position);
object.rotation.fromArray(message.rotation);
PubSub.publish(this._id, PubSubTopics.ENTITY_ADDED, message);
} else {
console.error("Missing child in message for _handleEntityAdded()");
}
}
_handleEntityAttached(peer, message) {
let messageBytes = new Uint8Array(message);
message = {
parentId: uuidFromBytes(messageBytes.subarray(0, 16)),
childId: uuidFromBytes(messageBytes.subarray(16, 32)),
position: Array.from(new Float64Array(message, 32, 3)),
rotation: Array.from(new Float64Array(message, 56, 3)),
};
let parentAsset = ProjectHandler.getSessionAsset(message.parentId);
let childAsset = ProjectHandler.getSessionAsset(message.childId);
if(childAsset) {
if(childAsset.editorHelper) {
childAsset.editorHelper.attachTo(parentAsset, true, true);
} else {
childAsset.attachTo(parentAsset, true);
}
let object = childAsset.getObject();
object.position.fromArray(message.position);
object.rotation.fromArray(message.rotation);
PubSub.publish(this._id, PubSubTopics.ENTITY_ATTACHED, message);
} else {
console.error("Missing child from entity_attached message");
}
}
_handleEntityPosition(peer, message) {
let id = uuidFromBytes(new Uint8Array(message, 0, 16));
let position = Array.from(new Float64Array(message, 16, 3));
let asset = ProjectHandler.getSessionAsset(id);
if(asset) {
asset.setPosition(position);
} else {
console.error("Missing asset in message for _handleEntityPosition()");
}
}
_handleEntityRotation(peer, message) {
let id = uuidFromBytes(new Uint8Array(message, 0, 16));
let rotation = Array.from(new Float64Array(message, 16, 3));
let asset = ProjectHandler.getSessionAsset(id);
if(asset) {
asset.setRotation(rotation);
} else {
console.error("Missing asset in message for _handleEntityRotation()");
}
}
_handleEntityScale(peer, message) {
let id = uuidFromBytes(new Uint8Array(message, 0, 16));
let scale = Array.from(new Float64Array(message, 16, 3));
let asset = ProjectHandler.getSessionAsset(id);
if(asset) {
asset.setScale(scale);
} else {
console.error("Missing asset in message for _handleEntityScale()");
}
}
_handleEntityTransformation(peer, message) {
let id = uuidFromBytes(new Uint8Array(message, 0, 16));
let position = Array.from(new Float64Array(message, 16, 3));
let rotation = Array.from(new Float64Array(message, 40, 3));
let scale = Array.from(new Float64Array(message, 64, 3));
let asset = ProjectHandler.getSessionAsset(id);
if(asset) {
asset.setPosition(position);
asset.setRotation(rotation);
asset.setScale(scale);
} else {
console.error("Missing asset in message for _handleEntityTransformation()");
}
}
_handleInstanceAdded(peer, message) {
let asset = ProjectHandler.getSessionAsset(message.asset.id);
if(asset) {
ProjectHandler.addAsset(asset, true, true);
} else {
asset = ProjectHandler.addNewAsset(message.asset.assetId,
message.asset, true, true);
if(asset.getParentId) {
let parentId = asset.getParentId();
let parentAsset = ProjectHandler.getSessionAsset(parentId);
if(parentAsset) asset.addTo(parentAsset, true);
}
}
PubSub.publish(this._id, message.assetType + '_ADDED', asset);
}
_handleInstanceDeleted(peer, message) {
let asset = ProjectHandler.getAsset(message.id);
if(asset) {
ProjectHandler.deleteAsset(asset, true, true);
let topic = message.assetType + '_DELETED:' + message.id;
PubSub.publish(this._id, topic, { asset: asset });
} else {
console.error("Asset to delete does not exist");
}
}
_handleInstanceUpdated(peer, message) {
let asset = ProjectHandler.getSessionAsset(message.params.id);
if(asset) {
this._handleAssetUpdate(asset, message.params,
message.assetType + '_UPDATED');
}
}
_handleInstanceAttached(peer, message) {
let instance = ProjectHandler.getSessionAsset(message.id);
if(instance) {
let editorHelper = instance.editorHelper;
if(editorHelper) editorHelper.attachToPeer(peer, message);
}
}
_handleInstanceDetached(peer, message) {
let instance = ProjectHandler.getSessionAsset(message.id);
if(instance) {
let editorHelper = instance.editorHelper;
if(editorHelper) editorHelper.detachFromPeer(peer, message);
}
}
_handleLoadedDiff(peer, message) {
PubSub.publish(this._id, PubSubTopics.PEER_READY, { peer: peer });
peer.readyForUpdates = true;
}
_handleSanitizeInternals(peer, message) {
PubSub.publish(this._id, PubSubTopics.SANITIZE_INTERNALS,null,true);
}
_handleSettingsUpdated(peer, settings) {
for(let setting in settings) {
let handler;
if(setting == 'User Settings') {
handler = 'setUserSetting';
} else if(setting == 'Skybox') {
handler = 'setSkyboxSide';
} else {
console.error("Unknown setting updated: " + setting);
return;
}
for(let key in settings[setting]) {
SettingsHandler[handler](key, settings[setting][key], true);
}
}
PubSub.publish(this._id, PubSubTopics.SETTINGS_UPDATED,
{ settings: settings });
}
_handleAssetUpdate(asset, params, topic) {
let updatedParams = [];
for(let param in params) {
if(param == 'id') continue;
updatedParams.push(param);
let capitalizedParam = capitalizeFirstLetter(param);
if(('set' + capitalizedParam) in asset)
asset['set' + capitalizedParam](params[param]);
if(asset.editorHelper) asset.editorHelper.updateMenuField(param);
}
let message = {
asset: asset,
fields: updatedParams,
};
PubSub.publish(this._id, topic, message);
}
_handleUsername(peer, username) {
if(peer.username == username) return;
peer.username = username;
if(peer.controller) peer.controller.setUsername(username);
PubSub.publish(this._id, PubSubTopics.PEER_USERNAME_UPDATED, {
peer: peer,
});
}
_handleUserPerspective(peer, message) {
let perspective = new Uint8Array(message)[0];
if(peer.controller) peer.controller.setFirstPerson(perspective == 1);
}
handlePartyStarted() {
PubSub.publish(this._id, PubSubTopics.PARTY_STARTED);
}
handlePartyEnded() {
PubSub.publish(this._id, PubSubTopics.PARTY_ENDED);
}
handlePeerConnected(peer) {
PubSub.publish(this._id, PubSubTopics.PEER_CONNECTED, { peer: peer });
}
handlePeerDisconnected(peer) {
PubSub.publish(this._id, PubSubTopics.PEER_DISCONNECTED,{ peer: peer });
}
handleDiffLoaded() {
this._partyHandler._sendToAllPeers(JSON.stringify({
topic: 'loaded_diff',
}));
PubSub.publish(this._id, PubSubTopics.USER_READY);
}
_removeHandlingLock(lock) {
this._handlingLocks.delete(lock);
while(this._handleQueue.length > 0 && this._handlingLocks.size == 0) {
this._handleQueue.dequeue()();
}
}
_publishAssetAdded(assetId) {
this._partyHandler.publishFromFunction(() => {
return new Promise((resolve) => {
let libraryDetails = LibraryHandler.getLibrary()[assetId];
let blob = libraryDetails['Blob'];
blob.arrayBuffer().then((buffer) => {
let parts = [];
let n = Math.ceil(buffer.byteLength / SIXTEEN_KB);
for(let i = 0; i < n; i++) {
let chunkStart = i * SIXTEEN_KB;
let chunkEnd = (i + 1) * SIXTEEN_KB;
parts.push(buffer.slice(chunkStart, chunkEnd));
}
this._partyHandler.publishInternalMessage('asset_added', {
assetId: assetId,
name: libraryDetails['Name'],
type: libraryDetails['Type'],
partsLength: parts.length,
}, true);
for(let part of parts) {
this._partyHandler.publishInternalBufferMessage(
InternalMessageIds.ASSET_ADDED_PART, part, true);
}
resolve();
});
});
});
}
_publishComponentAttachedDetached(internalMessageId, message) {
let peerMessage = concatenateArrayBuffers(
uuidToBytes(message.id),
uuidToBytes(message.componentId),
uuidToBytes(message.componentAssetId)
);
this._partyHandler.publishInternalBufferMessage(internalMessageId,
peerMessage);
}
_publishEntityAdded(message) {
let childAsset = ProjectHandler.getSessionAsset(message.childId);
let peerMessage = concatenateArrayBuffers(
uuidToBytes(message.parentId),
uuidToBytes(message.childId),
new Float64Array(childAsset.getPosition()),
new Float64Array(childAsset.getRotation()),
);
this._partyHandler.publishInternalBufferMessage(
InternalMessageIds.ENTITY_ADDED, peerMessage);
}
_publishEntityAttached(message) {
let childAsset = ProjectHandler.getSessionAsset(message.childId);
let peerMessage = concatenateArrayBuffers(
uuidToBytes(message.parentId),
uuidToBytes(message.childId),
new Float64Array(childAsset.getPosition()),
new Float64Array(childAsset.getRotation()),
);
this._partyHandler.publishInternalBufferMessage(
InternalMessageIds.ENTITY_ATTACHED, peerMessage);
}
_publishInstanceAdded(asset, assetType) {
let message = {
asset: asset.exportParams(),
assetType: assetType,
};
this._partyHandler.publishInternalMessage('instance_added', message);
}
_publishInstanceDeleted(asset, assetType) {
let message = {
id: asset.getId(),
assetId: asset.getAssetId(),
assetType: assetType,
};
this._partyHandler.publishInternalMessage('instance_deleted', message);
}
_publishInstanceUpdated(updateMessage, assetType) {
let asset = {};
asset['id'] = updateMessage.asset.getId();
for(let param of updateMessage.fields) {
let capitalizedParam = capitalizeFirstLetter(param);
asset[param] = updateMessage.asset['get' + capitalizedParam]();
if(asset[param] === undefined) asset[param] = null;
}
let peerMessage = {
params: asset,
assetType: assetType,
};
this._partyHandler.publishInternalMessage('instance_updated',
peerMessage);
}
_publishInstanceAttached(data) {
let message = {
id: data.instance.getId(),
assetId: data.instance.getAssetId(),
option: data.option,
type: data.type,
};
if(global.deviceType == 'XR') {
message['position'] = data.position;
message['rotation'] = data.rotation;
message['scale'] = data.scale;
message['twoHandScaling'] = data.twoHandScaling;
message['isXR'] = true;
}
this._partyHandler.publishInternalMessage('instance_attached', message);
}
_publishInstanceDetached(data) {
let message = {
id: data.instance.getId(),
assetId: data.instance.getAssetId(),
option: data.option,
type: data.type,
};
if(global.deviceType == 'XR') {
message['position'] = data.position;
message['rotation'] = data.rotation;
message['scale'] = data.scale;
message['twoHandScaling'] = data.twoHandScaling;
message['isXR'] = true;
}
this._partyHandler.publishInternalMessage('instance_detached', message);
}
_publishSettingsUpdated(updateMessage) {
let settings = updateMessage.settings;
let keys = updateMessage.keys;
let message = {};
message[keys[0]] = {};
message[keys[0]][keys[1]] = settings[keys[0]][keys[1]];
this._partyHandler.publishInternalMessage('settings_updated', message);
}
_publishUserPerspectiveChanged(perspective) {
let message = new Uint8Array([perspective]);
this._partyHandler.publishInternalBufferMessage(
InternalMessageIds.USER_PERSPECTIVE, message);
}
_publishUsernameUpdated(username) {
this._partyHandler.publishInternalMessage('username', username);
}
addSubscriptions() {
PubSub.subscribe(this._id, PubSubTopics.ASSET_ADDED, (assetId) => {
this._publishAssetAdded(assetId);
});
PubSub.subscribe(this._id, PubSubTopics.BECOME_PARTY_HOST, () => {
this._partyHandler.setIsHost(true);
});
PubSub.subscribe(this._id, PubSubTopics.BOOT_PEER, (peerId) => {
this._partyHandler.bootPeer(peerId);
});
PubSub.subscribe(this._id, PubSubTopics.COMPONENT_ATTACHED, (message)=>{
this._publishComponentAttachedDetached(
InternalMessageIds.COMPONENT_ATTACHED, message);
});
PubSub.subscribe(this._id, PubSubTopics.COMPONENT_DETACHED, (message)=>{
this._publishComponentAttachedDetached(
InternalMessageIds.COMPONENT_DETACHED, message);
});
PubSub.subscribe(this._id, PubSubTopics.ENTITY_ADDED, (message) => {
this._publishEntityAdded(message);
});
PubSub.subscribe(this._id, PubSubTopics.ENTITY_ATTACHED, (message) => {
this._publishEntityAttached(message);
});
PubSub.subscribe(this._id, PubSubTopics.INSTANCE_ATTACHED, (message) =>{
this._publishInstanceAttached(message);
});
PubSub.subscribe(this._id, PubSubTopics.INSTANCE_DETACHED, (message) =>{
this._publishInstanceDetached(message);
});
PubSub.subscribe(this._id, PubSubTopics.SANITIZE_INTERNALS, () => {
this._publishQueue.enqueue(() => {
this._partyHandler._sendToAllPeers(
JSON.stringify({ topic: 'sanitize_internals' }));
return Promise.resolve();
});
});
PubSub.subscribe(this._id, PubSubTopics.SETTINGS_UPDATED, (message) => {
this._publishSettingsUpdated(message);
});
PubSub.subscribe(this._id, PubSubTopics.USER_PERSPECTIVE_CHANGED, (n)=>{
this._publishUserPerspectiveChanged(n);
});
PubSub.subscribe(this._id, PubSubTopics.USERNAME_UPDATED, (username) =>{
this._publishUsernameUpdated(username);
});
for(let assetType in AssetTypes) {
let addedTopic = PubSubTopics[assetType + '_ADDED'];
let deletedTopic = PubSubTopics[assetType + '_DELETED'];
let updatedTopic = PubSubTopics[assetType + '_UPDATED'];
PubSub.subscribe(this._id, addedTopic, (asset) => {
this._publishInstanceAdded(asset, assetType);
});
PubSub.subscribe(this._id, deletedTopic, (message) => {
this._publishInstanceDeleted(message.asset, assetType);
});
PubSub.subscribe(this._id, updatedTopic, (message) => {
this._publishInstanceUpdated(message, assetType);
});
}
}
removeSubscriptions() {
PubSub.unsubscribe(this._id, PubSubTopics.ASSET_ADDED);
PubSub.unsubscribe(this._id, PubSubTopics.BECOME_PARTY_HOST);
PubSub.unsubscribe(this._id, PubSubTopics.BOOT_PEER);
PubSub.unsubscribe(this._id, PubSubTopics.COMPONENT_ATTACHED);
PubSub.unsubscribe(this._id, PubSubTopics.COMPONENT_DETACHED);
PubSub.unsubscribe(this._id, PubSubTopics.ENTITY_ADDED);
PubSub.unsubscribe(this._id, PubSubTopics.ENTITY_ATTACHED);
PubSub.unsubscribe(this._id, PubSubTopics.INSTANCE_ATTACHED);
PubSub.unsubscribe(this._id, PubSubTopics.INSTANCE_DETACHED);
PubSub.unsubscribe(this._id, PubSubTopics.SANITIZE_INTERNALS);
PubSub.unsubscribe(this._id, PubSubTopics.SETTINGS_UPDATED);
PubSub.unsubscribe(this._id, PubSubTopics.USER_PERSPECTIVE_CHANGED);
PubSub.unsubscribe(this._id, PubSubTopics.USERNAME_UPDATED);
for(let assetType in AssetTypes) {
PubSub.unsubscribe(this._id, PubSubTopics[assetType + '_ADDED']);
PubSub.unsubscribe(this._id, PubSubTopics[assetType + '_DELETED']);
PubSub.unsubscribe(this._id, PubSubTopics[assetType + '_UPDATED']);
}
}
update() {
if(this._isPublishing || this._publishQueue.length == 0) return;
this._isPublishing = true;
this._publishQueue.dequeue()().then(() => this._isPublishing = false);
}
}
let partyMessageHelper = new PartyMessageHelper();
export default partyMessageHelper;