@cocreate/lazy-loader 1.23.1 → 1.23.2
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/client.js +58 -63
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.23.2](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.1...v1.23.2) (2024-12-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* if (!window.CoCreate[name]) ([06f52b2](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/06f52b2f203d3860db065a786c095e8900d41508))
|
|
7
|
+
|
|
1
8
|
## [1.23.1](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.0...v1.23.1) (2024-12-09)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,80 +1,75 @@
|
|
|
1
|
-
import observer from
|
|
1
|
+
import observer from "@cocreate/observer";
|
|
2
2
|
|
|
3
3
|
function listen(name, callback, selector) {
|
|
4
|
+
async function observerCallback({ target }) {
|
|
5
|
+
if (!window.CoCreate) window.CoCreate = {};
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
if (window.CoCreate[name]) return;
|
|
8
|
+
window.CoCreate[name] = {};
|
|
9
|
+
observer.uninit(observerCallback);
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
observer.uninit(observerCallback)
|
|
11
|
+
const module = await callback();
|
|
12
|
+
observer.uninit(observerCallback);
|
|
13
|
+
window.CoCreate[name] = module.default || module;
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
window.CoCreate[name] = module.default || module
|
|
15
|
+
dispatchComponentLoaded(name);
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
selector,
|
|
25
|
-
callback: observerCallback
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
let selectorAttributes = [];
|
|
29
|
-
let attributes = selector.split(",")
|
|
30
|
-
for (let attribute of attributes) {
|
|
31
|
-
let attr = attribute.trim()
|
|
32
|
-
if (attr.startsWith("[")) {
|
|
33
|
-
let pos = attr.indexOf("*")
|
|
34
|
-
if (pos == -1)
|
|
35
|
-
pos = attr.indexOf("=")
|
|
36
|
-
if (pos !== -1) {
|
|
37
|
-
attr = attr.slice(1, pos)
|
|
38
|
-
} else {
|
|
39
|
-
attr = attr.slice(1, -1)
|
|
40
|
-
}
|
|
41
|
-
selectorAttributes.push(attr)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
if (selectorAttributes.length > 0)
|
|
46
|
-
observer.init({
|
|
47
|
-
name: 'lazyloadAttributeObserver',
|
|
48
|
-
observe: ['attributes'],
|
|
49
|
-
attributeName: selectorAttributes,
|
|
50
|
-
selector,
|
|
51
|
-
callback: observerCallback
|
|
52
|
-
});
|
|
18
|
+
observer.init({
|
|
19
|
+
name: "lazyloadObserver",
|
|
20
|
+
observe: ["childList"],
|
|
21
|
+
selector,
|
|
22
|
+
callback: observerCallback
|
|
23
|
+
});
|
|
53
24
|
|
|
25
|
+
let selectorAttributes = [];
|
|
26
|
+
let attributes = selector.split(",");
|
|
27
|
+
for (let attribute of attributes) {
|
|
28
|
+
let attr = attribute.trim();
|
|
29
|
+
if (attr.startsWith("[")) {
|
|
30
|
+
let pos = attr.indexOf("*");
|
|
31
|
+
if (pos == -1) pos = attr.indexOf("=");
|
|
32
|
+
if (pos !== -1) {
|
|
33
|
+
attr = attr.slice(1, pos);
|
|
34
|
+
} else {
|
|
35
|
+
attr = attr.slice(1, -1);
|
|
36
|
+
}
|
|
37
|
+
selectorAttributes.push(attr);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (selectorAttributes.length > 0)
|
|
41
|
+
observer.init({
|
|
42
|
+
name: "lazyloadAttributeObserver",
|
|
43
|
+
observe: ["attributes"],
|
|
44
|
+
attributeName: selectorAttributes,
|
|
45
|
+
selector,
|
|
46
|
+
callback: observerCallback
|
|
47
|
+
});
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
export async function lazyLoad(name, selector, callback) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
listen(name, callback, selector)
|
|
51
|
+
if (document.querySelector(selector))
|
|
52
|
+
await dependency(name, await callback());
|
|
53
|
+
else listen(name, callback, selector);
|
|
61
54
|
}
|
|
62
55
|
|
|
63
56
|
export async function dependency(name, promise) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
try {
|
|
58
|
+
let component = await promise;
|
|
59
|
+
if (!window.CoCreate) window.CoCreate = {};
|
|
60
|
+
if (!window.CoCreate[name]) {
|
|
61
|
+
window.CoCreate[name] = component.default || component;
|
|
62
|
+
dispatchComponentLoaded(name);
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error("error loading chunck: ", error);
|
|
66
|
+
}
|
|
74
67
|
}
|
|
75
68
|
|
|
76
69
|
function dispatchComponentLoaded(name) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
70
|
+
document.dispatchEvent(
|
|
71
|
+
new CustomEvent(name + "Loaded", {
|
|
72
|
+
detail: { name }
|
|
73
|
+
})
|
|
74
|
+
);
|
|
80
75
|
}
|