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); unpackConfig.onProgress && unpackConfig.onProgress(0);
let totalZipNumber = 0, progress = 0; let totalZipNumber = 0, progress = 0;
// 每次解包前确保 loader 可用
if (!this.loader) {
this.loader = new ObjectLoader();
}
await this.initOfflineSource(unpackConfig); await this.initOfflineSource(unpackConfig);
if (!this.offlineZipMap && !this.offlineFlatPackages) { if (!this.offlineZipMap && !this.offlineFlatPackages) {
@ -998,8 +1003,7 @@ export class Package {
that.offlineFlatPackages = null; that.offlineFlatPackages = null;
that.offlineFlatEntryBase = null; that.offlineFlatEntryBase = null;
that.dataComponentMap = null; that.dataComponentMap = null;
// @ts-ignore 清除loader // 不清除 loader避免连续导入时被上一轮异步收尾误清理
that.loader = undefined;
} }
const complete = () => { const complete = () => {
@ -1436,7 +1440,25 @@ export class Package {
const parse = (json) => { const parse = (json) => {
if (check(json.object, json)) { if (check(json.object, json)) {
if (!loader) return; 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) => { loader.parse(json, (group) => {
const bones: Bone[] = []; const bones: Bone[] = [];
group.getObjectsByProperty("type", "Bone", bones); group.getObjectsByProperty("type", "Bone", bones);
@ -1470,6 +1492,21 @@ export class Package {
func.call(this, uuid, group, rootGroupUuid); 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 { } else {
const timer = setTimeout(() => { const timer = setTimeout(() => {
clearTimeout(timer); clearTimeout(timer);