@gglib/content 0.6.2 → 0.8.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/LICENSE +7 -0
- package/Readme.md +1 -1
- package/dist/bundles/content.esm.js +1050 -0
- package/dist/bundles/content.esm.js.map +1 -0
- package/dist/content/src/AssetContainer.d.ts +18 -0
- package/dist/content/src/AssetContainer.d.ts.map +1 -0
- package/dist/content/src/AssetContainer.js +30 -0
- package/dist/content/src/AssetContainer.js.map +1 -0
- package/dist/content/src/AssetLoaderRegistry.d.ts +21 -0
- package/dist/content/src/AssetLoaderRegistry.d.ts.map +1 -0
- package/dist/content/src/AssetLoaderRegistry.js +28 -0
- package/dist/content/src/AssetLoaderRegistry.js.map +1 -0
- package/dist/content/src/AssetType.d.ts +13 -0
- package/dist/content/src/AssetType.d.ts.map +1 -0
- package/dist/content/src/AssetType.js +6 -0
- package/dist/content/src/AssetType.js.map +1 -0
- package/dist/content/src/AsyncExecutor.d.ts +63 -0
- package/dist/content/src/AsyncExecutor.d.ts.map +1 -0
- package/dist/content/src/AsyncExecutor.js +166 -0
- package/dist/content/src/AsyncExecutor.js.map +1 -0
- package/dist/content/src/ContentLoader.d.ts +266 -0
- package/dist/content/src/ContentLoader.d.ts.map +1 -0
- package/dist/content/src/ContentLoader.js +341 -0
- package/dist/content/src/ContentLoader.js.map +1 -0
- package/dist/content/src/ContentType.d.ts.map +1 -1
- package/dist/content/src/ContentType.js +6 -10
- package/dist/content/src/ContentType.js.map +1 -1
- package/dist/content/src/HttpClient.d.ts +48 -0
- package/dist/content/src/HttpClient.d.ts.map +1 -0
- package/dist/content/src/HttpClient.js +139 -0
- package/dist/content/src/HttpClient.js.map +1 -0
- package/dist/content/src/MaterialRegistry.d.ts +14 -0
- package/dist/content/src/MaterialRegistry.d.ts.map +1 -0
- package/dist/content/src/MaterialRegistry.js +24 -0
- package/dist/content/src/MaterialRegistry.js.map +1 -0
- package/dist/content/src/ResourceGraph.d.ts +54 -0
- package/dist/content/src/ResourceGraph.d.ts.map +1 -0
- package/dist/content/src/ResourceGraph.js +195 -0
- package/dist/content/src/ResourceGraph.js.map +1 -0
- package/dist/content/src/TextureLoader.d.ts +14 -0
- package/dist/content/src/TextureLoader.d.ts.map +1 -0
- package/dist/content/src/TextureLoader.js +70 -0
- package/dist/content/src/TextureLoader.js.map +1 -0
- package/dist/content/src/index.d.ts +8 -7
- package/dist/content/src/index.d.ts.map +1 -1
- package/dist/content/src/index.js +8 -7
- package/dist/content/src/index.js.map +1 -1
- package/dist/content/tsconfig.build.tsbuildinfo +1 -0
- package/dist/content/tsconfig.spec.tsbuildinfo +1 -0
- package/dist/stats.html +211 -170
- package/package.json +19 -8
- package/dist/bundles/content.umd.js +0 -713
- package/dist/bundles/content.umd.js.map +0 -1
- package/dist/content/src/ContentManager.d.ts +0 -153
- package/dist/content/src/ContentManager.d.ts.map +0 -1
- package/dist/content/src/ContentManager.js +0 -361
- package/dist/content/src/ContentManager.js.map +0 -1
- package/dist/content/src/ContentType.spec.d.ts +0 -2
- package/dist/content/src/ContentType.spec.d.ts.map +0 -1
- package/dist/content/src/ContentType.spec.js +0 -27
- package/dist/content/src/ContentType.spec.js.map +0 -1
- package/dist/content/src/Data.d.ts +0 -37
- package/dist/content/src/Data.d.ts.map +0 -1
- package/dist/content/src/Data.js +0 -34
- package/dist/content/src/Data.js.map +0 -1
- package/dist/content/src/Loader.d.ts +0 -69
- package/dist/content/src/Loader.d.ts.map +0 -1
- package/dist/content/src/Loader.js +0 -16
- package/dist/content/src/Loader.js.map +0 -1
- package/dist/content/src/Pipeline.d.ts +0 -38
- package/dist/content/src/Pipeline.d.ts.map +0 -1
- package/dist/content/src/Pipeline.js +0 -182
- package/dist/content/src/Pipeline.js.map +0 -1
- package/dist/content/src/Pipeline.spec.d.ts +0 -2
- package/dist/content/src/Pipeline.spec.d.ts.map +0 -1
- package/dist/content/src/Pipeline.spec.js +0 -121
- package/dist/content/src/Pipeline.spec.js.map +0 -1
- package/dist/content/src/PipelineContext.d.ts +0 -40
- package/dist/content/src/PipelineContext.d.ts.map +0 -1
- package/dist/content/src/PipelineContext.js +0 -2
- package/dist/content/src/PipelineContext.js.map +0 -1
- package/dist/content/src/symbols.d.ts +0 -2
- package/dist/content/src/symbols.d.ts.map +0 -1
- package/dist/content/src/symbols.js +0 -2
- package/dist/content/src/symbols.js.map +0 -1
- package/dist/content/src/utils.d.ts +0 -17
- package/dist/content/src/utils.d.ts.map +0 -1
- package/dist/content/src/utils.js +0 -42
- package/dist/content/src/utils.js.map +0 -1
- package/dist/content/tsconfig.tsbuildinfo +0 -2773
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncExecutor.js","sourceRoot":"","sources":["../../../src/AsyncExecutor.ts"],"names":[],"mappings":";AAyBA,MAAM,cAAc,GAAG,IAAI,eAAe,EAAE,CAAA;AAC5C,MAAM,UAAU,GAAG,cAAc,CAAC,MAAM,CAAA;AAExC;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAC7B;;OAEG;IACU,GAAG,CAAI,IAAkB,EAAE,MAAoB;;YAC1D,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EAAE,CAAC;gBACpB,MAAM,UAAU,CAAC,MAAM,CAAC,CAAA;YAC1B,CAAC;YAED,OAAO,IAAI,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,UAAU,CAAC,CAAA;QACnC,CAAC;KAAA;IAED;;OAEG;IACU,MAAM,CAAI,KAA0B,EAAE,MAAoB;;YACrE,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EAAE,CAAC;gBACpB,MAAM,UAAU,CAAC,MAAM,CAAC,CAAA;YAC1B,CAAC;YAED,OAAO,OAAO,CAAC,GAAG,CAChB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;gBACjB,OAAO,IAAI,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,UA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ACtC,SAAQ;YACV,CAAC;YAED,KAAK,CAAC,OAAO,EAAE,CAAA;YACf,OAAM;QACR,CAAC;IACH,CAAC;CACF;AAED,SAAS,YAAY,CAAC,KAAiB;IACrC,IAAI,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QAClC,KAAK,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAC1D,CAAC;AACH,CAAC","sourcesContent":["/**\n * Async task executed by an AsyncExecutor.\n * Receives an AbortSignal that should be respected for cancellation.\n */\nexport type AsyncTask<T> = (signal: AbortSignal) => Promise<T>\n\n/**\n * Executes asynchronous tasks with a defined concurrency policy.\n * Implementations may run tasks immediately or apply scheduling/limiting.\n */\nexport interface AsyncExecutor {\n /**\n * Executes a single async task.\n * If a signal is provided and already aborted, the task is not started.\n */\n run<T>(task: AsyncTask<T>, signal?: AbortSignal): Promise<T>\n\n /**\n * Executes multiple async tasks.\n * Default behavior is fail-fast (Promise.all semantics).\n * If a signal is provided and already aborted, no tasks are started.\n */\n runAll<T>(tasks: Array<AsyncTask<T>>, signal: AbortSignal): Promise<T[]>\n}\n\nconst noopController = new AbortController()\nconst noopSignal = noopController.signal\n\n/**\n * Naive executor with no concurrency limits.\n * Tasks are executed immediately and in parallel.\n */\nexport class NaiveAsyncExecutor implements AsyncExecutor {\n /**\n * Executes a task immediately.\n */\n public async run<T>(task: AsyncTask<T>, signal?: AbortSignal): Promise<T> {\n if (signal?.aborted) {\n throw abortError(signal)\n }\n\n return task(signal ?? noopSignal)\n }\n\n /**\n * Executes all tasks in parallel using Promise.all.\n */\n public async runAll<T>(tasks: Array<AsyncTask<T>>, signal?: AbortSignal): Promise<T[]> {\n if (signal?.aborted) {\n throw abortError(signal)\n }\n\n return Promise.all(\n tasks.map((task) => {\n return task(signal ?? noopSignal)\n }),\n )\n }\n}\n\nfunction abortError(signal: AbortSignal): Error {\n if (signal.reason !== undefined) {\n return signal.reason\n }\n\n return new Error('Aborted')\n}\n\ntype QueueEntry = {\n resolve: () => void\n reject: (err: unknown) => void\n signal?: AbortSignal\n onAbort?: () => void\n}\n\n/**\n * Executes asynchronous tasks with bounded concurrency.\n * Ensures that no more than a fixed number of tasks run in parallel.\n */\nexport class BoundedAsyncExecutor implements AsyncExecutor {\n private activeCount: number = 0\n private pendingCount: number = 0\n\n private queue: QueueEntry[] = []\n\n private readonly maxConcurrent: number\n\n public constructor(maxConcurrent: number) {\n if (maxConcurrent <= 0) {\n throw new Error('AsyncLimiter: maxConcurrent must be > 0')\n }\n\n this.maxConcurrent = maxConcurrent\n }\n\n public get active(): number {\n return this.activeCount\n }\n\n public get pending(): number {\n return this.pendingCount\n }\n\n /**\n * Executes a single task subject to the concurrency limit.\n * If a signal is provided and already aborted, the task is not started.\n */\n public async run<T>(task: AsyncTask<T>, signal?: AbortSignal): Promise<T> {\n if (signal?.aborted) {\n throw abortError(signal)\n }\n\n if (this.activeCount >= this.maxConcurrent) {\n await this.enqueue(signal)\n } else {\n this.activeCount++\n }\n\n if (signal?.aborted) {\n throw abortError(signal)\n }\n\n // create a linked controller so we always pass a signal\n const controller = new AbortController()\n\n const abortHandler = () => {\n controller.abort(signal?.reason)\n }\n\n if (signal) {\n signal.addEventListener('abort', abortHandler, { once: true })\n }\n\n try {\n const result = await task(controller.signal)\n return result\n } catch (error) {\n throw error\n } finally {\n if (signal) {\n signal.removeEventListener('abort', abortHandler)\n }\n\n this.activeCount--\n this.dequeue()\n }\n }\n\n /**\n * Executes multiple tasks with bounded concurrency.\n * Each task is scheduled through the shared limiter,\n * ensuring global concurrency constraints are respected.\n */\n public async runAll<T>(tasks: Array<AsyncTask<T>>, signal?: AbortSignal): Promise<T[]> {\n if (signal?.aborted) {\n throw signal.reason ?? new Error('Aborted')\n }\n\n return Promise.all(\n tasks.map((task) => {\n return this.run(task, signal ?? noopSignal)\n }),\n )\n }\n\n private enqueue(signal?: AbortSignal): Promise<void> {\n this.pendingCount++\n\n return new Promise<void>((resolve, reject) => {\n const entry: QueueEntry = {\n resolve: () => {\n this.pendingCount--\n cleanupEntry(entry)\n resolve()\n },\n reject: (err: unknown) => {\n this.pendingCount--\n cleanupEntry(entry)\n reject(err)\n },\n signal,\n }\n\n if (signal) {\n const onAbort = () => {\n entry.reject(abortError(signal))\n }\n\n entry.onAbort = onAbort\n signal.addEventListener('abort', onAbort, { once: true })\n }\n\n this.queue.push(entry)\n })\n }\n\n private dequeue(): void {\n while (this.queue.length > 0 && this.activeCount < this.maxConcurrent) {\n const entry = this.queue.shift()\n\n if (!entry) {\n return\n }\n\n // if aborted while waiting, skip\n if (entry.signal?.aborted) {\n entry.reject(abortError(entry.signal))\n continue\n }\n\n entry.resolve()\n return\n }\n }\n}\n\nfunction cleanupEntry(entry: QueueEntry): void {\n if (entry.signal && entry.onAbort) {\n entry.signal.removeEventListener('abort', entry.onAbort)\n }\n}\n"]}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { Device, Material, MaterialOptions, ResourceScope, Texture, TextureOptions } from '@gglib/graphics';
|
|
2
|
+
import { Model, ModelOptions } from '@gglib/model';
|
|
3
|
+
import { AssetContainer } from './AssetContainer';
|
|
4
|
+
import { AssetLoader, AssetLoaderRegistry, LoaderByExtension, LoaderEntry, LoaderFactory } from './AssetLoaderRegistry';
|
|
5
|
+
import { AssetType } from './AssetType';
|
|
6
|
+
import { AsyncExecutor } from './AsyncExecutor';
|
|
7
|
+
import { HttpClient, HttpOptions, HttpResponse } from './HttpClient';
|
|
8
|
+
export interface LoadContext {
|
|
9
|
+
/**
|
|
10
|
+
* The content loader instance that started the load request.
|
|
11
|
+
*/
|
|
12
|
+
content: ContentLoader;
|
|
13
|
+
/**
|
|
14
|
+
* The base URL to use to resolve asset URLs.
|
|
15
|
+
*/
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Abort signal to cancel the load request.
|
|
19
|
+
*/
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
/**
|
|
22
|
+
* The type hint for the asset to load. This can be a file extension or a MIME type.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Use this if the type cannot be found from the URL.
|
|
26
|
+
*/
|
|
27
|
+
type?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The intended color space for the currently loaded texture.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* This tells the loader which color space to use for the currently loaded texture.
|
|
33
|
+
*
|
|
34
|
+
* Some loaders may ignore this.
|
|
35
|
+
*/
|
|
36
|
+
color?: ColorSpace;
|
|
37
|
+
/**
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
crossOrigin?: string;
|
|
41
|
+
}
|
|
42
|
+
export type ColorSpace = 'srgb' | 'linear';
|
|
43
|
+
export type LoadOptions = Omit<LoadContext, 'content'> & {
|
|
44
|
+
/**
|
|
45
|
+
* Index of the asset in the container, default is 0
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* Only used by {@link ContentLoader.load}, it is not passed on to the load context.
|
|
49
|
+
*/
|
|
50
|
+
index?: number;
|
|
51
|
+
/**
|
|
52
|
+
* The scope that should own the loaded instance
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Only used by {@link ContentLoader.load}, it is not passed on to the load context.
|
|
56
|
+
* Loading the same asset in multiple scopes returns the same instance, owned by all of them.
|
|
57
|
+
* Once the instance is finalized, it is removed from the cache and the next load creates a new one.
|
|
58
|
+
*/
|
|
59
|
+
scope?: ResourceScope | null;
|
|
60
|
+
};
|
|
61
|
+
export type AssetCreator<Options = any, Instance = any> = (content: ContentLoader, options: Options) => Instance;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the key under which {@link ContentLoader.load} caches an asset
|
|
64
|
+
*/
|
|
65
|
+
export type AssetCacheKey = (type: AssetType<any, any>, url: string, options?: LoadOptions) => string;
|
|
66
|
+
/**
|
|
67
|
+
* The default {@link AssetCacheKey}: the url, and the asset index if it is not 0
|
|
68
|
+
*/
|
|
69
|
+
export declare const defaultAssetCacheKey: AssetCacheKey;
|
|
70
|
+
export interface ResolveUriOptions {
|
|
71
|
+
/**
|
|
72
|
+
* The asset whose content references the URI (e.g. the .gltf referencing a texture), if any.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* Acts like the document URL of an HTML page: relative URIs resolve against its directory,
|
|
76
|
+
* unless a {@link ResolveUriOptions.baseUri} is given.
|
|
77
|
+
*/
|
|
78
|
+
parentUri?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Acts like the `<base href>` of an HTML page.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* When set, relative URIs resolve against it instead of the {@link ResolveUriOptions.parentUri}.
|
|
84
|
+
* Like `<base href>`, it resolves against its directory (`/foo/` vs `/foo`), root-absolute URIs
|
|
85
|
+
* keep only its origin, and a relative base itself resolves against the `parentUri`.
|
|
86
|
+
*/
|
|
87
|
+
baseUri?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Resolves an asset URI to the URL that is loaded
|
|
91
|
+
*/
|
|
92
|
+
export type UriResolver = (assetUri: string, options: ResolveUriOptions) => string;
|
|
93
|
+
/**
|
|
94
|
+
* The default {@link UriResolver}: resolves `assetUri` like a browser resolves a link,
|
|
95
|
+
* with `parentUri` as the document URL and `baseUri` as its `<base href>`
|
|
96
|
+
*/
|
|
97
|
+
export declare const defaultUriResolver: UriResolver;
|
|
98
|
+
export interface ContentLoaderOptions {
|
|
99
|
+
http?: HttpClient;
|
|
100
|
+
executor?: AsyncExecutor;
|
|
101
|
+
registry?: AssetLoaderRegistry;
|
|
102
|
+
disableCache?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Custom cache key for assets, e.g. to share assets with the same content across urls.
|
|
105
|
+
* Default is {@link defaultAssetCacheKey}
|
|
106
|
+
*/
|
|
107
|
+
cacheKey?: AssetCacheKey;
|
|
108
|
+
/**
|
|
109
|
+
* Custom URI resolver, e.g. to rewrite or redirect asset URLs.
|
|
110
|
+
* Default is {@link defaultUriResolver}
|
|
111
|
+
*/
|
|
112
|
+
resolveUri?: UriResolver;
|
|
113
|
+
}
|
|
114
|
+
export declare class ContentLoader {
|
|
115
|
+
/**
|
|
116
|
+
* Registry for loader types
|
|
117
|
+
*/
|
|
118
|
+
static loaders: AssetLoaderRegistry;
|
|
119
|
+
/**
|
|
120
|
+
* Registers a loader type
|
|
121
|
+
*/
|
|
122
|
+
static registerLoader(descriptor: LoaderEntry | LoaderByExtension): void;
|
|
123
|
+
/**
|
|
124
|
+
* Registry for asset creators
|
|
125
|
+
*/
|
|
126
|
+
static creators: Map<string, AssetCreator<any, any>>;
|
|
127
|
+
/**
|
|
128
|
+
* Registers a creator function for all content loaders
|
|
129
|
+
*/
|
|
130
|
+
static registerCreator<O, T>(type: AssetType<O, T>, factory: AssetCreator<O, T>): void;
|
|
131
|
+
/**
|
|
132
|
+
* The graphics device instance
|
|
133
|
+
*/
|
|
134
|
+
device: Device;
|
|
135
|
+
/**
|
|
136
|
+
* The http client instance
|
|
137
|
+
*/
|
|
138
|
+
http: HttpClient;
|
|
139
|
+
/**
|
|
140
|
+
* Loader registry that take precedence over the static {@link ContentLoader.loaders}
|
|
141
|
+
*/
|
|
142
|
+
loaders: AssetLoaderRegistry;
|
|
143
|
+
/**
|
|
144
|
+
* Async executor used only for leaf I/O tasks.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* The default implementation is {@link NaiveAsyncExecutor}.
|
|
148
|
+
* It runs all tasks immediately and does not limit concurrency.
|
|
149
|
+
*
|
|
150
|
+
* Use this executor only for leaf-level operations like
|
|
151
|
+
* - network requests
|
|
152
|
+
* - texture decoding or uploading
|
|
153
|
+
* - GPU upload operations
|
|
154
|
+
*
|
|
155
|
+
* Do NOT use this executor for orchestration tasks like
|
|
156
|
+
* - model loading
|
|
157
|
+
* - material composition
|
|
158
|
+
* - dependency resolution
|
|
159
|
+
*
|
|
160
|
+
* because nested dependency chains can cause deadlocks.
|
|
161
|
+
*/
|
|
162
|
+
executor: AsyncExecutor;
|
|
163
|
+
/**
|
|
164
|
+
* Registers a loader type
|
|
165
|
+
*/
|
|
166
|
+
registerLoader(descriptor: LoaderEntry | LoaderByExtension): void;
|
|
167
|
+
/**
|
|
168
|
+
* Asset creator registry that takes precedence over the static {@link ContentLoader.creators}
|
|
169
|
+
*/
|
|
170
|
+
creators: Map<string, AssetCreator<any, any>>;
|
|
171
|
+
/**
|
|
172
|
+
* Registeres a creator function
|
|
173
|
+
*/
|
|
174
|
+
registerCreator<O, T>(type: AssetType<O, T>, factory: AssetCreator<O, T>): void;
|
|
175
|
+
/**
|
|
176
|
+
* The URI resolver used by {@link ContentLoader.resolveUri}
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* Replace or wrap it to customize how asset URIs are resolved.
|
|
180
|
+
* Default is {@link defaultUriResolver}
|
|
181
|
+
*/
|
|
182
|
+
uriResolver: UriResolver;
|
|
183
|
+
/**
|
|
184
|
+
* Resolves an asset URI to the URL that is loaded
|
|
185
|
+
*/
|
|
186
|
+
resolveUri(assetUri: string, options?: ResolveUriOptions): string;
|
|
187
|
+
/**
|
|
188
|
+
* Number of cached containers and asset instances
|
|
189
|
+
*/
|
|
190
|
+
get cacheStats(): {
|
|
191
|
+
containers: number;
|
|
192
|
+
assets: number;
|
|
193
|
+
};
|
|
194
|
+
protected disableCache: boolean;
|
|
195
|
+
protected containerCache: Map<string, Promise<AssetContainer>>;
|
|
196
|
+
protected assetCache: Map<string, Map<string, Promise<any>>>;
|
|
197
|
+
protected cacheKey: AssetCacheKey;
|
|
198
|
+
constructor(device: Device, options?: ContentLoaderOptions);
|
|
199
|
+
fetch(url: string, options: HttpOptions<'blob'>): Promise<HttpResponse<Blob>>;
|
|
200
|
+
fetch(url: string, options: HttpOptions<'arraybuffer'>): Promise<HttpResponse<ArrayBuffer>>;
|
|
201
|
+
fetch<T = any>(url: string, options: HttpOptions<'json'>): Promise<HttpResponse<T>>;
|
|
202
|
+
fetch(url: string, options: HttpOptions<'text'>): Promise<HttpResponse<string>>;
|
|
203
|
+
/**
|
|
204
|
+
* Loads an asset container from the given URL.
|
|
205
|
+
*
|
|
206
|
+
* @remarks
|
|
207
|
+
* Loads asset data from URL. Uses the `url` as cache key.
|
|
208
|
+
* First caller always populates the cache. Repeating calls with
|
|
209
|
+
* different `options.type` don't change the result.
|
|
210
|
+
*/
|
|
211
|
+
loadContainer(url: string, options?: LoadOptions): Promise<AssetContainer>;
|
|
212
|
+
/**
|
|
213
|
+
* Loads an asset from the container at the given URL and creates an instance.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* Loads the asset at `options.index` (default 0) of the container. The instance is cached,
|
|
217
|
+
* the key is given by the `cacheKey` option of the loader (url and index by default).
|
|
218
|
+
* Repeating calls with the same key return the same instance.
|
|
219
|
+
*/
|
|
220
|
+
load<T>(type: AssetType<any, T>, url: string, options?: LoadOptions): Promise<T>;
|
|
221
|
+
/**
|
|
222
|
+
* Creates a new asset instance.
|
|
223
|
+
*
|
|
224
|
+
* @remarks
|
|
225
|
+
*
|
|
226
|
+
*/
|
|
227
|
+
create<O, T>(type: AssetType<O, T>, options: O): T;
|
|
228
|
+
/**
|
|
229
|
+
* Shorthand for `load(AssetType.Texture, options)`
|
|
230
|
+
*/
|
|
231
|
+
loadTexture(url: string, options?: LoadOptions): Promise<Texture>;
|
|
232
|
+
/**
|
|
233
|
+
* Shorthand for `load(AssetType.Material, options)`
|
|
234
|
+
*/
|
|
235
|
+
loadMaterial(url: string, options?: LoadOptions): Promise<Material>;
|
|
236
|
+
/**
|
|
237
|
+
* Shorthand for `load(AssetType.Model, options)`
|
|
238
|
+
*/
|
|
239
|
+
loadModel(url: string, options?: LoadOptions): Promise<Model>;
|
|
240
|
+
/**
|
|
241
|
+
* Shorthand for `create(AssetType.Model, options)`
|
|
242
|
+
*/
|
|
243
|
+
createModel: (data: ModelOptions) => Model;
|
|
244
|
+
/**
|
|
245
|
+
* Shorthand for `create(AssetType.Material, options)`
|
|
246
|
+
*/
|
|
247
|
+
createMaterial: (options: Material | MaterialOptions) => Material;
|
|
248
|
+
/**
|
|
249
|
+
* Shorthand for `create(AssetType.Texture, options)`
|
|
250
|
+
*/
|
|
251
|
+
createTexture: (options: TextureOptions) => Texture;
|
|
252
|
+
/**
|
|
253
|
+
* Creates a loader for the given URL.
|
|
254
|
+
*
|
|
255
|
+
* @remarks
|
|
256
|
+
* If no loader can be found from the URL or options, this method sends
|
|
257
|
+
* a HEAD request to the URL. The method then uses the returned content type to try again.
|
|
258
|
+
*/
|
|
259
|
+
resolveLoader(url: string, type?: string): Promise<AssetLoader>;
|
|
260
|
+
protected findLoader(type: string): LoaderFactory<any>;
|
|
261
|
+
protected findCreator(type: AssetType): AssetCreator;
|
|
262
|
+
}
|
|
263
|
+
export declare const createModel: AssetCreator<ModelOptions, Model>;
|
|
264
|
+
export declare const createMaterial: AssetCreator<MaterialOptions, Material>;
|
|
265
|
+
export declare function createTexture(content: ContentLoader, options: TextureOptions): Texture;
|
|
266
|
+
//# sourceMappingURL=ContentLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContentLoader.d.ts","sourceRoot":"","sources":["../../../src/ContentLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EAGN,QAAQ,EAER,eAAe,EACf,aAAa,EACb,OAAO,EACP,cAAc,EACf,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AACvH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,aAAa,EAAsB,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AE,KAAK,CAoBzD,CAAA;AAgBD,eAAO,MAAM,cAAc,EAAE,YAAY,CAAC,eAAe,EAAE,QAAQ,CAiBlE,CAAA;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAKtF"}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { __awaiter, __rest } from "tslib";
|
|
2
|
+
import { BasicMaterial, isAquirableTextureOptions, isOwnable, Material, Mesh, } from '@gglib/graphics';
|
|
3
|
+
import { Model } from '@gglib/model';
|
|
4
|
+
import { extname, mergeUri } from '@gglib/utils';
|
|
5
|
+
import { AssetLoaderRegistry } from './AssetLoaderRegistry';
|
|
6
|
+
import { AssetType } from './AssetType';
|
|
7
|
+
import { NaiveAsyncExecutor } from './AsyncExecutor';
|
|
8
|
+
import { HttpClient } from './HttpClient';
|
|
9
|
+
/**
|
|
10
|
+
* The default {@link AssetCacheKey}: the url, and the asset index if it is not 0
|
|
11
|
+
*/
|
|
12
|
+
export const defaultAssetCacheKey = (_type, url, options) => {
|
|
13
|
+
return (options === null || options === void 0 ? void 0 : options.index) ? `${url}#${options.index}` : url;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The default {@link UriResolver}: resolves `assetUri` like a browser resolves a link,
|
|
17
|
+
* with `parentUri` as the document URL and `baseUri` as its `<base href>`
|
|
18
|
+
*/
|
|
19
|
+
export const defaultUriResolver = (assetUri, { parentUri, baseUri }) => {
|
|
20
|
+
let base = parentUri || '';
|
|
21
|
+
if (baseUri) {
|
|
22
|
+
const isDirectory = baseUri.endsWith('/');
|
|
23
|
+
base = mergeUri(base, baseUri);
|
|
24
|
+
// mergeUri drops the trailing slash, but it decides which directory the base points to
|
|
25
|
+
if (isDirectory && !base.endsWith('/')) {
|
|
26
|
+
base += '/';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return mergeUri(base, assetUri);
|
|
30
|
+
};
|
|
31
|
+
export class ContentLoader {
|
|
32
|
+
/**
|
|
33
|
+
* Registers a loader type
|
|
34
|
+
*/
|
|
35
|
+
static registerLoader(descriptor) {
|
|
36
|
+
this.loaders.register(descriptor);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Registers a creator function for all content loaders
|
|
40
|
+
*/
|
|
41
|
+
static registerCreator(type, factory) {
|
|
42
|
+
this.creators.set(type, factory);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Registers a loader type
|
|
46
|
+
*/
|
|
47
|
+
registerLoader(descriptor) {
|
|
48
|
+
this.loaders.register(descriptor);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Registeres a creator function
|
|
52
|
+
*/
|
|
53
|
+
registerCreator(type, factory) {
|
|
54
|
+
this.creators.set(type, factory);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolves an asset URI to the URL that is loaded
|
|
58
|
+
*/
|
|
59
|
+
resolveUri(assetUri, options) {
|
|
60
|
+
return this.uriResolver(assetUri, options || {});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Number of cached containers and asset instances
|
|
64
|
+
*/
|
|
65
|
+
get cacheStats() {
|
|
66
|
+
let assets = 0;
|
|
67
|
+
for (const cache of this.assetCache.values()) {
|
|
68
|
+
assets += cache.size;
|
|
69
|
+
}
|
|
70
|
+
return { containers: this.containerCache.size, assets };
|
|
71
|
+
}
|
|
72
|
+
constructor(device, options) {
|
|
73
|
+
/**
|
|
74
|
+
* Asset creator registry that takes precedence over the static {@link ContentLoader.creators}
|
|
75
|
+
*/
|
|
76
|
+
this.creators = new Map();
|
|
77
|
+
this.disableCache = false;
|
|
78
|
+
this.containerCache = new Map();
|
|
79
|
+
this.assetCache = new Map();
|
|
80
|
+
/**
|
|
81
|
+
* Shorthand for `create(AssetType.Model, options)`
|
|
82
|
+
*/
|
|
83
|
+
this.createModel = (data) => {
|
|
84
|
+
return this.create(AssetType.Model, data);
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Shorthand for `create(AssetType.Material, options)`
|
|
88
|
+
*/
|
|
89
|
+
this.createMaterial = (options) => {
|
|
90
|
+
return this.create(AssetType.Material, options);
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Shorthand for `create(AssetType.Texture, options)`
|
|
94
|
+
*/
|
|
95
|
+
this.createTexture = (options) => {
|
|
96
|
+
return this.create(AssetType.Texture, options);
|
|
97
|
+
};
|
|
98
|
+
this.device = device;
|
|
99
|
+
this.http = (options === null || options === void 0 ? void 0 : options.http) || new HttpClient();
|
|
100
|
+
this.loaders = (options === null || options === void 0 ? void 0 : options.registry) || new AssetLoaderRegistry();
|
|
101
|
+
this.executor = (options === null || options === void 0 ? void 0 : options.executor) || new NaiveAsyncExecutor();
|
|
102
|
+
this.disableCache = !!(options === null || options === void 0 ? void 0 : options.disableCache);
|
|
103
|
+
this.cacheKey = (options === null || options === void 0 ? void 0 : options.cacheKey) || defaultAssetCacheKey;
|
|
104
|
+
this.uriResolver = (options === null || options === void 0 ? void 0 : options.resolveUri) || defaultUriResolver;
|
|
105
|
+
}
|
|
106
|
+
fetch(url, options) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
return this.executor.run(() => this.http.fetch(url, options), options === null || options === void 0 ? void 0 : options.signal);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Loads an asset container from the given URL.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* Loads asset data from URL. Uses the `url` as cache key.
|
|
116
|
+
* First caller always populates the cache. Repeating calls with
|
|
117
|
+
* different `options.type` don't change the result.
|
|
118
|
+
*/
|
|
119
|
+
loadContainer(url, options) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
url = this.resolveUri(url, { baseUri: options === null || options === void 0 ? void 0 : options.baseUrl });
|
|
122
|
+
if (!this.disableCache && this.containerCache.has(url)) {
|
|
123
|
+
return this.containerCache.get(url);
|
|
124
|
+
}
|
|
125
|
+
const promise = this.resolveLoader(url, options === null || options === void 0 ? void 0 : options.type).then((loader) => {
|
|
126
|
+
if (!loader) {
|
|
127
|
+
throw new Error(`No loader found for URL: ${url}`);
|
|
128
|
+
}
|
|
129
|
+
return loader.load(url, Object.assign(Object.assign({}, (options || {})), { content: this }));
|
|
130
|
+
});
|
|
131
|
+
if (!this.disableCache) {
|
|
132
|
+
this.containerCache.set(url, promise);
|
|
133
|
+
}
|
|
134
|
+
return promise;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Loads an asset from the container at the given URL and creates an instance.
|
|
139
|
+
*
|
|
140
|
+
* @remarks
|
|
141
|
+
* Loads the asset at `options.index` (default 0) of the container. The instance is cached,
|
|
142
|
+
* the key is given by the `cacheKey` option of the loader (url and index by default).
|
|
143
|
+
* Repeating calls with the same key return the same instance.
|
|
144
|
+
*/
|
|
145
|
+
load(type, url, options) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
if (!this.assetCache.has(type)) {
|
|
148
|
+
this.assetCache.set(type, new Map());
|
|
149
|
+
}
|
|
150
|
+
const _a = options || {}, { index, scope } = _a, context = __rest(_a, ["index", "scope"]);
|
|
151
|
+
const cache = this.assetCache.get(type);
|
|
152
|
+
const key = this.cacheKey(type, url, options);
|
|
153
|
+
if (!this.disableCache && cache.has(key)) {
|
|
154
|
+
return cache.get(key).then((instance) => {
|
|
155
|
+
if (isOwnable(instance) && instance.ownership.isFinalized) {
|
|
156
|
+
// finalized while the request was pending, the entry is evicted by now
|
|
157
|
+
return this.load(type, url, options);
|
|
158
|
+
}
|
|
159
|
+
return ownInstance(instance, scope, this.device);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const promise = this.loadContainer(url, context)
|
|
163
|
+
.then((container) => {
|
|
164
|
+
return container.load(type, index !== null && index !== void 0 ? index : 0, Object.assign(Object.assign({}, context), { content: this }));
|
|
165
|
+
})
|
|
166
|
+
.then((options) => {
|
|
167
|
+
const instance = this.create(type, options);
|
|
168
|
+
if (!this.disableCache && isOwnable(instance)) {
|
|
169
|
+
instance.ownership.onFinalize(() => {
|
|
170
|
+
if (cache.get(key) === promise) {
|
|
171
|
+
cache.delete(key);
|
|
172
|
+
}
|
|
173
|
+
// the container may have released its data after creating the instance, load the file again next time
|
|
174
|
+
this.containerCache.delete(this.resolveUri(url, { baseUri: context.baseUrl }));
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return instance;
|
|
178
|
+
});
|
|
179
|
+
if (!this.disableCache) {
|
|
180
|
+
cache.set(key, promise);
|
|
181
|
+
}
|
|
182
|
+
return promise.then((instance) => ownInstance(instance, scope, this.device));
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Creates a new asset instance.
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
*
|
|
190
|
+
*/
|
|
191
|
+
create(type, options) {
|
|
192
|
+
const factory = this.findCreator(type);
|
|
193
|
+
if (!factory) {
|
|
194
|
+
throw new Error(`Factory not found for asset type: ${type}`);
|
|
195
|
+
}
|
|
196
|
+
return factory(this, options);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Shorthand for `load(AssetType.Texture, options)`
|
|
200
|
+
*/
|
|
201
|
+
loadTexture(url, options) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
return this.load(AssetType.Texture, url, options);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Shorthand for `load(AssetType.Material, options)`
|
|
208
|
+
*/
|
|
209
|
+
loadMaterial(url, options) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
return this.load(AssetType.Material, url, options);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Shorthand for `load(AssetType.Model, options)`
|
|
216
|
+
*/
|
|
217
|
+
loadModel(url, options) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
return this.load(AssetType.Model, url, options);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Creates a loader for the given URL.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* If no loader can be found from the URL or options, this method sends
|
|
227
|
+
* a HEAD request to the URL. The method then uses the returned content type to try again.
|
|
228
|
+
*/
|
|
229
|
+
resolveLoader(url, type) {
|
|
230
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
231
|
+
let factory = null;
|
|
232
|
+
if (type) {
|
|
233
|
+
factory = this.findLoader(type);
|
|
234
|
+
}
|
|
235
|
+
if (!factory) {
|
|
236
|
+
const ext = extname(url).toLowerCase();
|
|
237
|
+
factory = this.findLoader(ext);
|
|
238
|
+
}
|
|
239
|
+
if (!factory) {
|
|
240
|
+
const res = yield this.http.fetch(url, { method: 'HEAD' }).catch((err) => {
|
|
241
|
+
// ignore as it's just unable to determine the content type
|
|
242
|
+
// failed request is visible in the network tab
|
|
243
|
+
});
|
|
244
|
+
if (res) {
|
|
245
|
+
factory = this.findLoader(res.contentType);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (factory) {
|
|
249
|
+
return yield factory(this);
|
|
250
|
+
}
|
|
251
|
+
throw new Error(`No loader found for URL: ${url} with type: ${type || 'unknown'}`);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
findLoader(type) {
|
|
255
|
+
if (!type) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return this.loaders.find(type) || ContentLoader.loaders.find(type);
|
|
259
|
+
}
|
|
260
|
+
findCreator(type) {
|
|
261
|
+
if (this.creators.has(type)) {
|
|
262
|
+
return this.creators.get(type);
|
|
263
|
+
}
|
|
264
|
+
if (ContentLoader.creators.has(type)) {
|
|
265
|
+
return ContentLoader.creators.get(type);
|
|
266
|
+
}
|
|
267
|
+
switch (type) {
|
|
268
|
+
case AssetType.Material: {
|
|
269
|
+
return createMaterial;
|
|
270
|
+
}
|
|
271
|
+
case AssetType.Texture: {
|
|
272
|
+
return createTexture;
|
|
273
|
+
}
|
|
274
|
+
case AssetType.Model: {
|
|
275
|
+
return createModel;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Registry for loader types
|
|
283
|
+
*/
|
|
284
|
+
ContentLoader.loaders = new AssetLoaderRegistry();
|
|
285
|
+
/**
|
|
286
|
+
* Registry for asset creators
|
|
287
|
+
*/
|
|
288
|
+
ContentLoader.creators = new Map();
|
|
289
|
+
export const createModel = (content, data) => {
|
|
290
|
+
var _a;
|
|
291
|
+
return new Model(content.device, Object.assign(Object.assign({}, data), { meshes: (_a = data.meshes) === null || _a === void 0 ? void 0 : _a.map((mesh) => {
|
|
292
|
+
var _a;
|
|
293
|
+
if (mesh instanceof Mesh) {
|
|
294
|
+
return mesh;
|
|
295
|
+
}
|
|
296
|
+
return Object.assign(Object.assign({}, mesh), {
|
|
297
|
+
// meshes create (and own) their materials, routed through the creator registry of the content loader
|
|
298
|
+
materials: (_a = mesh.materials) === null || _a === void 0 ? void 0 : _a.map((it) => {
|
|
299
|
+
if (it instanceof Material) {
|
|
300
|
+
return it;
|
|
301
|
+
}
|
|
302
|
+
// the wrapper replaces `factory` on the copy only, `it` keeps its original factory, so this does not recurse
|
|
303
|
+
return Object.assign(Object.assign({}, it), { factory: () => content.create(AssetType.Material, it) });
|
|
304
|
+
}) });
|
|
305
|
+
}) }));
|
|
306
|
+
};
|
|
307
|
+
function ownInstance(instance, scope, device) {
|
|
308
|
+
if (!scope || !isOwnable(instance)) {
|
|
309
|
+
return instance;
|
|
310
|
+
}
|
|
311
|
+
if (scope.isDisposed) {
|
|
312
|
+
// the scope was disposed while loading, an instance nobody claimed would stay alive with the cache
|
|
313
|
+
if (!instance.ownership.isClaimed) {
|
|
314
|
+
device.scope.drop(instance);
|
|
315
|
+
}
|
|
316
|
+
throw new Error(`scope '${scope.name}' was disposed while loading`);
|
|
317
|
+
}
|
|
318
|
+
return scope.adopt(instance);
|
|
319
|
+
}
|
|
320
|
+
export const createMaterial = (content, options) => {
|
|
321
|
+
if (!options) {
|
|
322
|
+
throw new Error('Material options are required');
|
|
323
|
+
}
|
|
324
|
+
if (options instanceof Material) {
|
|
325
|
+
return options;
|
|
326
|
+
}
|
|
327
|
+
if (options.effect) {
|
|
328
|
+
return new Material(null, options);
|
|
329
|
+
}
|
|
330
|
+
if (options.factory) {
|
|
331
|
+
return options.factory(content.device, options);
|
|
332
|
+
}
|
|
333
|
+
return new BasicMaterial(content.device, options);
|
|
334
|
+
};
|
|
335
|
+
export function createTexture(content, options) {
|
|
336
|
+
if (isAquirableTextureOptions(options)) {
|
|
337
|
+
return content.device.acquireTexture(options);
|
|
338
|
+
}
|
|
339
|
+
return content.device.createTexture(options);
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=ContentLoader.js.map
|