@braintree/asset-loader 2.1.0 → 2.2.0
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/README.md +12 -5
- package/dist/load-script.js +70 -13
- package/dist/types.d.ts +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,17 +20,24 @@ loadScript({
|
|
|
20
20
|
// optional
|
|
21
21
|
foo: "value", // adds data-foo="value" to script tag
|
|
22
22
|
},
|
|
23
|
-
// optional, by default
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
// identical script tag already exists on the page.
|
|
23
|
+
// optional, by default a repeated call with the same options reuses the
|
|
24
|
+
// earlier load. Setting this to `true` always adds a new script tag and
|
|
25
|
+
// replaces the reused load for later calls with the same options.
|
|
27
26
|
forceScriptReload: true,
|
|
27
|
+
// optional, milliseconds to wait before giving up on a load that never
|
|
28
|
+
// finishes. There is no timeout by default. The script tag can't be
|
|
29
|
+
// cancelled, so a retry after a slow load may run the script twice.
|
|
30
|
+
timeout: 60000,
|
|
28
31
|
})
|
|
29
32
|
.then(function (script) {
|
|
30
33
|
script; // a reference to the dom node
|
|
31
34
|
})
|
|
32
35
|
.catch(function (err) {
|
|
33
|
-
// occurs when script fails to load or
|
|
36
|
+
// occurs when the script fails to load, is aborted or times out.
|
|
37
|
+
// err is an AssetLoadError with `src`, `failureKind` ("error", "abort"
|
|
38
|
+
// or "timeout"), `timing` (ms until failure) and `onLine`
|
|
39
|
+
// (navigator.onLine at failure). Calling loadScript again with the
|
|
40
|
+
// same options starts a new load.
|
|
34
41
|
});
|
|
35
42
|
```
|
|
36
43
|
|
package/dist/load-script.js
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
var scriptCache = {};
|
|
14
|
+
function canReuse(entry, forceReload) {
|
|
15
|
+
return !forceReload && entry.state !== "failed";
|
|
16
|
+
}
|
|
3
17
|
function buildLoadError(src, failureKind, startTime) {
|
|
4
|
-
var
|
|
18
|
+
var suffixes = {
|
|
19
|
+
abort: "has aborted.",
|
|
20
|
+
error: "failed to load.",
|
|
21
|
+
timeout: "timed out.",
|
|
22
|
+
};
|
|
23
|
+
var suffix = suffixes[failureKind];
|
|
5
24
|
var error = new Error("".concat(src, " ").concat(suffix));
|
|
6
25
|
error.src = src;
|
|
7
26
|
error.failureKind = failureKind;
|
|
@@ -10,13 +29,14 @@ function buildLoadError(src, failureKind, startTime) {
|
|
|
10
29
|
return error;
|
|
11
30
|
}
|
|
12
31
|
function loadScript(options) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
// forceScriptReload only controls whether the cache is read, so it must not
|
|
33
|
+
// be part of the key, otherwise a forced load can never replace the entry.
|
|
34
|
+
// timeout is left out too, so callers share the load that started first.
|
|
35
|
+
var forceScriptReload = options.forceScriptReload, timeout = options.timeout, cacheableOptions = __rest(options, ["forceScriptReload", "timeout"]);
|
|
36
|
+
var stringifiedOptions = JSON.stringify(cacheableOptions);
|
|
37
|
+
var existing = scriptCache[stringifiedOptions];
|
|
38
|
+
if (existing && canReuse(existing, forceScriptReload)) {
|
|
39
|
+
return existing.promise;
|
|
20
40
|
}
|
|
21
41
|
var script = document.createElement("script");
|
|
22
42
|
var attrs = options.dataAttributes || {};
|
|
@@ -36,23 +56,60 @@ function loadScript(options) {
|
|
|
36
56
|
Object.keys(attrs).forEach(function (key) {
|
|
37
57
|
script.setAttribute("data-".concat(key), "".concat(attrs[key]));
|
|
38
58
|
});
|
|
39
|
-
|
|
59
|
+
// Handlers only update their own entry and never touch the cache, so a
|
|
60
|
+
// forced reload replacing the cached entry can't be undone by an old load.
|
|
61
|
+
// State is set before settling so callers retrying from a catch see it.
|
|
62
|
+
var entry = { state: "pending" };
|
|
63
|
+
entry.promise = new Promise(function (resolve, reject) {
|
|
40
64
|
var startTime = performance.now();
|
|
65
|
+
var timedOut = false;
|
|
66
|
+
var timer;
|
|
67
|
+
// The first event decides the outcome; later ones can't override it.
|
|
41
68
|
script.addEventListener("load", function () {
|
|
69
|
+
if (timedOut) {
|
|
70
|
+
// A script can't be cancelled once inserted, so it may still finish.
|
|
71
|
+
// Fix the entry so the next call reuses it instead of adding a tag.
|
|
72
|
+
entry.state = "loaded";
|
|
73
|
+
entry.promise = Promise.resolve(script);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (entry.state !== "pending") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
entry.state = "loaded";
|
|
42
81
|
resolve(script);
|
|
43
82
|
});
|
|
44
83
|
script.addEventListener("error", function () {
|
|
84
|
+
if (entry.state !== "pending") {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
entry.state = "failed";
|
|
89
|
+
container.removeChild(script);
|
|
45
90
|
reject(buildLoadError(options.src, "error", startTime));
|
|
46
91
|
});
|
|
47
92
|
script.addEventListener("abort", function () {
|
|
93
|
+
if (entry.state !== "pending") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
entry.state = "failed";
|
|
48
98
|
reject(buildLoadError(options.src, "abort", startTime));
|
|
49
99
|
});
|
|
100
|
+
if (timeout) {
|
|
101
|
+
timer = setTimeout(function () {
|
|
102
|
+
timedOut = true;
|
|
103
|
+
entry.state = "failed";
|
|
104
|
+
reject(buildLoadError(options.src, "timeout", startTime));
|
|
105
|
+
}, timeout);
|
|
106
|
+
}
|
|
50
107
|
container.appendChild(script);
|
|
51
108
|
});
|
|
52
|
-
|
|
53
|
-
return
|
|
109
|
+
scriptCache[stringifiedOptions] = entry;
|
|
110
|
+
return entry.promise;
|
|
54
111
|
}
|
|
55
112
|
loadScript.clearCache = function () {
|
|
56
|
-
|
|
113
|
+
scriptCache = {};
|
|
57
114
|
};
|
|
58
115
|
module.exports = loadScript;
|
package/dist/types.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ export type LoadScriptOptions = {
|
|
|
5
5
|
forceScriptReload?: boolean;
|
|
6
6
|
integrity?: string;
|
|
7
7
|
src: string;
|
|
8
|
+
timeout?: number;
|
|
8
9
|
type?: string;
|
|
9
10
|
id?: string;
|
|
10
11
|
};
|
|
11
|
-
export type AssetLoadFailureKind = "error" | "abort";
|
|
12
|
+
export type AssetLoadFailureKind = "error" | "abort" | "timeout";
|
|
12
13
|
export type AssetLoadError = Error & {
|
|
13
14
|
src: string;
|
|
14
15
|
failureKind: AssetLoadFailureKind;
|