找物源码导读Cocos Creator 3.8 · assets/scripts
0%
已读 0 / 424
目录— 个文件
目录加载中…
…/core/utility/RuntimeFlowGuards.ts
1import { Framework } from "../../Framework";
2
3/** Coalesces concurrent callers onto one asynchronous operation. */
4export class AsyncSingleFlight<T> {
5 private inFlight: Promise<T> | null = null;
6
7 run(task: () => Promise<T>): Promise<T> {
8 if (this.inFlight) {
9 return this.inFlight;
10 }
11 try {
12 const pending = task();
13 let wrapped!: Promise<T>;
14 wrapped = pending.finally(() => {
15 if (this.inFlight === wrapped) {
16 this.inFlight = null;
17 }
18 });
19 this.inFlight = wrapped;
20 return wrapped;
21 } catch (error) {
22 this.inFlight = null;
23 return Promise.reject(error);
24 }
25 }
26}
27
28export type WxProfilePayload = { nickName: string; avatarUrl: string };
29
30/** Anonymous/default WeChat data must not be persisted as a real player profile. */
31export function isUsableWxProfile(profile: WxProfilePayload): boolean {
32 const nickName = profile.nickName.trim();
33 const avatarUrl = profile.avatarUrl.trim();
34 Framework.Log.Log("微信头像检测:", nickName, avatarUrl);
35 if (!nickName || !avatarUrl) {
36 Framework.Log.Log("微信头像检测无效:", nickName, avatarUrl);
37 return false;
38 }
39 const isValid = nickName !== "微信用户" && nickName.toLowerCase() !== "wechat user";
40 Framework.Log.Log(isValid ? "微信头像检测有效,nick&avatar不是匿名用户" : "微信头像检测无效,nick&avatar是匿名用户", nickName, avatarUrl);
41 return isValid;
42}
43
44/** Deduplicates one foreground invite while allowing a failed login to retry. */
45export class DistinctInviteGate {
46 private inFlightInviteId = "";
47 private handledInviteId = "";
48
49 begin(inviteId: string): boolean {
50 const normalized = inviteId.trim();
51 if (!normalized || normalized === this.inFlightInviteId || normalized === this.handledInviteId) {
52 return false;
53 }
54 this.inFlightInviteId = normalized;
55 return true;
56 }
57
58 complete(inviteId: string, success: boolean): void {
59 const normalized = inviteId.trim();
60 if (this.inFlightInviteId === normalized) {
61 this.inFlightInviteId = "";
62 }
63 if (success) {
64 this.handledInviteId = normalized;
65 }
66 }
67
68 observeNoInvite(): void {
69 this.handledInviteId = "";
70 }
71}
72
73/** Timestamp gate independent from Cocos frame/update scheduling. */
74export class CooldownGate {
75 private nextAllowedAt = 0;
76 private readonly cooldownMs: number;
77
78 constructor(cooldownMs: number) {
79 this.cooldownMs = cooldownMs;
80 }
81
82 tryEnter(now: number = Date.now()): boolean {
83 if (now < this.nextAllowedAt) {
84 return false;
85 }
86 this.block(now);
87 return true;
88 }
89
90 block(now: number = Date.now()): void {
91 this.nextAllowedAt = now + this.cooldownMs;
92 }
93}
讲解94 行
运行时流程防重入守卫人工通读

RuntimeFlowGuards.ts

提供三类与帧调度无关的流程控制工具:把并发调用合并成同一个 Promise 的单飞器、识别微信匿名资料与重复邀请的判定器,以及基于时间戳的冷却门。启动登录、邀请拉起和频繁触发入口都用它们避免重复执行。

按文件内出现顺序阅读:AsyncSingleFlight 的 run、isUsableWxProfile、DistinctInviteGate 的三个方法、CooldownGate 的两个方法。

特殊处理与易错点

4注释AsyncSingleFlight:Coalesces concurrent callers onto one asynchro
5属性当前进行中的 Promise,null 表示空闲。
7方法已有进行中的任务时直接返回同一个 Promise,否则执行 task 并在结束后清空占用。
31方法裁剪昵称和头像地址后判空,再排除微信默认匿名昵称,返回是否可当作真实资料。
49方法裁剪邀请 id,空白、与进行中或已处理 id 相同时返回 false,否则记为进行中并返回 true。
58方法结束指定邀请,匹配进行中标记则清空;成功时把 id 记为已处理。
68方法观测到本次启动没有邀请时清空已处理 id,使后续同一邀请可以重新进入。
76属性构造时固定的冷却时长,单位毫秒,只读。
78方法保存固定的冷却毫秒数,初始允许时间为 0,即第一次调用总能进入。
82方法当前时间早于允许时间就返回 false,否则立即进入冷却并返回 true。
90方法把下次允许时间设为传入时间加冷却时长,重复调用以最后一次为准。