@linear_non/stellar-kit 1.1.4 → 1.1.5
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/classes/ImageLoader.js +53 -15
- package/package.json +1 -1
package/classes/ImageLoader.js
CHANGED
|
@@ -3,7 +3,11 @@ import { emitter, EVENTS } from "../events"
|
|
|
3
3
|
|
|
4
4
|
export default class ImageLoader {
|
|
5
5
|
static type = "images"
|
|
6
|
-
static events = {
|
|
6
|
+
static events = {
|
|
7
|
+
PROGRESS: EVENTS.APP_IMAGES_PROGRESS,
|
|
8
|
+
LOADED: EVENTS.APP_IMAGES_LOADED,
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
constructor({ origin, fileName, extension, total, urls = [] }) {
|
|
8
12
|
this.origin = origin
|
|
9
13
|
this.fileName = fileName
|
|
@@ -17,7 +21,6 @@ export default class ImageLoader {
|
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
async load() {
|
|
20
|
-
// If explicit URLs are passed, use them
|
|
21
24
|
const urls = this.urls.length
|
|
22
25
|
? this.urls
|
|
23
26
|
: Array.from({ length: this.total }, (_, i) => `${this.origin}/${this.fileName}${i}.${this.extension}`)
|
|
@@ -31,30 +34,65 @@ export default class ImageLoader {
|
|
|
31
34
|
emitter.emit(EVENTS.APP_IMAGES_LOADED, this.images)
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
// --- inline worker (classic) ---
|
|
38
|
+
createWorker() {
|
|
39
|
+
const code = `
|
|
40
|
+
self.onmessage = async (e) => {
|
|
41
|
+
const { url, index } = e.data;
|
|
42
|
+
try {
|
|
43
|
+
const res = await fetch(url);
|
|
44
|
+
const blob = await res.blob();
|
|
45
|
+
self.postMessage({ url, blob, index });
|
|
46
|
+
} catch (err) {
|
|
47
|
+
self.postMessage({ url, error: String(err), index });
|
|
48
|
+
}
|
|
49
|
+
};`
|
|
50
|
+
const blob = new Blob([code], { type: "application/javascript" })
|
|
51
|
+
const workerUrl = URL.createObjectURL(blob)
|
|
52
|
+
const w = new Worker(workerUrl) // classic worker (no imports)
|
|
53
|
+
// (optional) URL.revokeObjectURL(workerUrl) after creation; leaving as-is for safety
|
|
54
|
+
return w
|
|
55
|
+
}
|
|
56
|
+
|
|
34
57
|
loadSingle(url, index) {
|
|
35
58
|
return new Promise(resolve => {
|
|
36
|
-
const worker =
|
|
37
|
-
new URL("../workers/image-loader.js", import.meta.url),
|
|
38
|
-
{ type: "module" } // important
|
|
39
|
-
)
|
|
59
|
+
const worker = this.createWorker()
|
|
40
60
|
worker.postMessage({ url, index })
|
|
41
61
|
|
|
42
62
|
worker.addEventListener("message", e => {
|
|
43
|
-
const { blob, index } = e.data
|
|
44
|
-
|
|
45
|
-
img.src = URL.createObjectURL(blob)
|
|
46
|
-
|
|
47
|
-
img.onload = () => {
|
|
48
|
-
this.images[index] = img
|
|
49
|
-
this.loaded++
|
|
63
|
+
const { blob, index, error } = e.data
|
|
64
|
+
this.loaded++
|
|
50
65
|
|
|
66
|
+
if (!error && blob) {
|
|
67
|
+
const img = new Image()
|
|
68
|
+
img.src = URL.createObjectURL(blob)
|
|
69
|
+
img.onload = () => {
|
|
70
|
+
this.images[index] = img
|
|
71
|
+
emitter.emit(EVENTS.APP_IMAGES_PROGRESS, {
|
|
72
|
+
loaded: this.loaded,
|
|
73
|
+
total: this.total,
|
|
74
|
+
percent: Math.round((this.loaded / this.total) * 100),
|
|
75
|
+
})
|
|
76
|
+
resolve(img)
|
|
77
|
+
worker.terminate()
|
|
78
|
+
}
|
|
79
|
+
img.onerror = () => {
|
|
80
|
+
emitter.emit(EVENTS.APP_IMAGES_PROGRESS, {
|
|
81
|
+
loaded: this.loaded,
|
|
82
|
+
total: this.total,
|
|
83
|
+
percent: Math.round((this.loaded / this.total) * 100),
|
|
84
|
+
})
|
|
85
|
+
resolve(null)
|
|
86
|
+
worker.terminate()
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
// count progress even on error
|
|
51
90
|
emitter.emit(EVENTS.APP_IMAGES_PROGRESS, {
|
|
52
91
|
loaded: this.loaded,
|
|
53
92
|
total: this.total,
|
|
54
93
|
percent: Math.round((this.loaded / this.total) * 100),
|
|
55
94
|
})
|
|
56
|
-
|
|
57
|
-
resolve(img)
|
|
95
|
+
resolve(null)
|
|
58
96
|
worker.terminate()
|
|
59
97
|
}
|
|
60
98
|
})
|