-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synchroniser.js
181 lines (160 loc) · 4.73 KB
/
Synchroniser.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
/**
* Copyright (C) 2022, BBC R&D
* This source code is licensed under the GPL license found in the LICENSE file in this repository.
*/
import EventEmitter from 'events';
/**
* @typedef {object} Sync#presence
* @property {string} deviceId
* @property {string} status - 'joined' or 'left'
*/
/**
* @typedef {null} Sync#connected
*/
/**
* @typedef {null} Sync#disconnected
*/
/**
* @typedef {object} Sync#broadcast
* @property {string} topic
* @property {string} deviceId
* @property {object} content
*/
/**
* This class defines a common interface -- events and methods -- for the different sync
* and communication back-ends.
*
* @emits Sync#presence when any device joins or leaves the session
* @emits Sync#connected once successfully connected to the wall clock service
* @emits Sync#disconnected after failing to connect
* @emits Sync#message when any session-wide custom broadcast message is received
*/
class Sync extends EventEmitter {
/**
* @param {SyncAdapter} adapter
*/
constructor(adapter) {
super();
/**
* @private
* @type {SyncAdapter}
*/
this.adapter = adapter;
this.adapter.on('presence', this.onPresence.bind(this));
this.adapter.on('connected', this.onConnected.bind(this));
this.adapter.on('disconnected', this.onDisconnected.bind(this));
this.adapter.on('broadcast', this.onBroadcast.bind(this));
}
/**
* Initialises the adapter connection, and registers this device to join the given session.
*
* @param {string | object} endpoint - depending on the chosen adapter, the URL or
* { hostname, port } to connect to.
* @param {string} sessionId - the session to join
* @param {string} deviceId - the device-id to register as
* @param {bool} [startSession] - create a session (only used if this is distinct from joining)
*
* @returns {Promise}
*/
connect(endpoint, sessionId, deviceId, startSession) {
if (typeof endpoint !== 'string' && typeof endpoint !== 'object') {
throw new Error('endpoint must be set and must be a string or object.');
}
if (typeof sessionId !== 'string') {
throw new Error('sessionId must be set and must be a string.');
}
if (typeof deviceId !== 'string') {
throw new Error('deviceId must be set and must be a string.');
}
return this.adapter.connect(endpoint, { sessionId, deviceId, startSession });
}
/**
* Sends a broadcast message to all devices in the session.
*
* @param {string} topic
* @param {object} message
*
* @returns {Promise} resolving when the message has been successfully sent.
*/
sendMessage(topic, message) {
return this.adapter.sendMessage(topic, message);
}
/**
* Waits for a sync timeline of given type and contentId to be registered in the session and
* provides a CorrelatedClock for it when it becomes available.
*
* * Use this method for an auxiliary client to be controlled by a remote timeline.
* * Use {@link provideTimelineClock} for a main device also providing updates to the server.
*
* @param {string} timelineType
* @param {string} contentId
*
* @return {Promise<CorrelatedClock>} resolving when the clock is available.
* @public
*/
requestTimelineClock(timelineType, contentId) {
return this.adapter.requestTimelineClock(timelineType, contentId);
}
/**
* Registers a correlated clock to provide timeline updates to the
* service, and receive updates from it.
*
* The provided clock may be changed by the Sync service.
*
* * Use this method for a main device client controlling the experience. Multiple main devices
* will elect a leader for updating the session-wide timeline.
* * Use {@link synchroniseToTimeline} for a auxiliary client to wait until
* another device provides a timeline to synchronise to.
*
* @param {CorrelatedClock} timelineClock
* @param {string} timelineType
* @param {string} contentId
*
* @returns {Promise}
* @public
*/
provideTimelineClock(timelineClock, timelineType, contentId) {
return this.adapter.provideTimelineClock(timelineClock, timelineType, contentId);
}
/**
* Get the underlying wall clock instance.
* @returns {CorrelatedClock} wallClock
*/
get wallClock() {
return this.adapter.wallClock;
}
/**
* @private
*/
onPresence(e) {
this.emit('presence', {
deviceId: e.deviceId,
status: e.status,
});
}
/**
* @private
*/
onConnected() {
this.emit('connected');
}
/**
* @private
*/
onDisconnected(e) {
this.emit('disconnected', e);
}
/**
* @private
*/
onBroadcast(e) {
this.emit('broadcast', e);
}
/**
* Unregisters the sync adapter
*/
destroy() {
this.adapter.destroy();
}
}
export default Sync;