Skip to content

Commit 3d28dc2

Browse files
committed
feat: add loadScriptOnce, loadStylesOnce helper functions
1 parent 8d344a9 commit 3d28dc2

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/lib/functions/loadOnce.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const loadScript = (url: string) =>
2+
new Promise<Event>((resolve, reject) => {
3+
const script = document.createElement('script');
4+
script.src = url;
5+
script.async = true;
6+
script.onload = resolve;
7+
script.onerror = reject;
8+
document.head.appendChild(script);
9+
});
10+
11+
export const loadScriptOnce = (() => {
12+
const loaded = [];
13+
return async function (url) {
14+
if (!loaded.includes(url)) {
15+
await loadScript(url);
16+
loaded.push(url);
17+
return true;
18+
} else {
19+
return true;
20+
}
21+
};
22+
})();
23+
24+
const loadStyles = (url: string) =>
25+
new Promise<Event>((resolve, reject) => {
26+
const link = document.createElement('link');
27+
link.rel = 'stylesheet';
28+
link.href = url;
29+
link.onload = resolve;
30+
link.onerror = reject;
31+
document.head.appendChild(link);
32+
});
33+
34+
export const loadStylesOnce = (() => {
35+
let loaded = [];
36+
return async function (url) {
37+
if (!loaded.includes(url)) {
38+
await loadStyles(url);
39+
loaded = [url];
40+
return true;
41+
} else {
42+
return true;
43+
}
44+
};
45+
})();

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export { default as Store } from './functions/Store.svelte';
1919
export { default as ReactiveSet } from './functions/ReactiveSet.svelte';
2020
export { default as IntersectionObserver } from './functions/IntersectionObserver.svelte';
2121
export { default as IntersectionObserverShared } from './functions/IntersectionObserverShared.svelte';
22+
export { loadScriptOnce, loadStylesOnce } from './functions/loadOnce.js';
2223

2324
export { default as QrCode } from './media/QrCode.svelte';
2425

src/lib/stores/query-param-store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ function parse(value: string) {
3434
}
3535
}
3636

37-
export function createQueryParamStore<T>(opts: QueryParamStoreOptions<T>) {
38-
const { key, log, persist, startWith, cleanFalseValues } = opts;
39-
const replaceState = typeof opts.replaceState === 'undefined' ? true : opts.replaceState;
40-
const storageKey = `${opts.storagePrefix || ''}${key}`
37+
export function createQueryParamStore<T>(options: QueryParamStoreOptions<T>) {
38+
const { key, log, persist, startWith, cleanFalseValues } = options;
39+
const replaceState = typeof options.replaceState === 'undefined' ? true : options.replaceState;
40+
const storageKey = `${options.storagePrefix || ''}${key}`
4141

4242
let storage: Storage = undefined
4343
if (typeof window !== 'undefined') {

0 commit comments

Comments
 (0)