Skip to content

Commit 68b7595

Browse files
committed
Improved script loader.
1 parent 9ebb417 commit 68b7595

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

Diff for: prettier.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
arrowParens: 'always',
4+
trailingComma: 'es5',
5+
};

Diff for: src/components/EmailEditor.vue

+3-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
</template>
44

55
<script>
6+
import { loadScript } from './loadScript';
7+
68
export default {
79
name: 'EmailEditor',
810
props: {
@@ -17,26 +19,7 @@ export default {
1719
},
1820
},
1921
created() {
20-
const embedJs = "//editor.unlayer.com/embed.js?2"
21-
const scripts = document.querySelectorAll('script');
22-
let scriptLoaded = false
23-
24-
scripts.forEach(script => {
25-
if (script.src.includes(embedJs)) {
26-
scriptLoaded = true
27-
}
28-
})
29-
30-
if (!scriptLoaded) {
31-
const unlayerScript = document.createElement('script');
32-
unlayerScript.setAttribute('src', embedJs);
33-
unlayerScript.onload = () => {
34-
this.loadEditor();
35-
};
36-
document.head.appendChild(unlayerScript);
37-
} else {
38-
this.loadEditor();
39-
}
22+
loadScript(this.loadEditor);
4023
},
4124
mounted() {
4225
},

Diff for: src/components/loadScript.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const callbacks = [];
2+
3+
const runCallbacks = () => {
4+
let callback;
5+
6+
while ((callback = callbacks.shift())) {
7+
callback();
8+
}
9+
};
10+
11+
const registerCallback = (callback) => {
12+
callbacks.push(callback);
13+
};
14+
15+
export const loadScript = (callback) => {
16+
const embedJs = '//editor.unlayer.com/embed.js?2';
17+
const scripts = document.querySelectorAll('script');
18+
let scriptLoaded = false;
19+
20+
scripts.forEach((script) => {
21+
if (script.src.includes(embedJs)) {
22+
scriptLoaded = true;
23+
}
24+
});
25+
26+
registerCallback(callback);
27+
28+
if (!scriptLoaded) {
29+
const unlayerScript = document.createElement('script');
30+
unlayerScript.setAttribute('src', embedJs);
31+
unlayerScript.onload = () => {
32+
runCallbacks();
33+
};
34+
document.head.appendChild(unlayerScript);
35+
} else {
36+
runCallbacks();
37+
}
38+
};

0 commit comments

Comments
 (0)