fix(sdk): fix loader lifecycle for consecutive offline pack imports

This commit is contained in:
plum 2026-04-10 11:22:28 +08:00
parent 6720a3e730
commit 83684b4c7b

View File

@ -964,6 +964,11 @@ export class Package {
unpackConfig.onProgress && unpackConfig.onProgress(0);
let totalZipNumber = 0, progress = 0;
// 每次解包前确保 loader 可用
if (!this.loader) {
this.loader = new ObjectLoader();
}
await this.initOfflineSource(unpackConfig);
if (!this.offlineZipMap && !this.offlineFlatPackages) {
@ -998,8 +1003,7 @@ export class Package {
that.offlineFlatPackages = null;
that.offlineFlatEntryBase = null;
that.dataComponentMap = null;
// @ts-ignore 清除loader
that.loader = undefined;
// 不清除 loader避免连续导入时被上一轮异步收尾误清理
}
const complete = () => {
@ -1436,8 +1440,26 @@ export class Package {
const parse = (json) => {
if (check(json.object, json)) {
if (!loader) return;
loader.parse(json, (group) => {
if (!loader) {
// 生产环境定位: 打印关键上下文和调用栈
console.error("[Package.unpackGroup] loader is undefined before parse", {
uuid,
rootGroupUuid,
parent,
callFunNum: this.callFunNum?.value,
hasJson: Boolean(json),
childCount: json?.object?.children?.length,
currentLoader: this.loader,
capturedLoader: loader,
stack: new Error("[Package.unpackGroup] missing loader").stack
});
// 防止早退导致 callFunNum 无法归零,进度卡住
this.callFunNum.value--;
return;
}
try {
loader.parse(json, (group) => {
const bones: Bone[] = [];
group.getObjectsByProperty("type", "Bone", bones);
if (bones.length > 0) {
@ -1469,7 +1491,22 @@ export class Package {
funcMap.forEach((func, uuid) => {
func.call(this, uuid, group, rootGroupUuid);
})
})
})
} catch (err) {
console.error("[Package.unpackGroup] loader.parse threw", {
uuid,
rootGroupUuid,
parent,
callFunNum: this.callFunNum?.value,
hasJson: Boolean(json),
childCount: json?.object?.children?.length,
currentLoader: this.loader,
capturedLoader: loader,
err,
stack: err instanceof Error ? err.stack : new Error("[Package.unpackGroup] parse error").stack
});
throw err;
}
} else {
const timer = setTimeout(() => {
clearTimeout(timer);