Skip to content

Commit

Permalink
Support dependency map in resource config (#2476)
Browse files Browse the repository at this point in the history
* feat: support dependency map in `resource config`
  • Loading branch information
johanzhu authored Jan 2, 2025
1 parent 980a7ed commit 70af4ee
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class ResourceManager {
* @internal
*/
_getRemoteUrl(url: string): string {
return this._virtualPathMap[url] ?? url;
return this._virtualPathResourceMap[url]?.path ?? url;
}

/**
Expand All @@ -206,7 +206,7 @@ export class ResourceManager {
* @internal
*/
_onSubAssetSuccess<T>(assetBaseURL: string, assetSubPath: string, value: T): void {
const remoteAssetBaseURL = this._virtualPathMap[assetBaseURL] ?? assetBaseURL;
const remoteAssetBaseURL = this._virtualPathResourceMap[assetBaseURL]?.path ?? assetBaseURL;

const subPromiseCallback = this._subAssetPromiseCallbacks[remoteAssetBaseURL]?.[assetSubPath];
if (subPromiseCallback) {
Expand Down Expand Up @@ -361,7 +361,7 @@ export class ResourceManager {
const paths = queryPath ? this._parseQueryPath(queryPath) : [];

// Get remote asset base url
const remoteAssetBaseURL = this._virtualPathMap[assetBaseURL] ?? assetBaseURL;
const remoteAssetBaseURL = this._virtualPathResourceMap[assetBaseURL]?.path ?? assetBaseURL;

// Check cache
const cacheObject = this._assetUrlPool[remoteAssetBaseURL];
Expand Down Expand Up @@ -553,9 +553,9 @@ export class ResourceManager {
/** @internal */
_objectPool: { [key: string]: any } = Object.create(null);
/** @internal */
_editorResourceConfig: EditorResourceConfig = Object.create(null);
_idResourceMap: Record<ResourceId, EditorResourceItem> = Object.create(null);
/** @internal */
_virtualPathMap: Record<string, string> = Object.create(null);
_virtualPathResourceMap: Record<VirtualPath, EditorResourceItem> = Object.create(null);

/**
* @internal
Expand All @@ -568,9 +568,9 @@ export class ResourceManager {
if (obj) {
promise = Promise.resolve(obj);
} else {
const resourceConfig = this._editorResourceConfig[refId];
const resourceConfig = this._idResourceMap[refId];
if (!resourceConfig) {
Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`);
Logger.warn(`refId:${refId} is not find in this._idResourceMap.`);
return Promise.resolve(null);
}
let url = resourceConfig.virtualPath;
Expand All @@ -592,8 +592,11 @@ export class ResourceManager {
*/
initVirtualResources(config: EditorResourceItem[]): void {
config.forEach((element) => {
this._virtualPathMap[element.virtualPath] = element.path;
this._editorResourceConfig[element.id] = element;
this._virtualPathResourceMap[element.virtualPath] = element;
this._idResourceMap[element.id] = element;
if (element.dependentAssetMap) {
this._virtualPathResourceMap[element.virtualPath].dependentAssetMap = element.dependentAssetMap;
}
});
}
//-----------------Editor temp solution-----------------
Expand Down Expand Up @@ -631,8 +634,15 @@ const rePropName = RegExp(
"g"
);

type EditorResourceItem = { virtualPath: string; path: string; type: string; id: string };
type EditorResourceConfig = Record<string, EditorResourceItem>;
type ResourceId = string;
type VirtualPath = string;
type EditorResourceItem = {
virtualPath: string;
path: string;
type: string;
id: string;
dependentAssetMap?: { [key: string]: string };
};
type SubAssetPromiseCallbacks<T> = Record<
// main asset url, ie. "https://***.glb"
string,
Expand Down

0 comments on commit 70af4ee

Please sign in to comment.