feat: 内存优化
This commit is contained in:
@@ -12,7 +12,12 @@ export default class CloudRiseService {
|
||||
private static runtime: any = null;
|
||||
private static bundleTask: Promise<cc.AssetManager.Bundle> = null;
|
||||
private static prefabTask: Promise<cc.Prefab> = null;
|
||||
private static pageTasks: { [name: string]: { bundle: cc.AssetManager.Bundle; task: Promise<cc.Prefab> } } = {};
|
||||
private static pageTasks: { [name: string]: { bundle: cc.AssetManager.Bundle; task: Promise<cc.Prefab>; release: () => void } } = {};
|
||||
private static pageUsers: { [name: string]: number } = {};
|
||||
private static resourceUsers = 0;
|
||||
private static pendingPageLoads = 0;
|
||||
private static releaseQueued = false;
|
||||
private static artBundle: cc.AssetManager.Bundle = null;
|
||||
private static checkTask: Promise<boolean> = null;
|
||||
private static snapshot: any = null;
|
||||
private static uid = "";
|
||||
@@ -93,15 +98,15 @@ export default class CloudRiseService {
|
||||
static resultKey(): string { return this.runtime ? this.runtime.resultKey() : ""; }
|
||||
static unseenResult(): boolean { return !!this.runtime && this.runtime.unseenResult(); }
|
||||
static allowsAutomaticResults(): boolean { this.ensureUser(); return this.autoDisplay; }
|
||||
static acknowledgeResult(): void { if (this.runtime) this.runtime.acknowledgeResult(); }
|
||||
static acknowledgeResult(): void { if (this.runtime) this.runtime.acknowledgeResult(); this.queueResourceRelease(); }
|
||||
static elimination(): any { return this.runtime ? this.runtime.elimination() : null; }
|
||||
static hasPendingLoss(): boolean { return !!this.runtime && this.runtime.hasPendingLoss(); }
|
||||
static acknowledgeElimination(key: string): void { if (this.runtime) this.runtime.acknowledgeElimination(key); }
|
||||
static dismissExpiryPresentation(): void { if (this.runtime) this.runtime.dismissExpiryPresentation(); }
|
||||
static acknowledgeElimination(key: string): void { if (this.runtime) this.runtime.acknowledgeElimination(key); this.queueResourceRelease(); }
|
||||
static dismissExpiryPresentation(): void { if (this.runtime) this.runtime.dismissExpiryPresentation(); this.queueResourceRelease(); }
|
||||
static subscribe(fn: () => void): () => void {
|
||||
this.listeners.push(fn); return () => { this.listeners = this.listeners.filter(item => item !== fn); };
|
||||
}
|
||||
private static emit(): void { this.listeners.slice().forEach(fn => fn()); }
|
||||
private static emit(): void { this.listeners.slice().forEach(fn => fn()); this.queueResourceRelease(); }
|
||||
/** Automatic loading requires fresh server eligibility, never just a cached run. */
|
||||
static shouldPreload(data: any): boolean {
|
||||
if (!data) return false;
|
||||
@@ -125,30 +130,76 @@ export default class CloudRiseService {
|
||||
})).catch(error => { this.prefabTask = null; throw error; });
|
||||
return this.prefabTask;
|
||||
}
|
||||
static retainResources(): () => void { return ActivityBundleLoader.retainGroup("cloudRise"); }
|
||||
static retainResources(): () => void {
|
||||
const release = ActivityBundleLoader.retainGroup("cloudRise");
|
||||
this.resourceUsers++;
|
||||
let released = false;
|
||||
return () => {
|
||||
if (released) return; released = true;
|
||||
this.resourceUsers--; release(); this.queueResourceRelease();
|
||||
};
|
||||
}
|
||||
/** A page owns its assets through loading, rendering and deferred node destruction. */
|
||||
static retainPageResources(page: "matching" | "stage", stage = 1): () => void {
|
||||
const name = page === "matching" ? "matching" : "stage" + stage;
|
||||
const release = this.retainResources();
|
||||
this.pageUsers[name] = (this.pageUsers[name] || 0) + 1;
|
||||
let released = false;
|
||||
return () => {
|
||||
if (released) return; released = true;
|
||||
cc.director.once(cc.Director.EVENT_AFTER_DRAW, () => {
|
||||
this.pageUsers[name]--; release();
|
||||
});
|
||||
};
|
||||
}
|
||||
private static queueResourceRelease(): void {
|
||||
if (this.releaseQueued || !Object.keys(this.pageTasks).length && !this.artBundle) return;
|
||||
this.releaseQueued = true;
|
||||
cc.director.once(cc.Director.EVENT_AFTER_DRAW, () => {
|
||||
this.releaseQueued = false;
|
||||
// A different stage may still be resolving shared dependencies.
|
||||
if (this.pendingPageLoads) return;
|
||||
const run = this.run(), stage = this.stage(), elimination = this.elimination();
|
||||
const active = this.challengeActive(run);
|
||||
const keepStage = stage && (active || this.unseenResult()) ? "stage" + stage.stage : "";
|
||||
const keepElimination = elimination && elimination.after ? "stage" + elimination.after.stage : "";
|
||||
const keepMatching = run ? active && run.status === "waiting" : this.shouldPreload(this.data());
|
||||
for (const name of Object.keys(this.pageTasks)) {
|
||||
if (this.pageUsers[name] || name === keepStage || name === keepElimination || name === "matching" && keepMatching) continue;
|
||||
const entry = this.pageTasks[name]; delete this.pageTasks[name];
|
||||
entry.bundle.releaseAll(); entry.release();
|
||||
}
|
||||
// Release dependent pages first. Surviving pages continue to own shared art.
|
||||
if (!Object.keys(this.pageTasks).length && !this.resourceUsers && this.artBundle) {
|
||||
this.artBundle.releaseAll(); this.artBundle = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
static async pageFragment(page: "matching" | "stage", stage = 1): Promise<cc.Prefab> {
|
||||
if (page === "stage" && ![1, 2, 3].includes(stage)) throw new Error("无效的百人赛阶段");
|
||||
const name = page === "matching" ? "matching" : "stage" + stage;
|
||||
const release = this.retainResources();
|
||||
const release = this.retainPageResources(page, stage);
|
||||
this.pendingPageLoads++;
|
||||
let phase = 'home-and-manifest';
|
||||
try {
|
||||
await Promise.all([this.bundle(), this.prepareActivityVersion()]);
|
||||
phase = 'art';
|
||||
await ActivityBundleLoader.ensureBundle("art");
|
||||
this.artBundle = await ActivityBundleLoader.ensureBundle("art");
|
||||
phase = 'page-bundle';
|
||||
const bundle = await ActivityBundleLoader.ensureBundle(name);
|
||||
phase = 'prefab';
|
||||
if (!this.pageTasks[name] || this.pageTasks[name].bundle !== bundle) {
|
||||
const entry = { bundle, task: null as Promise<cc.Prefab> };
|
||||
if (this.pageTasks[name]) this.pageTasks[name].release();
|
||||
const entry = { bundle, task: null as Promise<cc.Prefab>, release: ActivityBundleLoader.retainGroup("cloudRise") };
|
||||
entry.task = new Promise<cc.Prefab>((resolve, reject) => {
|
||||
bundle.load("prefab/CloudRise", cc.Prefab, (error, prefab: cc.Prefab) => error ? reject(error) : resolve(prefab));
|
||||
}).catch(error => { if (this.pageTasks[name] === entry) delete this.pageTasks[name]; throw error; });
|
||||
}).catch(error => { if (this.pageTasks[name] === entry) delete this.pageTasks[name]; entry.release(); throw error; });
|
||||
this.pageTasks[name] = entry;
|
||||
}
|
||||
const prefab = await this.pageTasks[name].task;
|
||||
return prefab;
|
||||
} catch (error) { traceResources('fragment.failed', { name, phase }, error); throw error; }
|
||||
finally { release(); }
|
||||
finally { this.pendingPageLoads--; release(); }
|
||||
}
|
||||
/** Select the remote release only; each bundle and its resources load on demand. */
|
||||
private static prepareActivityVersion(): Promise<void> {
|
||||
@@ -160,8 +211,8 @@ export default class CloudRiseService {
|
||||
void Promise.all([this.pagePrefab(1), this.prepareActivityVersion()]).then(() => {
|
||||
const latest = this.data(), run = this.run();
|
||||
// Warm only matching and its shared dependencies when another match is possible.
|
||||
if (run && run.status === "waiting" && this.challengeActive(run)
|
||||
|| latest && latest.available && this.registrationOpen(latest) && !this.lockedUntilLevel(latest)) {
|
||||
if (run ? run.status === "waiting" && this.challengeActive(run)
|
||||
: latest && latest.available && this.registrationOpen(latest) && !this.lockedUntilLevel(latest)) {
|
||||
return this.pageFragment("matching").then(() => {});
|
||||
}
|
||||
}).catch(error => cc.warn("百人赛预加载失败,打开时可重试", error));
|
||||
|
||||
@@ -18,6 +18,11 @@ export default class PyramidService {
|
||||
private static prefabBundle: cc.AssetManager.Bundle = null;
|
||||
private static paymentOnShowRegistered = false;
|
||||
private static prefabTask: Promise<cc.Prefab> = null;
|
||||
private static resourceUsers = 0;
|
||||
private static resourceLoads = 0;
|
||||
private static releaseQueued = false;
|
||||
private static resourceBundles = new Set<cc.AssetManager.Bundle>();
|
||||
private static resourceLeases: Array<() => void> = [];
|
||||
private static paymentRetryBlocked = false;
|
||||
private static settlementTask: Promise<boolean> = null;
|
||||
private static settlementNotices: any[] = [];
|
||||
@@ -99,20 +104,58 @@ export default class PyramidService {
|
||||
return ActivityBundleLoader.ensureBundle("pyramid");
|
||||
}
|
||||
static async acquireResources(): Promise<() => void> {
|
||||
const lease = await ActivityBundleLoader.acquireBundle("pyramid"); return lease.release;
|
||||
// Reserve before awaiting, including a new open racing with a queued release.
|
||||
this.resourceUsers++;
|
||||
try {
|
||||
const lease = await ActivityBundleLoader.acquireBundle("pyramid");
|
||||
this.resourceBundles.add(lease.bundle);
|
||||
let released = false;
|
||||
return () => {
|
||||
if (released) return; released = true;
|
||||
this.resourceUsers--;
|
||||
// Keep the version pinned until every late asset callback has finished.
|
||||
this.resourceLeases.push(lease.release);
|
||||
this.queueResourceRelease();
|
||||
};
|
||||
} catch (error) { this.resourceUsers--; this.queueResourceRelease(); throw error; }
|
||||
}
|
||||
static loadAsset(bundle: cc.AssetManager.Bundle, path: string, type: any,
|
||||
done: (error: Error, asset: any) => void): void {
|
||||
this.resourceLoads++;
|
||||
let finished = false;
|
||||
const finish = (error: Error, asset?: any) => {
|
||||
if (finished) return; finished = true;
|
||||
try { done(error, asset); }
|
||||
finally { this.resourceLoads--; this.queueResourceRelease(); }
|
||||
};
|
||||
try { bundle.load(path, type, finish); }
|
||||
catch (error) { if (finished) throw error; finish(error); }
|
||||
}
|
||||
private static queueResourceRelease(): void {
|
||||
if (this.resourceUsers || this.resourceLoads || this.releaseQueued || !this.resourceLeases.length) return;
|
||||
this.releaseQueued = true;
|
||||
cc.director.once(cc.Director.EVENT_AFTER_DRAW, () => {
|
||||
this.releaseQueued = false;
|
||||
if (this.resourceUsers || this.resourceLoads) return;
|
||||
this.prefabTask = null; this.prefabBundle = null;
|
||||
this.resourceBundles.forEach(bundle => bundle.releaseAll());
|
||||
this.resourceBundles.clear();
|
||||
this.resourceLeases.splice(0).forEach(release => release());
|
||||
});
|
||||
}
|
||||
static async prefab(): Promise<cc.Prefab> {
|
||||
// Preloads also retain their version until Prefab dependencies finish loading.
|
||||
const lease = await ActivityBundleLoader.acquireBundle("pyramid"), bundle = lease.bundle;
|
||||
const release = await this.acquireResources();
|
||||
try {
|
||||
const bundle = await this.bundle();
|
||||
if (bundle !== this.prefabBundle) { this.prefabBundle = bundle; this.prefabTask = null; }
|
||||
if (!this.prefabTask) this.prefabTask = new Promise<cc.Prefab>((resolve, reject) => {
|
||||
bundle.load("prefab/Pyramid", cc.Prefab, (error, prefab: cc.Prefab) => {
|
||||
this.loadAsset(bundle, "prefab/Pyramid", cc.Prefab, (error, prefab: cc.Prefab) => {
|
||||
if (error) reject(error); else resolve(prefab);
|
||||
});
|
||||
}).catch(error => { this.prefabTask = null; throw error; });
|
||||
return await this.prefabTask;
|
||||
} finally { lease.release(); }
|
||||
} finally { release(); }
|
||||
}
|
||||
private static async request(route: string, body: any = {}): Promise<any> {
|
||||
this.ensure(); const generation = this.generation;
|
||||
|
||||
Reference in New Issue
Block a user