-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
208 lines (188 loc) · 6.3 KB
/
index.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
/**
* Class representing an IframeSyncClient.
* Browser iframes that want to participate in state synchronization should instantiate this class.
*/
class IframeSyncClient {
#channel;
#recv;
#clientName;
/**
* Create an IframeSyncClient.
* @param {string} [clientName] - A unique client name. If not provided, one will be generated randomly.
* @param {function} recv - A callback function to receive state updates.
*/
constructor(clientName, recv) {
this.#recv = recv;
this.#channel = 'IframeSync';
this.#clientName = clientName || [...Array(16)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
if (!window) {
return;
}
window.addEventListener('message', (event) => {
if (!event.data || event.data.channel !== this.#channel) {
return;
}
const isOwnMessage = event.data.sourceClientName === this.#clientName;
const isReadyReceived = event.data.type === 'readyReceived';
if (['syncState', 'readyReceived'].includes(event.data.type) && typeof this.#recv === 'function') {
this.#recv(event.data.payload, isOwnMessage, isReadyReceived);
}
});
}
/**
* Notify the parent window that this client is ready to receive state updates.
*/
ready() {
if (!window || !window.parent) {
return;
}
window.parent.postMessage({
channel: this.#channel,
type: 'ready',
sourceClientName: this.#clientName
}, '*');
}
/**
* Send a state update to the broker, which will broadcast it to all other clients.
* Partial updates are OK, as the broker will merge the update into the current state.
* @param {Object} update - The state update to send.
*/
stateChange(update) {
if (!window || !window.parent) {
return;
}
window.parent.postMessage({
channel: this.#channel,
type: 'stateChange',
sourceClientName: this.#clientName,
payload: update
}, '*');
}
}
/**
* Class representing an IframeSyncBroker.
*/
class IframeSyncBroker {
#channel;
#state;
#clientIframes;
#debugMode;
/**
* Create an IframeSyncBroker.
*/
constructor() {
this.#channel = 'IframeSync';
this.#state = {};
this.#clientIframes = new Set();
this.#debugMode = false;
if (!window) {
return;
}
window.addEventListener('message', (event) => this.#handleMessage(event));
}
/**
* Handle incoming messages.
* @param {MessageEvent} event - The message event.
* @private
*/
#handleMessage(event) {
const { data, source: clientIframe } = event;
if (!data || data.channel !== this.#channel) {
return;
}
if (data.type === 'ready') {
this.#clientIframes.add(clientIframe);
this.#sendReadyReceived(clientIframe);
} else if (data.type === 'stateChange' && data.payload) {
this.#updateState(data.payload, data.sourceClientName);
}
}
/**
* Update the state with the provided update.
* @param {Object} update - The state update.
* @param {string} sourceClientName - The name of the client that sent the update.
* @private
*/
#updateState(update, sourceClientName) {
const prevState = JSON.stringify(this.#state);
Object.assign(this.#state, update);
const newState = JSON.stringify(this.#state);
if (prevState !== newState) {
this.#debug();
this.#broadcastState(sourceClientName);
}
}
/**
* Send the current state to a specific client iframe.
* @param {Window} clientIframe - The client iframe to send the state to.
* @param {string} sourceClientName - The name of the client that requested the state.
* @private
*/
#sendSyncState(clientIframe, sourceClientName) {
if (clientIframe && typeof clientIframe.postMessage === 'function') {
clientIframe.postMessage({
channel: this.#channel,
type: 'syncState',
sourceClientName: sourceClientName, // Pass through the source
payload: this.#state,
}, '*');
}
}
/**
* Send the current state to a specific client iframe.
* @param {Window} clientIframe - The client iframe to send the state to.
* @param {string} sourceClientName - The name of the client that requested the state.
* @private
*/
#sendReadyReceived(clientIframe) {
if (clientIframe && typeof clientIframe.postMessage === 'function') {
clientIframe.postMessage({
channel: this.#channel,
type: 'readyReceived',
payload: this.#state,
}, '*');
}
}
/**
* Broadcast the current state to all client iframes.
* @param {string} sourceClientName - The name of the client that sent the update.
* @private
*/
#broadcastState(sourceClientName) {
this.#clientIframes.forEach((clientIframe) =>
this.#sendSyncState(clientIframe, sourceClientName)
);
}
/**
* Log a debug message.
* @private
*/
#debug() {
if (this.#debugMode === false) {
return; // noop by default
}
const stateJson = JSON.stringify(this.#state, null, 2);
if (this.#debugMode === true) {
console.log('IframeSyncBroker state change', stateJson);
} else if (typeof this.#debugMode === 'function') {
this.#debugMode(stateJson);
} else if (this.#debugMode instanceof HTMLElement) {
this.#debugMode.innerText = stateJson;
}
}
/**
* Control debug behavior.
* @param {boolean|Function|HTMLElement} mode - The debug mode.
* * false (default): no debug
* * true: console.log
* * function: call a provided function
* * HTML element: set the text of an element
*/
setDebugMode(mode) {
this.#debugMode = mode;
}
}
// CommonJS export for Node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = { IframeSyncClient, IframeSyncBroker };
}