t1k:cocos:playable:async-utilities
| Field | Value |
|---|---|
| Module | playable |
| Version | 0.5.6 |
| Effort | high |
| Tools | — |
Keywords: ads, async, cancellation token, cocos, playable, task, utilities
How to invoke
Section titled “How to invoke”/t1k:cocos:playable:async-utilitiesAsyncTask & CancellationToken — Async Utilities
Section titled “AsyncTask & CancellationToken — Async Utilities”UniTask-inspired async helpers for Cocos Creator. All methods are static on AsyncTask. See also: t1k-cocos-playable-signalbus (for waitFor), t1k-cocos-playable-lifecycle.
Import paths:
db://assets/PLAGameFoundation/async/AsyncTaskdb://assets/PLAGameFoundation/async/CancellationToken
Key APIs
Section titled “Key APIs”Timing
Section titled “Timing”await AsyncTask.Delay(1000); // msawait AsyncTask.DelaySeconds(1.5); // seconds (wraps Delay)await AsyncTask.Yield(); // next rAF frameawait AsyncTask.WaitForFrames(3); // N framesawait AsyncTask.WaitForFixedUpdate(); // ~0.02s fixed step approximationawait AsyncTask.FromSchedule(this, 0.5); // uses Cocos scheduleOnce (seconds)Condition Polling (checked every rAF frame)
Section titled “Condition Polling (checked every rAF frame)”await AsyncTask.WaitUntil(() => this.isReady);await AsyncTask.WaitWhile(() => this.isLoading); // inverse of WaitUntilConcurrency
Section titled “Concurrency”// All complete (typed overload for mixed types)await AsyncTask.WhenAll([loadA(), loadB(), loadC()]);await AsyncTask.WhenAllTyped([loadSprite(), loadAudio()] as const);
// First winsawait AsyncTask.WhenAny([attempt1(), attempt2()]);
// Sequential — one after another, collects resultsconst results = await AsyncTask.Sequence([() => step1(), () => step2()]);
// Parallel with concurrency cap (default 5)const results = await AsyncTask.Parallel([() => fetch(a), () => fetch(b)], 3);Retry with Exponential Backoff
Section titled “Retry with Exponential Backoff”// Attempts: 0..maxRetries. Delays: 1s, 2s, 4s, ...const data = await AsyncTask.Retry(() => this.fetchData(), 3, 1000, cts.token);Deferred
Section titled “Deferred”// External resolve/reject — useful as an async gateconst deferred = AsyncTask.Deferred<boolean>();someCallback = () => deferred.resolve(true);const result = await deferred.promise;CancellationToken
Section titled “CancellationToken”const cts = new CancellationTokenSource();
// Pass token into cancellable opsawait AsyncTask.Delay(5000, cts.token);await AsyncTask.WaitUntil(() => ready, cts.token);await AsyncTask.Retry(() => load(), 3, 1000, cts.token);
// Cancel from anywherects.cancel(); // triggers rejection in all registered opscts.dispose(); // same as cancel(), call when done
// Manual check inside a loopfor (const item of items) { cts.token.throwIfCancelled(); // throws if cancelled if (cts.token.isCancelled) break; await process(item);}
// Register teardown callbackcts.token.register(() => cleanupResources());Workflow: Common Patterns in Playable Ads
Section titled “Workflow: Common Patterns in Playable Ads”Loading gate — wait for parameters then animate in:
async onLoad() { await AsyncTask.WaitUntil(() => PlayableConfig.isReady); await AsyncTask.DelaySeconds(0.3); this.playIntroAnimation();}Cancellable tutorial sequence:
private _cts: CancellationTokenSource;
async startTutorial() { this._cts = new CancellationTokenSource(); try { await AsyncTask.DelaySeconds(1, this._cts.token); this.showHand(); await AsyncTask.DelaySeconds(2, this._cts.token); this.hideHand(); } catch { /* cancelled */ }}
onFirstInteraction() { this._cts?.cancel();}Component-safe delay (avoids rAF after destroy):
// Prefer FromSchedule inside Cocos Components — respects component lifecycleawait AsyncTask.FromSchedule(this, 1.0);Common Mistakes
Section titled “Common Mistakes”- Using
Delay/WaitUntilinside a destroyed Component — rAF keeps running. UseFromScheduleor cancel viaCancellationToken. - Not catching cancellation —
Delayrejects with"Operation was cancelled". Wrap in try/catch. WaitUntilwith an expensive condition — runs every frame. Keep the lambda fast.Paralleldefault concurrency is 5 — lower it for network-bound operations.
Gotchas
Section titled “Gotchas”- Promises survive scene unload — outstanding
setTimeout/Promisecallbacks fire after the scene is gone unless explicitly cancelled. Tie all timers to a CancellationToken bound to component lifecycle. async/awaitin Cocosupdate()deopts — use coroutine-like primitives (Tween chains) for per-frame work.- Unhandled rejections silently disappear in playable runtime — wrap top-level
.catch(reportToHost).