@deepnoid/canvas 0.1.28 → 0.1.29

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.
@@ -11,11 +11,18 @@ export const useImageLoader = (src, imageRef, timeout = 10000, onLoadSuccess, on
11
11
  setError(null);
12
12
  return;
13
13
  }
14
- const controller = new AbortController();
15
- const { signal } = controller;
14
+ if (imageRef.current && imageRef.current.src === src) {
15
+ setImage(imageRef.current);
16
+ setIsLoading(false);
17
+ setError(null);
18
+ onLoadSuccess?.();
19
+ return;
20
+ }
16
21
  setIsLoading(true);
17
22
  setError(null);
18
23
  setImage(null);
24
+ const controller = new AbortController();
25
+ const { signal } = controller;
19
26
  const img = new Image();
20
27
  img.crossOrigin = 'anonymous';
21
28
  img.src = src;
@@ -28,44 +35,37 @@ export const useImageLoader = (src, imageRef, timeout = 10000, onLoadSuccess, on
28
35
  onLoadError?.(e);
29
36
  }
30
37
  }, timeout);
31
- const load = async () => {
32
- try {
33
- await img.decode();
34
- if (signal.aborted)
35
- return;
36
- if (img.naturalWidth === 0 || img.naturalHeight === 0) {
37
- throw new Error(`Invalid image dimensions: ${src}`);
38
- }
39
- try {
40
- imageRef.current = img;
41
- }
42
- catch { }
43
- setImage(img);
44
- setIsLoading(false);
45
- setError(null);
46
- onLoadSuccess?.();
47
- }
48
- catch (err) {
49
- if (signal.aborted)
50
- return;
51
- const e = new Error(`Failed to load image: ${src}`);
52
- setError(e.message);
53
- setIsLoading(false);
54
- onLoadError?.(err instanceof Error ? err : e);
55
- }
56
- finally {
57
- clearTimeout(timeoutId);
58
- }
38
+ const handleLoad = () => {
39
+ if (signal.aborted)
40
+ return;
41
+ clearTimeout(timeoutId);
42
+ imageRef.current = img;
43
+ setImage(img);
44
+ setIsLoading(false);
45
+ setError(null);
46
+ onLoadSuccess?.();
59
47
  };
60
- load();
48
+ const handleError = (e) => {
49
+ if (signal.aborted)
50
+ return;
51
+ clearTimeout(timeoutId);
52
+ const err = e instanceof Error ? e : new Error(`Failed to load image: ${src}`);
53
+ setError(err.message);
54
+ setIsLoading(false);
55
+ onLoadError?.(err);
56
+ };
57
+ img.onload = handleLoad;
58
+ img.onerror = handleError;
61
59
  return () => {
62
- controller.abort();
63
60
  clearTimeout(timeoutId);
61
+ controller.abort();
62
+ img.onload = null;
63
+ img.onerror = null;
64
64
  try {
65
65
  img.src = '';
66
66
  }
67
67
  catch { }
68
68
  };
69
- }, [src, timeout, onLoadError, onLoadSuccess, imageRef]);
69
+ }, [src, imageRef, timeout, onLoadSuccess, onLoadError]);
70
70
  return { image, isLoading, error };
71
71
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",