@colisweb/rescript-toolkit 5.29.6 → 5.30.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "5.29.6",
3
+ "version": "5.30.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -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
+ }
@@ -45,3 +45,4 @@ module DropdownMenu = Toolkit__Ui_DropdownMenu
45
45
  module ScrollArea = Toolkit__Ui_ScrollArea
46
46
  module PasswordRulesNotice = Toolkit__Ui_PasswordRulesNotice
47
47
  module SectionCard = Toolkit__Ui_SectionCard
48
+ module SuspenseImage = Toolkit__Ui_SuspenseImage
@@ -0,0 +1,2 @@
1
+ @module("@root/src/ui/SuspenseImage.jsx") @react.component
2
+ external make: (~src: string, ~className: string=?) => React.element = "default"