@colisweb/rescript-toolkit 5.29.6 → 5.30.1
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/package.json
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const cache = new Map();
|
|
2
|
+
|
|
3
|
+
function createResource(asyncFn) {
|
|
4
|
+
let status = "pending";
|
|
5
|
+
let result;
|
|
6
|
+
const promise = asyncFn().then(
|
|
7
|
+
(r) => {
|
|
8
|
+
status = "success";
|
|
9
|
+
result = r;
|
|
10
|
+
},
|
|
11
|
+
(e) => {
|
|
12
|
+
status = "error";
|
|
13
|
+
result = e;
|
|
14
|
+
},
|
|
15
|
+
);
|
|
16
|
+
return {
|
|
17
|
+
read() {
|
|
18
|
+
switch (status) {
|
|
19
|
+
case "pending":
|
|
20
|
+
throw promise;
|
|
21
|
+
case "error":
|
|
22
|
+
throw result;
|
|
23
|
+
case "success":
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function loadImage(source) {
|
|
30
|
+
let resource = cache.get(source);
|
|
31
|
+
if (resource) return resource;
|
|
32
|
+
|
|
33
|
+
resource = createResource(
|
|
34
|
+
() =>
|
|
35
|
+
new Promise((resolve, reject) => {
|
|
36
|
+
const img = new window.Image();
|
|
37
|
+
img.src = source;
|
|
38
|
+
|
|
39
|
+
img.addEventListener("load", () => resolve(source));
|
|
40
|
+
img.addEventListener("error", () =>
|
|
41
|
+
reject(new Error(`Failed to load image ${source}`)),
|
|
42
|
+
);
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
cache.set(source, resource);
|
|
47
|
+
return resource;
|
|
48
|
+
}
|
|
49
|
+
export default function SuspenseImage(props) {
|
|
50
|
+
loadImage(props.src).read();
|
|
51
|
+
return <img {...props} />;
|
|
52
|
+
}
|
package/src/ui/Toolkit__Ui.res
CHANGED