-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth_bus.js
More file actions
35 lines (31 loc) · 879 Bytes
/
Copy pathauth_bus.js
File metadata and controls
35 lines (31 loc) · 879 Bytes
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
function createAuthBus() {
function buildMessage(type, snapshot, extra = {}) {
return {
type: String(type || 'auth.snapshot'),
snapshot: snapshot && typeof snapshot === 'object' ? { ...snapshot } : {},
...extra,
};
}
function sendToTarget(target, type, snapshot, extra = {}) {
if (!target || target.killed || target.connected === false || typeof target.send !== 'function') return false;
try {
target.send(buildMessage(type, snapshot, extra));
return true;
} catch (_) {
return false;
}
}
function broadcast(targets = [], type, snapshot, extra = {}) {
return (Array.isArray(targets) ? targets : [])
.map((target) => sendToTarget(target, type, snapshot, extra))
.some(Boolean);
}
return {
buildMessage,
sendToTarget,
broadcast,
};
}
module.exports = {
createAuthBus,
};