@fi4f/hg 0.0.0 → 0.0.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/.github/workflows/publish.yml +25 -23
- package/package.json +27 -29
- package/src/asset.ts +248 -248
- package/src/event.ts +173 -173
- package/src/hg.ts +20 -20
- package/src/math.ts +4 -4
- package/src/matrix2.ts +214 -214
- package/src/matrix3.ts +301 -301
- package/src/matrix4.ts +414 -414
- package/src/scene.ts +44 -44
- package/src/stage.ts +481 -481
- package/src/vector2.ts +79 -79
- package/src/vector3.ts +87 -87
- package/src/vector4.ts +95 -95
- package/src/version.ts +25 -25
- package/tsconfig.json +44 -44
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
name: Publish Package
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
name: Publish Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
id-token: write # Required for OIDC
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: '24'
|
|
21
|
+
registry-url: 'https://registry.npmjs.org'
|
|
22
|
+
- run: npm ci
|
|
23
|
+
- run: npm run build --if-present
|
|
24
|
+
# - run: npm test
|
|
25
|
+
- run: npm publish
|
package/package.json
CHANGED
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@fi4f/hg",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"homepage": "https://github.com/fi4f/hg#readme",
|
|
6
|
-
"bugs": {
|
|
7
|
-
"url": "https://github.com/fi4f/hg/issues"
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/fi4f/hg.git"
|
|
12
|
-
},
|
|
13
|
-
"license": "ISC",
|
|
14
|
-
"author": "",
|
|
15
|
-
"type": "module",
|
|
16
|
-
"main"
|
|
17
|
-
"types": "dist/hg.d.ts",
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
20
|
-
"dev": "concurrently \"tsc --watch\" \"npx serve\"",
|
|
21
|
-
"build": "tsc"
|
|
22
|
-
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@fi4f/hg",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"homepage": "https://github.com/fi4f/hg#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/fi4f/hg/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/fi4f/hg.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"author": "",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "dist/hg.js",
|
|
17
|
+
"types": "dist/hg.d.ts",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
20
|
+
"dev": "concurrently \"tsc --watch\" \"npx serve\"",
|
|
21
|
+
"build": "tsc"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"concurrently": "^9.2.1",
|
|
25
|
+
"typescript": "^5.9.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/asset.ts
CHANGED
|
@@ -1,249 +1,249 @@
|
|
|
1
|
-
export const IMAGE = "image" as const;
|
|
2
|
-
export const AUDIO = "audio" as const;
|
|
3
|
-
export const TEXT = "text" as const;
|
|
4
|
-
export const BLOB = "blob" as const;
|
|
5
|
-
export const JSON = "json" as const;
|
|
6
|
-
|
|
7
|
-
export type IMAGE = typeof IMAGE;
|
|
8
|
-
export type AUDIO = typeof AUDIO;
|
|
9
|
-
export type TEXT = typeof TEXT ;
|
|
10
|
-
export type BLOB = typeof BLOB ;
|
|
11
|
-
export type JSON = typeof JSON ;
|
|
12
|
-
|
|
13
|
-
export type Kind = IMAGE | AUDIO | TEXT | BLOB | JSON;
|
|
14
|
-
|
|
15
|
-
export type Asset<T extends Kind> = { kind: T, path: string, id ?: string | undefined }
|
|
16
|
-
export namespace Asset {
|
|
17
|
-
export type Image = Asset<IMAGE>
|
|
18
|
-
export type Audio = Asset<AUDIO>
|
|
19
|
-
export type Text = Asset<TEXT >
|
|
20
|
-
export type Blob = Asset<BLOB >
|
|
21
|
-
export type Json = Asset<JSON >
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type Cache = {
|
|
25
|
-
images: { [id: string]: HTMLImageElement }
|
|
26
|
-
audios: { [id: string]: HTMLAudioElement }
|
|
27
|
-
texts: { [id: string]: string }
|
|
28
|
-
blobs: { [id: string]: globalThis.Blob }
|
|
29
|
-
jsons: { [id: string]: any }
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function load(a: Asset<any>) {
|
|
33
|
-
switch (a.kind) {
|
|
34
|
-
case IMAGE: return loadImage(a);
|
|
35
|
-
case AUDIO: return loadAudio(a);
|
|
36
|
-
case TEXT: return loadText (a);
|
|
37
|
-
case BLOB: return loadBlob (a);
|
|
38
|
-
case JSON: return loadJson (a);
|
|
39
|
-
default: throw `[Asset.load]: Unknown kind of asset '${a.kind}'`;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function loadImage(a: Asset.Image) {
|
|
44
|
-
return new Promise<HTMLImageElement>((res, rej) => {
|
|
45
|
-
const image = new Image();
|
|
46
|
-
image.onload = () => res(image);
|
|
47
|
-
image.onerror = () => rej( );
|
|
48
|
-
image.src = a.path;
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function loadAudio(a: Asset.Audio) {
|
|
53
|
-
return new Promise<HTMLAudioElement>((res, rej) => {
|
|
54
|
-
const audio = new Audio();
|
|
55
|
-
audio.onload = () => res(audio);
|
|
56
|
-
audio.onerror = () => rej( );
|
|
57
|
-
audio.src = a.path;
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export async function loadText(a: Asset.Text) {
|
|
62
|
-
return fetch(a.path).then(res => res.text())
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export async function loadBlob(a: Asset.Blob) {
|
|
66
|
-
return fetch(a.path).then(res => res.blob())
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export async function loadJson(a: Asset.Json) {
|
|
70
|
-
return fetch(a.path).then(res => res.json())
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export const Asset = {
|
|
74
|
-
new<T extends Kind>(kind: T, path: string, id ?: string) {
|
|
75
|
-
return { kind, path, id } satisfies Asset<T>
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
Image(path: string, id ?: string) {
|
|
79
|
-
return Asset.new(IMAGE, path, id) satisfies Asset.Image
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
Audio(path: string, id ?: string) {
|
|
83
|
-
return Asset.new(AUDIO, path, id) satisfies Asset.Audio
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
Text(path: string, id ?: string) {
|
|
87
|
-
return Asset.new(TEXT, path, id) satisfies Asset.Text
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
Blob(path: string, id ?: string) {
|
|
91
|
-
return Asset.new(BLOB, path, id) satisfies Asset.Blob
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
Json(path: string, id ?: string) {
|
|
95
|
-
return Asset.new(JSON, path, id) satisfies Asset.Json
|
|
96
|
-
},
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export const Cache = {
|
|
100
|
-
new() {
|
|
101
|
-
return {
|
|
102
|
-
images: {},
|
|
103
|
-
audios: {},
|
|
104
|
-
texts: {},
|
|
105
|
-
blobs: {},
|
|
106
|
-
jsons: {},
|
|
107
|
-
} satisfies Cache
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
getImage(cache: Cache, id: string) {
|
|
111
|
-
if (!(id in cache.images))
|
|
112
|
-
throw `[Cache.getImage]: Image with id '${id}' does not exist`;
|
|
113
|
-
return cache.images[id];
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
getAudio(cache: Cache, id: string) {
|
|
117
|
-
if (!(id in cache.audios))
|
|
118
|
-
throw `[Cache.getAudio]: Audio with id '${id}' does not exist`;
|
|
119
|
-
return cache.audios[id];
|
|
120
|
-
},
|
|
121
|
-
|
|
122
|
-
getText(cache: Cache, id: string) {
|
|
123
|
-
if (!(id in cache.texts))
|
|
124
|
-
throw `[Cache.getText]: Text with id '${id}' does not exist`;
|
|
125
|
-
return cache.texts[id];
|
|
126
|
-
},
|
|
127
|
-
|
|
128
|
-
getBlob(cache: Cache, id: string) {
|
|
129
|
-
if (!(id in cache.blobs))
|
|
130
|
-
throw `[Cache.getBlob]: Blob with id '${id}' does not exist`;
|
|
131
|
-
return cache.blobs[id];
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
getJson(cache: Cache, id: string) {
|
|
135
|
-
if(!(id in cache.jsons))
|
|
136
|
-
throw `[Cache.getJson]: Json with id '${id}' does not exist`;
|
|
137
|
-
return cache.jsons[id];
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
putImage(cache: Cache, id: string, image: HTMLImageElement) {
|
|
141
|
-
if (id in cache.images)
|
|
142
|
-
console.warn(`[Cache.putImage]: Image with id '${id}' already exists`);
|
|
143
|
-
return cache.images[id] = image;
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
putAudio(cache: Cache, id: string, audio: HTMLAudioElement) {
|
|
147
|
-
if (id in cache.audios)
|
|
148
|
-
console.warn(`[Cache.putAudio]: Audio with id '${id}' already exists`);
|
|
149
|
-
return cache.audios[id] = audio;
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
putText(cache: Cache, id: string, text: string) {
|
|
153
|
-
if (id in cache.texts)
|
|
154
|
-
console.warn(`[Cache.putText]: Text with id '${id}' already exists`);
|
|
155
|
-
return cache.texts[id] = text;
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
putBlob(cache: Cache, id: string, blob: Blob) {
|
|
159
|
-
if (id in cache.blobs)
|
|
160
|
-
console.warn(`[Cache.putBlob]: Blob with id '${id}' already exists`);
|
|
161
|
-
return cache.blobs[id] = blob;
|
|
162
|
-
},
|
|
163
|
-
|
|
164
|
-
putJson(cache: Cache, id: string, json: any) {
|
|
165
|
-
if (id in cache.jsons)
|
|
166
|
-
console.warn(`[Cache.putJson]: Json with id '${id}' already exists`);
|
|
167
|
-
return cache.jsons[id] = json;
|
|
168
|
-
},
|
|
169
|
-
|
|
170
|
-
freeImage(cache: Cache, id: string) {
|
|
171
|
-
delete cache.images[id];
|
|
172
|
-
},
|
|
173
|
-
|
|
174
|
-
freeAudio(cache: Cache, id: string) {
|
|
175
|
-
delete cache.audios[id];
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
freeText(cache: Cache, id: string) {
|
|
179
|
-
delete cache.texts[id];
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
freeBlob(cache: Cache, id: string) {
|
|
183
|
-
delete cache.blobs[id];
|
|
184
|
-
},
|
|
185
|
-
|
|
186
|
-
freeJson(cache: Cache, id: string) {
|
|
187
|
-
delete cache.jsons[id];
|
|
188
|
-
},
|
|
189
|
-
|
|
190
|
-
freeAll(cache: Cache) {
|
|
191
|
-
cache.images = {};
|
|
192
|
-
cache.audios = {};
|
|
193
|
-
cache.texts = {};
|
|
194
|
-
cache.blobs = {};
|
|
195
|
-
cache.jsons = {};
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
load(cache: Cache, a: Asset<any>) {
|
|
199
|
-
switch (a.kind) {
|
|
200
|
-
case IMAGE: return Cache.loadImage(cache, a);
|
|
201
|
-
case AUDIO: return Cache.loadAudio(cache, a);
|
|
202
|
-
case TEXT: return Cache.loadText (cache, a);
|
|
203
|
-
case BLOB: return Cache.loadBlob (cache, a);
|
|
204
|
-
case JSON: return Cache.loadJson (cache, a);
|
|
205
|
-
default: throw `[Cache.load]: Unknown kind of asset '${a.kind}'`;
|
|
206
|
-
}
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
async loadImage(cache: Cache, a: Asset.Image) {
|
|
210
|
-
const id = a.id ?? uniqueId(cache.images);
|
|
211
|
-
Cache.putImage(cache, id, await loadImage(a));
|
|
212
|
-
return id;
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
async loadAudio(cache: Cache, a: Asset.Audio) {
|
|
216
|
-
const id = a.id ?? uniqueId(cache.audios);
|
|
217
|
-
Cache.putAudio(cache, id, await loadAudio(a));
|
|
218
|
-
return id;
|
|
219
|
-
},
|
|
220
|
-
|
|
221
|
-
async loadText(cache: Cache, a: Asset.Text) {
|
|
222
|
-
const id = a.id ?? uniqueId(cache.texts );
|
|
223
|
-
Cache.putText(cache, id, await loadText(a));
|
|
224
|
-
return id;
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
async loadBlob(cache: Cache, a: Asset.Blob) {
|
|
228
|
-
const id = a.id ?? uniqueId(cache.blobs );
|
|
229
|
-
Cache.putBlob(cache, id, await loadBlob(a));
|
|
230
|
-
return id;
|
|
231
|
-
},
|
|
232
|
-
|
|
233
|
-
async loadJson(cache: Cache, a: Asset.Json) {
|
|
234
|
-
const id = a.id ?? uniqueId(cache.jsons );
|
|
235
|
-
Cache.putJson(cache, id, await loadJson(a));
|
|
236
|
-
return id;
|
|
237
|
-
},
|
|
238
|
-
|
|
239
|
-
async loadAll(cache: Cache, a: Array<Asset<any>>) {
|
|
240
|
-
return Promise.all(a.map(a => Cache.load(cache, a)));
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function uniqueId(ids: {[id: string]: any}) {
|
|
245
|
-
let id = crypto.randomUUID();
|
|
246
|
-
while (id in ids)
|
|
247
|
-
id = crypto.randomUUID();
|
|
248
|
-
return id;
|
|
1
|
+
export const IMAGE = "image" as const;
|
|
2
|
+
export const AUDIO = "audio" as const;
|
|
3
|
+
export const TEXT = "text" as const;
|
|
4
|
+
export const BLOB = "blob" as const;
|
|
5
|
+
export const JSON = "json" as const;
|
|
6
|
+
|
|
7
|
+
export type IMAGE = typeof IMAGE;
|
|
8
|
+
export type AUDIO = typeof AUDIO;
|
|
9
|
+
export type TEXT = typeof TEXT ;
|
|
10
|
+
export type BLOB = typeof BLOB ;
|
|
11
|
+
export type JSON = typeof JSON ;
|
|
12
|
+
|
|
13
|
+
export type Kind = IMAGE | AUDIO | TEXT | BLOB | JSON;
|
|
14
|
+
|
|
15
|
+
export type Asset<T extends Kind> = { kind: T, path: string, id ?: string | undefined }
|
|
16
|
+
export namespace Asset {
|
|
17
|
+
export type Image = Asset<IMAGE>
|
|
18
|
+
export type Audio = Asset<AUDIO>
|
|
19
|
+
export type Text = Asset<TEXT >
|
|
20
|
+
export type Blob = Asset<BLOB >
|
|
21
|
+
export type Json = Asset<JSON >
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type Cache = {
|
|
25
|
+
images: { [id: string]: HTMLImageElement }
|
|
26
|
+
audios: { [id: string]: HTMLAudioElement }
|
|
27
|
+
texts: { [id: string]: string }
|
|
28
|
+
blobs: { [id: string]: globalThis.Blob }
|
|
29
|
+
jsons: { [id: string]: any }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function load(a: Asset<any>) {
|
|
33
|
+
switch (a.kind) {
|
|
34
|
+
case IMAGE: return loadImage(a);
|
|
35
|
+
case AUDIO: return loadAudio(a);
|
|
36
|
+
case TEXT: return loadText (a);
|
|
37
|
+
case BLOB: return loadBlob (a);
|
|
38
|
+
case JSON: return loadJson (a);
|
|
39
|
+
default: throw `[Asset.load]: Unknown kind of asset '${a.kind}'`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function loadImage(a: Asset.Image) {
|
|
44
|
+
return new Promise<HTMLImageElement>((res, rej) => {
|
|
45
|
+
const image = new Image();
|
|
46
|
+
image.onload = () => res(image);
|
|
47
|
+
image.onerror = () => rej( );
|
|
48
|
+
image.src = a.path;
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function loadAudio(a: Asset.Audio) {
|
|
53
|
+
return new Promise<HTMLAudioElement>((res, rej) => {
|
|
54
|
+
const audio = new Audio();
|
|
55
|
+
audio.onload = () => res(audio);
|
|
56
|
+
audio.onerror = () => rej( );
|
|
57
|
+
audio.src = a.path;
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function loadText(a: Asset.Text) {
|
|
62
|
+
return fetch(a.path).then(res => res.text())
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function loadBlob(a: Asset.Blob) {
|
|
66
|
+
return fetch(a.path).then(res => res.blob())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function loadJson(a: Asset.Json) {
|
|
70
|
+
return fetch(a.path).then(res => res.json())
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const Asset = {
|
|
74
|
+
new<T extends Kind>(kind: T, path: string, id ?: string) {
|
|
75
|
+
return { kind, path, id } satisfies Asset<T>
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
Image(path: string, id ?: string) {
|
|
79
|
+
return Asset.new(IMAGE, path, id) satisfies Asset.Image
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
Audio(path: string, id ?: string) {
|
|
83
|
+
return Asset.new(AUDIO, path, id) satisfies Asset.Audio
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
Text(path: string, id ?: string) {
|
|
87
|
+
return Asset.new(TEXT, path, id) satisfies Asset.Text
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
Blob(path: string, id ?: string) {
|
|
91
|
+
return Asset.new(BLOB, path, id) satisfies Asset.Blob
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
Json(path: string, id ?: string) {
|
|
95
|
+
return Asset.new(JSON, path, id) satisfies Asset.Json
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const Cache = {
|
|
100
|
+
new() {
|
|
101
|
+
return {
|
|
102
|
+
images: {},
|
|
103
|
+
audios: {},
|
|
104
|
+
texts: {},
|
|
105
|
+
blobs: {},
|
|
106
|
+
jsons: {},
|
|
107
|
+
} satisfies Cache
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
getImage(cache: Cache, id: string) {
|
|
111
|
+
if (!(id in cache.images))
|
|
112
|
+
throw `[Cache.getImage]: Image with id '${id}' does not exist`;
|
|
113
|
+
return cache.images[id];
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
getAudio(cache: Cache, id: string) {
|
|
117
|
+
if (!(id in cache.audios))
|
|
118
|
+
throw `[Cache.getAudio]: Audio with id '${id}' does not exist`;
|
|
119
|
+
return cache.audios[id];
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
getText(cache: Cache, id: string) {
|
|
123
|
+
if (!(id in cache.texts))
|
|
124
|
+
throw `[Cache.getText]: Text with id '${id}' does not exist`;
|
|
125
|
+
return cache.texts[id];
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
getBlob(cache: Cache, id: string) {
|
|
129
|
+
if (!(id in cache.blobs))
|
|
130
|
+
throw `[Cache.getBlob]: Blob with id '${id}' does not exist`;
|
|
131
|
+
return cache.blobs[id];
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
getJson(cache: Cache, id: string) {
|
|
135
|
+
if(!(id in cache.jsons))
|
|
136
|
+
throw `[Cache.getJson]: Json with id '${id}' does not exist`;
|
|
137
|
+
return cache.jsons[id];
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
putImage(cache: Cache, id: string, image: HTMLImageElement) {
|
|
141
|
+
if (id in cache.images)
|
|
142
|
+
console.warn(`[Cache.putImage]: Image with id '${id}' already exists`);
|
|
143
|
+
return cache.images[id] = image;
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
putAudio(cache: Cache, id: string, audio: HTMLAudioElement) {
|
|
147
|
+
if (id in cache.audios)
|
|
148
|
+
console.warn(`[Cache.putAudio]: Audio with id '${id}' already exists`);
|
|
149
|
+
return cache.audios[id] = audio;
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
putText(cache: Cache, id: string, text: string) {
|
|
153
|
+
if (id in cache.texts)
|
|
154
|
+
console.warn(`[Cache.putText]: Text with id '${id}' already exists`);
|
|
155
|
+
return cache.texts[id] = text;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
putBlob(cache: Cache, id: string, blob: Blob) {
|
|
159
|
+
if (id in cache.blobs)
|
|
160
|
+
console.warn(`[Cache.putBlob]: Blob with id '${id}' already exists`);
|
|
161
|
+
return cache.blobs[id] = blob;
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
putJson(cache: Cache, id: string, json: any) {
|
|
165
|
+
if (id in cache.jsons)
|
|
166
|
+
console.warn(`[Cache.putJson]: Json with id '${id}' already exists`);
|
|
167
|
+
return cache.jsons[id] = json;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
freeImage(cache: Cache, id: string) {
|
|
171
|
+
delete cache.images[id];
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
freeAudio(cache: Cache, id: string) {
|
|
175
|
+
delete cache.audios[id];
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
freeText(cache: Cache, id: string) {
|
|
179
|
+
delete cache.texts[id];
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
freeBlob(cache: Cache, id: string) {
|
|
183
|
+
delete cache.blobs[id];
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
freeJson(cache: Cache, id: string) {
|
|
187
|
+
delete cache.jsons[id];
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
freeAll(cache: Cache) {
|
|
191
|
+
cache.images = {};
|
|
192
|
+
cache.audios = {};
|
|
193
|
+
cache.texts = {};
|
|
194
|
+
cache.blobs = {};
|
|
195
|
+
cache.jsons = {};
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
load(cache: Cache, a: Asset<any>) {
|
|
199
|
+
switch (a.kind) {
|
|
200
|
+
case IMAGE: return Cache.loadImage(cache, a);
|
|
201
|
+
case AUDIO: return Cache.loadAudio(cache, a);
|
|
202
|
+
case TEXT: return Cache.loadText (cache, a);
|
|
203
|
+
case BLOB: return Cache.loadBlob (cache, a);
|
|
204
|
+
case JSON: return Cache.loadJson (cache, a);
|
|
205
|
+
default: throw `[Cache.load]: Unknown kind of asset '${a.kind}'`;
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
async loadImage(cache: Cache, a: Asset.Image) {
|
|
210
|
+
const id = a.id ?? uniqueId(cache.images);
|
|
211
|
+
Cache.putImage(cache, id, await loadImage(a));
|
|
212
|
+
return id;
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
async loadAudio(cache: Cache, a: Asset.Audio) {
|
|
216
|
+
const id = a.id ?? uniqueId(cache.audios);
|
|
217
|
+
Cache.putAudio(cache, id, await loadAudio(a));
|
|
218
|
+
return id;
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
async loadText(cache: Cache, a: Asset.Text) {
|
|
222
|
+
const id = a.id ?? uniqueId(cache.texts );
|
|
223
|
+
Cache.putText(cache, id, await loadText(a));
|
|
224
|
+
return id;
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
async loadBlob(cache: Cache, a: Asset.Blob) {
|
|
228
|
+
const id = a.id ?? uniqueId(cache.blobs );
|
|
229
|
+
Cache.putBlob(cache, id, await loadBlob(a));
|
|
230
|
+
return id;
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
async loadJson(cache: Cache, a: Asset.Json) {
|
|
234
|
+
const id = a.id ?? uniqueId(cache.jsons );
|
|
235
|
+
Cache.putJson(cache, id, await loadJson(a));
|
|
236
|
+
return id;
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
async loadAll(cache: Cache, a: Array<Asset<any>>) {
|
|
240
|
+
return Promise.all(a.map(a => Cache.load(cache, a)));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function uniqueId(ids: {[id: string]: any}) {
|
|
245
|
+
let id = crypto.randomUUID();
|
|
246
|
+
while (id in ids)
|
|
247
|
+
id = crypto.randomUUID();
|
|
248
|
+
return id;
|
|
249
249
|
}
|