argous 1.2.204 → 1.2.205

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.
@@ -1,161 +0,0 @@
1
- import { uuid } from './Utils';
2
-
3
- export type CallbackFn = () => void;
4
-
5
- export interface ScriptItem {
6
- src: string;
7
- async?: boolean;
8
- defer?: boolean;
9
- }
10
-
11
- interface Id {
12
- id: string;
13
- }
14
-
15
- const injectScriptTag = (doc: Document, item: ScriptItem & Id, handler: (id: string, err?: unknown) => void) => {
16
- const scriptTag = doc.createElement('script');
17
- scriptTag.referrerPolicy = 'origin';
18
- scriptTag.type = 'application/javascript';
19
- scriptTag.id = item.id;
20
- scriptTag.src = item.src;
21
- scriptTag.async = item.async ?? false;
22
- scriptTag.defer = item.defer ?? false;
23
-
24
- const loadHandler = () => {
25
- scriptTag.removeEventListener('load', loadHandler);
26
- scriptTag.removeEventListener('error', errorHandler);
27
- handler(item.src);
28
- };
29
- const errorHandler = (err: unknown) => {
30
- scriptTag.removeEventListener('load', loadHandler);
31
- scriptTag.removeEventListener('error', errorHandler);
32
- handler(item.src, err);
33
- };
34
-
35
- scriptTag.addEventListener('load', loadHandler);
36
- scriptTag.addEventListener('error', errorHandler);
37
-
38
- if (doc.head) {
39
- doc.head.appendChild(scriptTag);
40
- }
41
- };
42
-
43
- interface ScriptState {
44
- id: string;
45
- src: string;
46
- done: boolean;
47
- error?: unknown;
48
- handlers: ((src: string, err?: unknown) => void)[];
49
- }
50
-
51
- const createDocumentScriptLoader = (doc: Document) => {
52
- let lookup: Record<string, ScriptState> = {};
53
-
54
- const scriptLoadOrErrorHandler = (src: string, err?: unknown) => {
55
- const item = lookup[src];
56
- item.done = true;
57
- item.error = err;
58
- for (const h of item.handlers) {
59
- h(src, err);
60
- }
61
- item.handlers = [];
62
- };
63
-
64
- const loadScripts = (items: ScriptItem[], success: () => void, failure?: (err: unknown) => void) => {
65
- // eslint-disable-next-line no-console
66
- const failureOrLog = (err: unknown) => failure !== undefined ? failure(err) : console.error(err);
67
- if (items.length === 0) {
68
- failureOrLog(new Error('At least one script must be provided'));
69
- return;
70
- }
71
- let successCount = 0;
72
- let failed = false;
73
- const loaded = (_src: string, err?: unknown) => {
74
- if (failed) {
75
- return;
76
- }
77
- if (err) {
78
- failed = true;
79
- failureOrLog(err);
80
- } else if (++successCount === items.length) {
81
- success();
82
- }
83
- };
84
- for (const item of items) {
85
- const existing = lookup[item.src];
86
- if (existing) {
87
- if (existing.done) {
88
- loaded(item.src, existing.error);
89
- } else {
90
- existing.handlers.push(loaded);
91
- }
92
- } else {
93
- // create a new entry
94
- const id = uuid('tiny-');
95
- lookup[item.src] = {
96
- id,
97
- src: item.src,
98
- done: false,
99
- error: null,
100
- handlers: [loaded],
101
- };
102
- injectScriptTag(doc, { id, ...item }, scriptLoadOrErrorHandler);
103
- }
104
- }
105
- };
106
-
107
- const deleteScripts = () => {
108
- for (const item of Object.values(lookup)) {
109
- const scriptTag = doc.getElementById(item.id);
110
- if (scriptTag != null && scriptTag.tagName === 'SCRIPT') {
111
- scriptTag.parentNode?.removeChild(scriptTag);
112
- }
113
- }
114
- lookup = {};
115
- };
116
-
117
- const getDocument = () => doc;
118
-
119
- return {
120
- loadScripts,
121
- deleteScripts,
122
- getDocument
123
- };
124
- };
125
-
126
- type DocumentScriptLoader = ReturnType<typeof createDocumentScriptLoader>;
127
-
128
- const createScriptLoader = () => {
129
- const cache: DocumentScriptLoader[] = [];
130
-
131
- const getDocumentScriptLoader = (doc: Document) => {
132
- let loader = cache.find((l) => l.getDocument() === doc);
133
- if (loader === undefined) {
134
- loader = createDocumentScriptLoader(doc);
135
- cache.push(loader);
136
- }
137
- return loader;
138
- };
139
-
140
- const loadList = (doc: Document, items: ScriptItem[], delay: number, success: () => void, failure?: (err: unknown) => void) => {
141
- const doLoad = () => getDocumentScriptLoader(doc).loadScripts(items, success, failure);
142
- if (delay > 0) {
143
- setTimeout(doLoad, delay);
144
- } else {
145
- doLoad();
146
- }
147
- };
148
-
149
- const reinitialize = () => {
150
- for (let loader = cache.pop(); loader != null; loader = cache.pop()) {
151
- loader.deleteScripts();
152
- }
153
- };
154
-
155
- return {
156
- loadList,
157
- reinitialize
158
- };
159
- };
160
-
161
- export const ScriptLoader = createScriptLoader();