saasufy-components 9.1.3 → 9.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/socket.js +53 -1
package/package.json
CHANGED
package/socket.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { create } from '/node_modules/socketcluster-client/socketcluster-client.min.js';
|
|
2
|
-
import { convertStringToFieldParams } from './utils.js';
|
|
2
|
+
import { convertStringToFieldParams, debouncer } from './utils.js';
|
|
3
3
|
|
|
4
4
|
let globalSocket;
|
|
5
5
|
|
|
6
6
|
let urlPartsRegExp = /(^[^:]+):\/\/([^:\/]*)(:[0-9]*)?(\/.*)/;
|
|
7
7
|
|
|
8
|
+
const DEFAULT_DEBOUNCE_DELAY = 200;
|
|
9
|
+
|
|
8
10
|
export function createGlobalSocket(options) {
|
|
9
11
|
globalSocket = create({ ...options });
|
|
10
12
|
return globalSocket;
|
|
@@ -23,6 +25,7 @@ export class SocketProvider extends HTMLElement {
|
|
|
23
25
|
this.saasufySocket = null;
|
|
24
26
|
this.isReady = false;
|
|
25
27
|
this.lastExtraSocketOptionsString = '';
|
|
28
|
+
this.debounce = debouncer();
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
connectedCallback() {
|
|
@@ -189,12 +192,61 @@ export class SocketProvider extends HTMLElement {
|
|
|
189
192
|
return socket;
|
|
190
193
|
}
|
|
191
194
|
|
|
195
|
+
async reauthenticateSocket() {
|
|
196
|
+
if (this.saasufySocket) {
|
|
197
|
+
await this.saasufySocket.invoke('reauthenticate');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
handleAuthStateChange(event) {
|
|
202
|
+
if (this.saasufySocket) {
|
|
203
|
+
if (this.accessChangeChannelConsumer) {
|
|
204
|
+
this.accessChangeChannelConsumer.kill();
|
|
205
|
+
this.accessChangeChannelConsumer = null;
|
|
206
|
+
}
|
|
207
|
+
let accountId = event.authToken?.accountId;
|
|
208
|
+
if (accountId) {
|
|
209
|
+
this.accessChangeChannelConsumer = this.saasufySocket
|
|
210
|
+
.subscribe(`account-access-change/${accountId}`)
|
|
211
|
+
.createConsumer();
|
|
212
|
+
|
|
213
|
+
let debounceDelay = Number(
|
|
214
|
+
this.getAttribute('debounce-delay') ?? DEFAULT_DEBOUNCE_DELAY
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
(async () => {
|
|
218
|
+
for await (let data of this.accessChangeChannelConsumer) {
|
|
219
|
+
this.debounce(this.reauthenticateSocket, debounceDelay);
|
|
220
|
+
}
|
|
221
|
+
})();
|
|
222
|
+
} else {
|
|
223
|
+
let accessChangesChannelRegExp = /^account-access-change\/([^\/]*)/;
|
|
224
|
+
let accessSubscriptions = this.saasufySocket.subscriptions(true).filter(
|
|
225
|
+
(channelName) => channelName.match(accessChangesChannelRegExp)
|
|
226
|
+
);
|
|
227
|
+
for (let channelName of accessSubscriptions) {
|
|
228
|
+
this.saasufySocket.unsubscribe(channelName);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
192
234
|
createSocket() {
|
|
193
235
|
let socketOptions = this.getSocketOptions();
|
|
194
236
|
this.saasufySocket = create(socketOptions);
|
|
195
237
|
this.lastSaasufySocketURL = this.getSanitizedURL();
|
|
196
238
|
this.lastExtraSocketOptionsString = this.getAttribute('socket-options') || '';
|
|
197
239
|
|
|
240
|
+
if (this.authStateChangeConsumer) {
|
|
241
|
+
this.authStateChangeConsumer.kill();
|
|
242
|
+
}
|
|
243
|
+
this.authStateChangeConsumer = this.saasufySocket.listener('authStateChange').createConsumer();
|
|
244
|
+
(async () => {
|
|
245
|
+
for await (let event of this.authStateChangeConsumer) {
|
|
246
|
+
this.handleAuthStateChange(event);
|
|
247
|
+
}
|
|
248
|
+
})();
|
|
249
|
+
|
|
198
250
|
return this.saasufySocket;
|
|
199
251
|
}
|
|
200
252
|
}
|