Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06f52b2

Browse files
fix: if (!window.CoCreate[name])
1 parent 22bf747 commit 06f52b2

File tree

1 file changed

+58
-63
lines changed

1 file changed

+58
-63
lines changed

‎src/client.js

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,75 @@
1-
import observer from '@cocreate/observer';
1+
import observer from "@cocreate/observer";
22

33
function listen(name, callback, selector) {
4+
async function observerCallback({ target }) {
5+
if (!window.CoCreate) window.CoCreate = {};
46

5-
asyncfunctionobserverCallback({ target }){
6-
if(!window.CoCreate)
7-
window.CoCreate={}
7+
if(window.CoCreate[name])return;
8+
window.CoCreate[name]={};
9+
observer.uninit(observerCallback);
810

9-
if (window.CoCreate[name])
10-
return
11-
window.CoCreate[name] = {}
12-
observer.uninit(observerCallback)
11+
const module = await callback();
12+
observer.uninit(observerCallback);
13+
window.CoCreate[name] = module.default || module;
1314

14-
const module = await callback()
15-
observer.uninit(observerCallback)
16-
window.CoCreate[name] = module.default || module
15+
dispatchComponentLoaded(name);
16+
}
1717

18-
dispatchComponentLoaded(name)
19-
}
20-
21-
observer.init({
22-
name: 'lazyloadObserver',
23-
observe: ['childList'],
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+
});
5324

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+
});
5448
}
5549

5650
export async function lazyLoad(name, selector, callback) {
57-
if (document.querySelector(selector))
58-
await dependency(name, await callback())
59-
else
60-
listen(name, callback, selector)
51+
if (document.querySelector(selector))
52+
await dependency(name, await callback());
53+
else listen(name, callback, selector);
6154
}
6255

6356
export async function dependency(name, promise) {
64-
try {
65-
let component = await promise;
66-
if (!window.CoCreate)
67-
window.CoCreate={}
68-
69-
window.CoCreate[name]=component.default||component
70-
dispatchComponentLoaded(name)
71-
} catch (error) {
72-
console.error('error loading chunck: ', error)
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+
}
7467
}
7568

7669
function dispatchComponentLoaded(name) {
77-
document.dispatchEvent(new CustomEvent(name + 'Loaded', {
78-
detail: { name }
79-
}));
70+
document.dispatchEvent(
71+
new CustomEvent(name + "Loaded", {
72+
detail: { name }
73+
})
74+
);
8075
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /