saasufy-components 1.8.0 → 1.9.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/app-router.js +90 -8
- package/package.json +1 -1
package/app-router.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import { toSafeHTML } from './utils.js';
|
|
2
|
-
import {
|
|
1
|
+
import { toSafeHTML, renderTemplate, debouncer } from './utils.js';
|
|
2
|
+
import { SocketConsumer } from './socket.js';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const DEFAULT_DEBOUNCE_DELAY = 100;
|
|
5
|
+
|
|
6
|
+
export class AppRouter extends SocketConsumer {
|
|
5
7
|
constructor() {
|
|
6
8
|
super();
|
|
7
9
|
this.attachShadow({ mode: 'open' });
|
|
8
10
|
|
|
9
11
|
this.pages = {};
|
|
10
12
|
this.pageRouteInfos = [];
|
|
13
|
+
this.debounce = debouncer();
|
|
14
|
+
this.debounceDelay = DEFAULT_DEBOUNCE_DELAY;
|
|
11
15
|
|
|
12
16
|
this.hashStartRegex = /^#/;
|
|
13
17
|
|
|
14
18
|
this.handleHashChangeEvent = () => {
|
|
15
|
-
this.renderCurrentPage
|
|
19
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
16
20
|
};
|
|
17
21
|
|
|
18
|
-
this.handleSlotChangeEvent = () => {
|
|
22
|
+
this.handleSlotChangeEvent = (event) => {
|
|
19
23
|
let pageTemplates = this.shadowRoot.querySelector('slot[name="page"]').assignedNodes();
|
|
20
24
|
|
|
21
25
|
for (let template of pageTemplates) {
|
|
@@ -30,8 +34,9 @@ export class AppRouter extends HTMLElement {
|
|
|
30
34
|
params: [ ...route.matchAll(/:[^\/]*/g) ].map(paramMatch => paramMatch[0].replace(':', ''))
|
|
31
35
|
};
|
|
32
36
|
});
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
if (!this.socket || this.socket.state === this.socket.OPEN) {
|
|
38
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
39
|
+
}
|
|
35
40
|
};
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -43,15 +48,61 @@ export class AppRouter extends HTMLElement {
|
|
|
43
48
|
return Object.fromEntries(argEntries);
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
static get observedAttributes() {
|
|
52
|
+
return [
|
|
53
|
+
'default-page',
|
|
54
|
+
'debounce-delay'
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
59
|
+
if (name === 'debounce-delay') {
|
|
60
|
+
if (newValue) {
|
|
61
|
+
this.debounceDelay = Number(newValue);
|
|
62
|
+
} else {
|
|
63
|
+
this.debounceDelay = DEFAULT_DEBOUNCE_DELAY;
|
|
64
|
+
}
|
|
65
|
+
} else if (this.isReady) {
|
|
66
|
+
this.renderCurrentPage();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
46
70
|
connectedCallback() {
|
|
47
71
|
window.addEventListener('hashchange', this.handleHashChangeEvent);
|
|
48
72
|
this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
// Socket is optional as not all routing use cases require authentication.
|
|
76
|
+
this.socket = this.getSocket();
|
|
77
|
+
} catch (error) {}
|
|
78
|
+
if (this.socket) {
|
|
79
|
+
let hasProcessedSocketConnect = this.socket.state === this.socket.OPEN;
|
|
80
|
+
this.authStateChangeConsumer = this.socket.listener('authStateChange').createConsumer();
|
|
81
|
+
(async () => {
|
|
82
|
+
for await (let event of this.authStateChangeConsumer) {
|
|
83
|
+
if (hasProcessedSocketConnect) {
|
|
84
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
|
|
89
|
+
this.connectConsumer = this.socket.listener('connect').createConsumer();
|
|
90
|
+
(async () => {
|
|
91
|
+
for await (let event of this.connectConsumer) {
|
|
92
|
+
hasProcessedSocketConnect = true;
|
|
93
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
}
|
|
49
97
|
this.render();
|
|
98
|
+
this.isReady = true;
|
|
50
99
|
}
|
|
51
100
|
|
|
52
101
|
disconnectedCallback() {
|
|
53
102
|
window.removeEventListener('hashchange', this.handleHashChangeEvent);
|
|
54
103
|
this.shadowRoot.removeEventListener('slotchange', this.handleSlotChangeEvent);
|
|
104
|
+
this.authStateChangeConsumer.kill();
|
|
105
|
+
this.connectConsumer.kill();
|
|
55
106
|
}
|
|
56
107
|
|
|
57
108
|
renderCurrentPage() {
|
|
@@ -66,7 +117,38 @@ export class AppRouter extends HTMLElement {
|
|
|
66
117
|
) || {};
|
|
67
118
|
|
|
68
119
|
let pageTemplate = this.pages[route];
|
|
69
|
-
if (!pageTemplate)
|
|
120
|
+
if (!pageTemplate) {
|
|
121
|
+
let defaultPage = this.getAttribute('default-page');
|
|
122
|
+
if (!defaultPage) return;
|
|
123
|
+
pageTemplate = this.pages[defaultPage];
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
let noAuthRedirect = pageTemplate.getAttribute('no-auth-redirect');
|
|
127
|
+
let authRedirect = pageTemplate.getAttribute('auth-redirect');
|
|
128
|
+
let hardRedirect = pageTemplate.hasAttribute('hard-redirect');
|
|
129
|
+
|
|
130
|
+
if (this.socket && (authRedirect || noAuthRedirect)) {
|
|
131
|
+
if (this.socket.authState === 'authenticated') {
|
|
132
|
+
if (authRedirect) {
|
|
133
|
+
if (hardRedirect) {
|
|
134
|
+
location.hash = authRedirect;
|
|
135
|
+
return;
|
|
136
|
+
} else {
|
|
137
|
+
pageTemplate = this.pages[authRedirect];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
if (this.socket.state !== this.socket.OPEN) return;
|
|
142
|
+
if (noAuthRedirect) {
|
|
143
|
+
if (hardRedirect) {
|
|
144
|
+
location.hash = noAuthRedirect;
|
|
145
|
+
return;
|
|
146
|
+
} else {
|
|
147
|
+
pageTemplate = this.pages[noAuthRedirect];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
70
152
|
|
|
71
153
|
let routeArgs;
|
|
72
154
|
if (route) {
|