saasufy-components 3.19.1 → 3.21.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.
Files changed (4) hide show
  1. package/README.md +1 -0
  2. package/package.json +1 -1
  3. package/socket.js +15 -0
  4. package/utils.js +41 -24
package/README.md CHANGED
@@ -207,6 +207,7 @@ A Saasufy component which integrates with data from Saasufy is known as a `socke
207
207
 
208
208
  - `url` (required): Specifies the URL for the Saasufy service to use as the data store for all components which are placed inside it.
209
209
  - `auth-token-name`: Allows you to specify a custom key for your token - This will be used as the key in localStorage. You generally do not need to set this attribute. It is intended for situations were an app has multiple `socket-provider` elements connecting different services with different/separate authentication flows.
210
+ - `disable-tab-sync`: By default, the socket provider synchronizes socket auth state across multiple tabs via localStorage. If this attribute is set, then the socket auth state will not sync automatically and a manual page refresh may be necessary to update to the latest auth state.
210
211
 
211
212
  ### collection-browser
212
213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "3.19.1",
3
+ "version": "3.21.0",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/socket.js CHANGED
@@ -26,9 +26,24 @@ export class SocketProvider extends HTMLElement {
26
26
  connectedCallback() {
27
27
  this.isReady = true;
28
28
  this.getSocket();
29
+ let disableTabSync = this.hasAttribute('disable-tab-sync');
30
+ if (!disableTabSync) {
31
+ if (this.storageChangeWatcher) {
32
+ window.removeEventListener('storage', this.storageChangeWatcher);
33
+ }
34
+ this.storageChangeWatcher = (event) => {
35
+ if (event.storageArea === window.localStorage && event.key === this.saasufySocket.options.authTokenName) {
36
+ this.saasufySocket.reconnect();
37
+ }
38
+ };
39
+ }
40
+ window.addEventListener('storage', this.storageChangeWatcher);
29
41
  }
30
42
 
31
43
  disconnectedCallback() {
44
+ if (this.storageChangeWatcher) {
45
+ window.removeEventListener('storage', this.storageChangeWatcher);
46
+ }
32
47
  this.destroySocket();
33
48
  }
34
49
 
package/utils.js CHANGED
@@ -233,6 +233,7 @@ let templateFormatters = {
233
233
  computeId
234
234
  };
235
235
 
236
+ let templateRawTagsRegExp = /{{{.*?}}}/gs;
236
237
  let templateTagsRegExp = /{{.*?}}/gs;
237
238
 
238
239
  function execExpression(expression, options) {
@@ -246,33 +247,49 @@ function execExpression(expression, options) {
246
247
  return (new Function(...args))(...keys.map(key => options[key]));
247
248
  }
248
249
 
250
+ function getRenderOptions(data, socket) {
251
+ return {
252
+ ...templateFormatters,
253
+ uuid,
254
+ socket: socket ? {
255
+ state: socket.state,
256
+ pendingReconnect: socket.pendingReconnect,
257
+ connectAttempts: socket.connectAttempts,
258
+ authState: socket.authState,
259
+ authToken: socket.authToken
260
+ } : undefined,
261
+ ...data
262
+ };
263
+ }
264
+
249
265
  export function renderTemplate(templateString, data, socket) {
250
- return templateString.replace(templateTagsRegExp, (match) => {
251
- let expString = match.slice(2, -2);
252
- let options = {
253
- ...templateFormatters,
254
- uuid,
255
- socket: socket ? {
256
- state: socket.state,
257
- pendingReconnect: socket.pendingReconnect,
258
- connectAttempts: socket.connectAttempts,
259
- authState: socket.authState,
260
- authToken: socket.authToken
261
- } : undefined,
262
- ...data
263
- };
264
-
265
- try {
266
- return toSafeHTML(
267
- execExpression(
266
+ return templateString
267
+ .replace(templateRawTagsRegExp, (match) => {
268
+ let expString = match.slice(3, -3);
269
+ let options = getRenderOptions(data, socket);
270
+ try {
271
+ return execExpression(
268
272
  toExpression(expString),
269
273
  options
270
- )
271
- );
272
- } catch (error) {
273
- return match;
274
- }
275
- });
274
+ );
275
+ } catch (error) {
276
+ return match;
277
+ }
278
+ })
279
+ .replace(templateTagsRegExp, (match) => {
280
+ let expString = match.slice(2, -2);
281
+ let options = getRenderOptions(data, socket);
282
+ try {
283
+ return toSafeHTML(
284
+ execExpression(
285
+ toExpression(expString),
286
+ options
287
+ )
288
+ );
289
+ } catch (error) {
290
+ return match;
291
+ }
292
+ });
276
293
  }
277
294
 
278
295
  export function updateConsumerElements(consumers, value, template, sourceElementName) {