1 | /**
|
2 | * Broadcaster is used to call functions in multiple contexts at the
|
3 | * same time. This can be useful without having to handle complex logic
|
4 | * in the application side.
|
5 | *
|
6 | * See Plattar.messenger.broadcast
|
7 | */
|
8 | class Broadcaster {
|
9 | constructor(messengerInstance) {
|
10 | this._messengerInstance = messengerInstance;
|
11 | this._interfaces = [];
|
12 |
|
13 | return new Proxy(this, {
|
14 | get: (target, prop, receiver) => {
|
15 | switch (prop) {
|
16 | case "_push":
|
17 | case "_interfaces": return target[prop];
|
18 | default:
|
19 | break;
|
20 | }
|
21 |
|
22 | // execute the desired function on all available stacks
|
23 | return (...args) => {
|
24 | const interfaces = target._interfaces;
|
25 | const promises = [];
|
26 |
|
27 | interfaces.forEach((callable) => {
|
28 | promises.push(target._messengerInstance[callable][prop](...args));
|
29 | });
|
30 |
|
31 | return Promise.allSettled(promises);
|
32 | };
|
33 | }
|
34 | });
|
35 | }
|
36 |
|
37 | /**
|
38 | * Adds a new callable interface ID to the list of callables
|
39 | */
|
40 | _push(interfaceID) {
|
41 | this._interfaces.push(interfaceID);
|
42 | }
|
43 | }
|
44 |
|
45 | module.exports = Broadcaster; |
\ | No newline at end of file |