Skip to content

Commit

Permalink
Fix sub asset promise error (#2274)
Browse files Browse the repository at this point in the history
* fix: sub promise error
  • Loading branch information
zhuxudong authored Jul 31, 2024
1 parent 78a00f6 commit 4f6d954
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,26 @@ export class ResourceManager {
_onSubAssetSuccess<T>(assetBaseURL: string, assetSubPath: string, value: T): void {
const subPromiseCallback = this._subAssetPromiseCallbacks[assetBaseURL]?.[assetSubPath];
if (subPromiseCallback) {
// Already resolved
subPromiseCallback.resolve(value);
} else {
// Pending
(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
resolve: value
resolvedValue: value
};
}
}

/**
* @internal
*/
_onSubAssetFail(assetBaseURL: string, assetSubPath: string, value: (reason: any) => void): void {
_onSubAssetFail(assetBaseURL: string, assetSubPath: string, value: Error): void {
const subPromiseCallback = this._subAssetPromiseCallbacks[assetBaseURL]?.[assetSubPath];
if (subPromiseCallback) {
// Already rejected
subPromiseCallback.reject(value);
} else {
// Pending
(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
reject: value
rejectedValue: value
};
}
}
Expand Down Expand Up @@ -352,7 +350,6 @@ export class ResourceManager {
let assetURL = assetBaseURL;
if (queryPath) {
assetURL += "?q=" + paths.shift();

let index: string;
while ((index = paths.shift())) {
assetURL += `[${index}]`;
Expand Down Expand Up @@ -425,35 +422,36 @@ export class ResourceManager {
): AssetPromise<T> {
const loadingPromises = this._loadingPromises;
const subPromiseCallback = this._subAssetPromiseCallbacks[assetBaseURL]?.[assetSubPath];
const resolvedValue = subPromiseCallback?.resolve;
const rejectedValue = subPromiseCallback?.reject;
const resolvedValue = subPromiseCallback?.resolvedValue;
const rejectedValue = subPromiseCallback?.rejectedValue;

// Already resolved or rejected
if (resolvedValue || rejectedValue) {
return new AssetPromise<T>((resolve, reject) => {
if (resolvedValue) {
resolve(resolvedValue);
} else if (rejectedValue) {
reject(rejectedValue);
}
});
}

// Pending
const promise = new AssetPromise<T>((resolve, reject) => {
if (resolvedValue) {
// Already resolved
resolve(resolvedValue);
} else if (rejectedValue) {
// Already rejected
reject(rejectedValue);
} else {
// Pending
loadingPromises[assetURL] = promise;

(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
resolve,
reject
};
}
(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
resolve,
reject
};
});

if (!resolvedValue && !rejectedValue) {
promise.then(
() => {
delete loadingPromises[assetURL];
},
() => delete loadingPromises[assetURL]
);
}
loadingPromises[assetURL] = promise;

promise.then(
() => {
delete loadingPromises[assetURL];
},
() => delete loadingPromises[assetURL]
);

return promise;
}
Expand Down Expand Up @@ -607,7 +605,11 @@ type SubAssetPromiseCallbacks<T> = Record<
// sub asset url, ie. "textures[0]"
string,
{
resolve?: T | PromiseLike<T>;
// Already resolved or rejected
resolvedValue?: T;
rejectedValue?: Error;
// Pending
resolve?: (value: T) => void;
reject?: (reason: any) => void;
}
>
Expand Down

0 comments on commit 4f6d954

Please sign in to comment.