MatchMaster/assets/home_popup_queue/HomePopupQueue.ts

47 lines
1.9 KiB
TypeScript

const { ccclass } = cc._decorator;
/** HomeScene 共用队列。显示中的活动及其奖励窗口关闭后才推进下一项。 */
@ccclass
export default class HomePopupQueue extends cc.Component {
private tasks: Array<{ key: string; priority: number; run: (done: () => void) => void }> = [];
private running = "";
public static get(canvas: cc.Node): HomePopupQueue {
return canvas.getComponent(HomePopupQueue) || canvas.addComponent(HomePopupQueue);
}
public enqueue(key: string, priority: number, run: (done: () => void) => void) {
if (this.running === key || this.tasks.some(task => task.key === key)) return;
this.tasks.push({ key, priority, run });
this.tasks.sort((a, b) => a.priority - b.priority);
}
update() {
if (this.running || !this.tasks.length || this.hasVisiblePopup()) return;
const task = this.tasks.shift();
this.running = task.key;
let finished = false;
const done = () => {
if (finished) return;
finished = true;
this.running = "";
};
try { task.run(done); }
catch (error) { done(); cc.error("[HomePopupQueue] " + task.key, error); }
}
private hasVisiblePopup(): boolean {
const visible = (node: cc.Node) => node && cc.isValid(node, true) && node.activeInHierarchy;
if (visible(this.node.getChildByName("Transfer"))) return true;
const seven = this.node.getChildByName("sevenDayGift");
if (seven && seven.children.some(visible)) return true;
const home: any = this.node.getComponent("JiaZai");
if (!home) return false;
if (home.passContentLoading) return true;
return ["RewardNode", "getcard", "monthlyCardNode", "actionpNode", "winStreakNode", "passCheckNode", "passIntroNode", "jungleTreasureNode"]
.some(key => visible(home[key]));
}
onDestroy() { this.tasks.length = 0; this.running = ""; }
}