Skip to content

Level: an async option on load/reload/next/previous, and a microtask deferral - #1647

Merged
obiot merged 17 commits into
masterfrom
feat/level-load-async
Sep 6, 2026
Merged

Level: an async option on load/reload/next/previous, and a microtask deferral#1647
obiot merged 17 commits into
masterfrom
feat/level-load-async

Conversation

@obiot

@obiot obiot commented Sep 5, 2026

Copy link
Copy Markdown
Member

Closes #1646.

The timer

level.load() deferred its work with a timer so the current frame could unwind before the world is reset. The deferral is still needed — it is routinely called from a trigger handler mid-loop, and safeLoadLevel resets and destroys the very container the loop may be iterating, while state.stop() only sets a flag.

The timer itself is a 2011 artefact: that line and its comment trace to v0.9.0, four years before promises existed. Browsers clamp a timer to ≥1 s in a background tab, so a load queued as the tab hides sat behind that clamp. A microtask drains when the JS stack empties — the end of the rAF callback holding update and draw — so it unwinds the frame identically and is not clamped.

The no-loop branch stays synchronous exactly as before.

The async option

load, reload, next and previous each keep one name and gain a flag:

level.load("map1");                          // boolean, exactly as before
await level.load("map1", { async: true });   // Promise<boolean>
if (await level.next({ async: true })) {  }

options.onLoaded still fires either way, so the forms mix.

No type break. The signatures are preserved as JSDoc @overload pairs, not a boolean | Promise<boolean> union — a union fails every existing const ok: boolean = level.load(id) with TS2322, which I verified before choosing. The overload form compiles both that and the awaited call against the real emitted build:

const ok: boolean = level.load("map1");                       // still compiles
const a: boolean = await level.load("map1", { async: true });  // Promise<boolean>

Running out of levels still reports false rather than rejecting — reaching the end of a game is an ordinary outcome. An unknown level id throws synchronously in both forms: that is a typo, not a load failure, and it should not need await to surface.

The bounds check next and previous each spelled out is now a shared levelIdAt(offset) helper, so the two cannot drift.

Trigger stops rewriting its caller's options

The fade/mask path sequenced hide → load → reveal by replacing settings.onLoaded with its own function and calling the user's from inside — mutating an option object the caller owns. Awaiting the load removes the interception.

The viewport is deliberately re-read after the load: game.reset() reassigns app.viewport, which is precisely why the callback this replaces read it late.

The cost of putting the switch in the options

await level.load(id) without the flag is silent, because await true is valid JavaScript. It happens to be harmless today — the deferral is a single microtask queued before the await's continuation, so the load still runs first — but that is incidental ordering, not a contract. Documented on the LevelLoadOptions typedef and pinned by a test that records exactly this.

Tests

No spec called level.load() at all before this, so both files are new — 24 tests across the legacy contract, the flag, the scheduling, the reload/next/previous paths, and the trigger.

Nine mutations of the changed behaviour fail as they should: the flag ignored, the microtask reverted to a timer, the unknown id rejecting instead of throwing, the loop not stopped, the synchronous branch throwing instead of rejecting, next/previous off-by-one, the onLoaded wrap reintroduced, and a stale viewport captured before the load.

A tenth (rewriting the bounds helper as levelIdx[index] ?? null) survives, and is an equivalent mutant rather than a gap: out-of-range array access is already undefined, so the explicit bounds check is belt-and-braces. The observable behaviour is covered by the two off-by-one mutations.

The viewport guard is a source check rather than a behavioural test — the reveal only runs when the hide tween completes, which needs a live game loop the suite does not have. It was vacuous on the first attempt (the explanatory comment in the inspected slice contained the string it asserted on), so comment lines are stripped before matching, and it was re-verified against the mutation.

Also

reload() was documented as returning object — "the current level" — but returns whatever load() returns. The 2011 original returned nothing at all, so the declaration was never right. Corrected to boolean; getCurrentLevel() is the call that hands back the level object.

Verification

276 files, 6716 tests, 0 failures. Lint 0 errors, build clean, emitted overloads and legacy compatibility both checked against the built types.

`level.load()` deferred its work with a timer so the current frame could
unwind before the world is reset. The deferral is still needed — it is
routinely called from a trigger handler mid-loop, and `safeLoadLevel`
resets and destroys the very container the loop may be iterating, while
`state.stop()` only sets a flag — but the timer is a 2011 artefact. That
line and its comment date to v0.9.0, four years before promises existed;
there was never a macrotask semantic to preserve. Browsers clamp a timer
to at least a second in a background tab, so a load queued as the tab
hides was stranded behind that clamp. A microtask drains when the JS stack
empties, which unwinds the frame just the same and is not clamped.

The no-loop branch stays synchronous exactly as before: with no loop there
is no frame to unwind, and deferring would change when the level exists
for anyone loading one before the game starts.

`loadAsync()` then returns that completion instead of discarding it.
`load()` is unchanged and still returns `true` — the emitted type stays
`boolean`, so a typed consumer doing `const ok: boolean = level.load(id)`
keeps compiling, which is why this is a sibling rather than a changed
return type. `options.onLoaded` still fires either way. An unknown level
id throws synchronously rather than rejecting: that is a typo, not a load
failure, and it should not need `await` to surface. `load()` rethrows a
rejection on a clean stack so a failure still surfaces as an uncaught
error the way it did under the timer, rather than as a silent unhandled
rejection.

`Trigger` stops rewriting its caller's options. Its fade/mask path
sequenced hide → load → reveal by replacing `settings.onLoaded` with its
own function and calling the user's from inside it; awaiting the load
removes that interception. The viewport is deliberately re-read after the
load — `game.reset()` reassigns `app.viewport`, which is exactly why the
callback this replaces read it late.

Tests: no spec called `level.load()` at all before this, so both files are
new. Fifteen tests over the legacy contract, the new method, the
scheduling, and the trigger paths; all eight mutations of the changed
behaviour fail as they should, including a source guard on the viewport
re-read, which the reveal path cannot cover behaviourally because its
tween needs a live loop.

Closes #1646

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI lite review requested due to automatic review settings September 5, 2026 07:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are a few correctness/docs issues to address (notably loadAsync() error-surface consistency and a non-Markdown {@link ...} tag in the changelog).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR modernizes level-loading scheduling by replacing the legacy setTimeout deferral with a microtask-based deferral, and introduces an awaitable level.loadAsync() to let callers observe completion without changing the existing level.load() boolean-return contract. It also updates Trigger’s level-transition path to avoid mutating caller-owned settings.onLoaded, and adds new Vitest coverage for the behavior and scheduling.

Changes:

  • Add level.loadAsync(levelId, options): Promise<void> while keeping level.load()’s boolean return and “fire-and-forget” behavior.
  • Replace timer-based deferral with a microtask deferral when the game loop is running; preserve synchronous load behavior when no loop is running.
  • Refactor trigger level transitions to await the load (via loadAsync) instead of wrapping/overwriting settings.onLoaded, and add new tests.
File summaries
File Description
packages/melonjs/src/level/level.js Adds loadAsync(), refactors load() to delegate and rethrow failures, and replaces timer deferral with a microtask deferral when the loop is running.
packages/melonjs/src/renderable/trigger.js Updates transition path to use level.loadAsync(...).then(...) for reveal instead of rewriting settings.onLoaded.
packages/melonjs/tests/level_load_async.spec.js New tests for loadAsync, legacy load() contract, and microtask-vs-timer scheduling behavior.
packages/melonjs/tests/trigger_level_change.spec.js New tests ensuring trigger transition behavior doesn’t overwrite caller callbacks and that load is deferred until hide completes.
packages/melonjs/CHANGELOG.md Documents the new API and the background-tab timer clamp fix.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/melonjs/src/level/level.js Outdated
Comment on lines +194 to +198
this.loadAsync(levelId, options).catch((error) => {
queueMicrotask(() => {
throw error;
});
});
Comment thread packages/melonjs/src/level/level.js Outdated
Comment on lines +270 to +271
safeLoadLevel(levelId, options);
return Promise.resolve();
Comment on lines +204 to +209
.catch((error) => {
// same loudness as the fire-and-forget form
queueMicrotask(() => {
throw error;
});
});
Comment thread packages/melonjs/CHANGELOG.md Outdated
## [20.4.0] (melonJS 2) - _unreleased_

### Added
- `level.loadAsync(levelId, options)` — the same load as {@link level.load}, resolving once the level is in the world instead of discarding the completion. `options.onLoaded` still fires, so the two forms mix freely, and `load()` is unchanged and still returns `true`. An unknown level id throws synchronously rather than rejecting: that is a typo, not a load failure, and it should not need `await` to surface ([#1646](https://github.com/melonjs/melonJS/issues/1646))
`loadAsync()` on its own left the other three loading calls with no
awaitable form, so a game could await its first level but not a reload or
a level transition.

Each twin resolves with exactly what its synchronous counterpart returns,
which makes a port mechanical: `if (level.next())` becomes
`if (await level.nextAsync())`. That is why `loadAsync()` now resolves
`true` rather than `void` — the rule is worth more than the slightly
noisier type.

Running out of levels resolves `false` without loading anything rather
than rejecting: `next()` returns `false` there, and reaching the end of a
game is an ordinary outcome, not an error.

The four originals are untouched, and their emitted types are unchanged —
`load`, `next` and `previous` still declare `boolean`.

Not included: `reload()` declares `object` from a stale `@returns {object}
the current level`, but it returns whatever `load()` returns. Correcting
that would change an emitted type, which is the one thing this change set
is careful not to do, so it is left alone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
@obiot

obiot commented Sep 5, 2026

Copy link
Copy Markdown
Member Author

Added the three missing twins — reloadAsync(), nextAsync(), previousAsync(). loadAsync() alone left a game able to await its first level but not a reload or a level transition.

Each resolves with exactly what its synchronous counterpart returns, so a port is mechanical:

if (level.next())                 if (await level.nextAsync())

That is why loadAsync() now resolves true rather than void — the rule is worth more than the marginally noisier type.

Running out of levels resolves false without loading anything rather than rejecting. next() returns false there, and reaching the end of a game is an ordinary outcome, not an error.

Emitted types, verified from build/level/level.d.ts — all four originals unchanged:

load boolean loadAsyncPromise<boolean>
reload object reloadAsyncPromise<boolean>
next boolean nextAsyncPromise<boolean>
previous boolean previousAsyncPromise<boolean>

Six more tests, and five mutations of the new behaviour all fail as they should: nextAsync resolving true at the end, loading past the end, a previousAsync off-by-one at the start, reloadAsync not reloading, and loadAsync resolving undefined.

Noticed but deliberately not fixed: reload() declares object above, from a stale @returns {object} the current level — it actually returns whatever load() returns. Correcting it would change an emitted type, which is precisely what this PR is careful not to do, so it is left for a separate call.

Suite now 276 files, 6713 tests, 0 failures.

Copilot AI review requested due to automatic review settings September 5, 2026 08:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

It introduces unconditional queueMicrotask usage (risking runtime ReferenceError in unsupported environments) and the PR description’s stated scope conflicts with the included async twin APIs.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

packages/melonjs/src/level/level.js:198

  • load() rethrows rejections using queueMicrotask, but this global is not guaranteed to exist in all runtimes. If it’s missing, a rejected loadAsync() will cause a ReferenceError here instead of surfacing the original failure. Consider a small fallback to setTimeout when queueMicrotask is unavailable.
	load(levelId, options) {
		// Fire-and-forget by contract: this returns `true`, not the promise, so
		// existing (including typed) callers are unaffected. Use `loadAsync()`
		// to await the load. The rejection is rethrown on a clean stack so a
		// failure still surfaces as an uncaught error the way it did when the
		// deferral was a timer, rather than as a silent unhandled rejection.
		this.loadAsync(levelId, options).catch((error) => {
			queueMicrotask(() => {
				throw error;
			});
		});

packages/melonjs/src/renderable/trigger.js:209

  • This catch path rethrows via queueMicrotask, which may be undefined in some runtimes; in that case the code would throw a ReferenceError and potentially mask the real level-load failure. Using a simple fallback (e.g. setTimeout) keeps the intended “uncaught” loudness without requiring queueMicrotask support.
							})
							.catch((error) => {
								// same loudness as the fire-and-forget form
								queueMicrotask(() => {
									throw error;
								});
							});
  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread packages/melonjs/CHANGELOG.md Outdated
Comment on lines +5 to +6
### Added
- `level.loadAsync()`, `reloadAsync()`, `nextAsync()` and `previousAsync()` — awaitable twins of the four level-loading calls, resolving once the level is actually in the world instead of discarding the completion. Each resolves with exactly what its synchronous twin returns, so a port is mechanical: `if (level.next())` becomes `if (await level.nextAsync())`, and running out of levels still resolves `false` rather than rejecting. `options.onLoaded` still fires, so the two forms mix freely, and the originals are unchanged — `load()` still returns `true`, and its emitted type is still `boolean`. An unknown level id throws synchronously rather than rejecting: that is a typo, not a load failure, and it should not need `await` to surface ([#1646](https://github.com/melonjs/melonJS/issues/1646))
`@returns {object} the current level` was never true. `reload()` is
`return this.load(...)`, and `load()` returns `true` — and the 2011
original returned nothing at all, so the declaration has been wrong for
the method's entire life. `getCurrentLevel()` is the call that hands back
the level object.

This corrects the emitted type from `object` to `boolean`. A
`const lvl: object = level.reload()` that compiled while receiving `true`
now fails to compile, which surfaces a bug that was already there rather
than introducing one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 5, 2026 08:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It changes core level-loading scheduling and trigger transition sequencing in a way that can have subtle runtime/event-loop effects best validated by a human reviewer.

Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Review catch. The deferred branch turned a failing load into a rejection,
but the synchronous one let the exception escape the call — so the error
surface depended on whether the loop happened to be running, and
`loadAsync(...).catch()` could never see the synchronous case, because the
throw beat the handler being attached.

The unknown-id check still throws synchronously, before either branch:
that is a typo rather than a load failure, and should not need `await`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 5, 2026 08:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

level.load() now forces some previously synchronous failures (when the loop isn’t running) to become asynchronous throws via loadAsync().catch(...), which is a behavior/contract change that should be addressed before merging.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread packages/melonjs/src/level/level.js Outdated
Comment on lines +188 to +199
load(levelId, options) {
// Fire-and-forget by contract: this returns `true`, not the promise, so
// existing (including typed) callers are unaffected. Use `loadAsync()`
// to await the load. The rejection is rethrown on a clean stack so a
// failure still surfaces as an uncaught error the way it did when the
// deferral was a timer, rather than as a silent unhandled rejection.
this.loadAsync(levelId, options).catch((error) => {
queueMicrotask(() => {
throw error;
});
});
return true;
Eight loading methods for four operations was too much surface. The switch
moves into the options object the calls already take, so `load`, `reload`,
`next` and `previous` each keep one name and gain a flag.

No type break, which is the part that had to be got right. The signatures
are preserved as JSDoc `@overload` pairs rather than a
`boolean | Promise<boolean>` union — a union would fail every existing
`const ok: boolean = level.load(id)`, verified with tsc. The overload form
compiles both that and `await level.load(id, { async: true })` against the
real emitted build.

The bounds check `next` and `previous` each spelled out is now a shared
`levelIdAt(offset)` helper, so the two cannot drift.

The cost of putting the switch in the options is that `await level.load(id)`
without the flag is silent — `await true` is valid. It happens to be
harmless today, since the deferral is a single microtask queued before the
await's continuation, so the load still runs first; that is incidental
ordering rather than a contract. Documented on the options typedef and
pinned by a test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 5, 2026 08:58
@obiot obiot changed the title Level: add level.loadAsync(), and defer with a microtask Level: an async option on load/reload/next/previous, and a microtask deferral Sep 5, 2026
@obiot

obiot commented Sep 5, 2026

Copy link
Copy Markdown
Member Author

Responses to the review, plus a design change since it was written.

queueMicrotaskPromise.resolve().then(() => { throw error }) (two comments): declining. The two are not equivalent for this purpose, measured in the browser:

queueMicrotask(() => { throw e })          → ["uncaught-error"]
Promise.resolve().then(() => { throw e })  → ["unhandled-rejection"]

That rethrow exists specifically to preserve the uncaught error the timer-based version produced. The suggested form would turn it into an unhandled rejection, which is the behaviour change the code is there to avoid. On availability: queueMicrotask is baseline in every browser since 2018 and Node 11, comfortably older than the ES2022 target this package already requires.

loadAsync() error-surface inconsistency: valid, fixed. The deferred branch produced a rejection while the synchronous one let the exception escape the call, so the surface depended on state.isRunning() — and .catch() could never see the synchronous case, since the throw beat the handler being attached. The synchronous branch now returns a rejected promise, with a test asserting both branches reject identically. Good catch.

{@link ...} in the changelog: already gone — the entry had been rewritten before the review ran.

PR description out of step with scope: it was, and it is now further out of step, because the API changed after this review. The description has been rewritten.

Design change: the four *Async siblings are gone. Eight loading methods for four operations was too much surface, so the switch moved into the options object: level.load(id, { async: true }), and the same on reload/next/previous.

The signatures are preserved as JSDoc @overload pairs rather than a boolean | Promise<boolean> union — the union form fails every existing const ok: boolean = level.load(id) with TS2322, verified with tsc before choosing. The overload form compiles both the legacy call and the awaited one against the real emitted build.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are a few correctness/robustness issues in the newly added docs/tests and a missing queueMicrotask fallback that could cause runtime failures in unsupported environments.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

packages/melonjs/src/renderable/trigger.js:208

  • queueMicrotask is used here to rethrow load failures, but it isn’t feature-detected or polyfilled in this repo. In runtimes without queueMicrotask, this catch handler will throw a ReferenceError and may mask the original error. Consider falling back to setTimeout for the rethrow path.
								// same loudness as the fire-and-forget form
								queueMicrotask(() => {
									throw error;
								});
  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Lite

Comment thread packages/melonjs/src/level/level.js Outdated
Comment on lines +260 to +262
queueMicrotask(() => {
throw error;
});
Comment thread packages/melonjs/src/level/level.js Outdated
Comment on lines +109 to +111
* Note that awaiting a call WITHOUT `async: true` is not an error — `await true`
* is valid and resolves immediately — so the level will not be loaded yet. Pass
* the flag whenever you intend to await.
Comment on lines +100 to +103
const load = triggerSource.indexOf(
"load(gotolevel, { ...settings, async: true })",
);
const reveal = triggerSource.indexOf("addCameraEffect", load);
Left over from the rename to the `async` option.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 5, 2026 09:29
`melonjs-tilemaps` described the deferral as a `setTimeout` and told the
reader the only way to sequence work after a load was `onLoaded` or
`LEVEL_LOADED`. Both are now out of date: the deferral is a microtask, and
`async: true` gives a promise to await.

Also states the caveat that comes with putting the switch in the options —
`await level.load(id)` without the flag returns a boolean, so it does not
await the load. It happens to finish first today, because the deferral is
a single microtask queued ahead of the await's continuation, but that is
incidental ordering rather than a contract, and the skills say so rather
than implying either that it is safe or that it is broken.

`melonjs-3d-assets` gains the `async` row in its `level.load` options
table and the same note; glTF/GLB scenes load through the same call.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes are cohesive, preserve legacy behavior, and are backed by targeted tests covering the new async contract, scheduling, and Trigger sequencing.

Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 5, 2026 09:33
The promise branch wrapped the deferral in a `new Promise` executor with a
try/catch to funnel a throw into `reject`. A `.then` already turns a throw
into a rejection, so a small `nextTask()` helper — a promise that settles
on the next task, through the engine's own `defer` — removes the executor,
the try/catch and the manual resolve/reject.

Deliberately not converted further. Nothing in the level lifecycle is
actually asynchronous: both `addTo` implementations, glTF and TMX, are
fully synchronous. Making `safeLoadLevel` async would buy syntax and cost
atomicity — it currently runs to completion inside one task, so the world
is never observable half-built, and spreading it across microtask ticks
would give up that property for nothing.

The fire-and-forget branch keeps calling `defer` directly. Routing it
through `nextTask()` would create a promise, and a promise is exactly what
must not exist there: with nothing holding it, a failed load would become
a silent unhandled rejection instead of an uncaught error.

Adds the test that made the refactor honest. The existing macrotask test
drives the fire-and-forget path, which calls `defer` directly, so it never
touched the promise branch — `nextTask()` could have reverted to a
microtask unnoticed. Now pinned for both.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 6, 2026 00:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The implementation and inline API docs describe timer/macrotask deferral, which conflicts with the PR description’s “microtask deferral” claim and includes multiple “microtask” doc strings that don’t match the actual scheduling.

Review details

Suppressed comments (5)

packages/melonjs/src/level/level.js:215

  • The docs here say the load is deferred to a microtask while the loop is running, but the implementation below schedules via defer, which is a setTimeout(..., 0) macrotask. This should say timer/macrotask to match real behavior.
	 * While the game loop is running the load is DEFERRED to a microtask, so
	 * this returns before anything is in the world. Sequence follow-up work from
	 * `options.onLoaded`, from an `event.LEVEL_LOADED` listener, or by passing
	 * `async: true` and awaiting the promise that overload returns.

packages/melonjs/src/level/level.js:345

  • This comment says reload is deferred to a microtask, but level.load() defers via defer (setTimeout), i.e. a timer/macrotask. Updating this avoids misleading API docs.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:384

  • This comment says next() is deferred to a microtask, but the deferral is a timer/macrotask via defer (setTimeout). Please align wording with the actual scheduling semantics.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:270

  • The PR description/linked issue text calls out replacing the old setTimeout deferral with a microtask to avoid background-tab timer clamping, but this implementation explicitly keeps using a timer/macrotask via defer (which is setTimeout(..., 0)). That means background clamping behavior remains; either the PR description/title needs updating, or the scheduling should be changed to an actual microtask (e.g. queueMicrotask/Promise.resolve().then(...)) and the associated tests updated accordingly.
		// A TIMER, through the engine's own `defer`, and deliberately not a
		// microtask. A macrotask cannot run inside another, so the load lands
		// after the current frame whatever the frame does. A microtask only
		// lands there while the whole update-and-draw path stays synchronous —
		// true today, but it would start running mid-frame the day anything in
		// that path awaits, silently and with nothing to catch it.

packages/melonjs/src/level/level.js:425

  • This comment says previous() is deferred to a microtask, but the deferral is a timer/macrotask via defer (setTimeout). Please align wording with the actual scheduling semantics.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

One definition, one call site — the same shape as the `scheduleLoad`
helper removed earlier. A private function plus a JSDoc block to name a
three-line idiom is more ceremony than the name is worth.

The `.then` structure stays, and the comment now says why it is
load-bearing rather than incidental: a throw from inside `defer`'s
callback escapes the promise executor entirely, leaving the promise
pending forever. Raised one level up, in `.then`, it rejects — which is
what removes the try/catch.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 6, 2026 00:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

There are documented semantics claiming “microtask” deferral (and PR text referencing a microtask switch) that conflict with the actual timer/macrotask scheduling used in the implementation and tests.

Review details

Suppressed comments (6)

packages/melonjs/src/level/level.js:202

  • This JSDoc says the deferred branch is a “microtask”, but the implementation explicitly schedules via utils.function.defer (setTimeout), i.e. a timer/macrotask. The docs should match the actual scheduling semantics.
	 * While the game loop is running the load is DEFERRED to a microtask, so
	 * this returns before anything is in the world. Sequence follow-up work from
	 * `options.onLoaded`, from an `event.LEVEL_LOADED` listener, or by passing
	 * `async: true` and awaiting the promise that overload returns.

packages/melonjs/src/level/level.js:336

  • This overload doc claims the load is deferred to a microtask, but level.load() actually defers via utils.function.defer (setTimeout) when the loop is running. Update wording to avoid misleading callers about scheduling and background-tab timer clamping.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:375

  • This doc mentions “microtask” deferral, but next()/previous() just delegate to level.load(), which defers via utils.function.defer (setTimeout) while running. Align the wording with the actual macrotask/timer behavior.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:416

  • This doc mentions “microtask” deferral, but previous() delegates to level.load(), which defers via utils.function.defer (setTimeout) while running. Update wording to reflect the macrotask/timer scheduling.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/tests/level_load_async.spec.js:21

  • The file header comment says level.load() deferral is “now a microtask”, but the code under test (and several assertions below) treats it as timer/macrotask deferral via utils.function.defer (setTimeout). This comment should be corrected to avoid contradicting the tests’ intent.
 * The deferral in `level.load()` dates to 2011 and used a timer because that
 * was the only way to defer at the time. It is still needed — `level.load()` is
 * routinely called from inside the loop, and `safeLoadLevel` resets and
 * destroys the very container the loop may be iterating — but it is now a
 * microtask, and `async: true` hands that completion back instead of a boolean.

packages/melonjs/src/level/level.js:256

  • The PR description/title claims level loading switched from a timer to a microtask to avoid background-tab timer clamping, but the implementation here explicitly keeps a timer/macrotask (via utils.function.defer/setTimeout) and even calls out “deliberately not a microtask”. Either update the PR description to match the shipped behavior, or change the scheduling to a true microtask and adjust tests/docs accordingly.
		// A TIMER, through the engine's own `defer`, and deliberately not a
		// microtask. A macrotask cannot run inside another, so the load lands
		// after the current frame whatever the frame does. A microtask only
		// lands there while the whole update-and-draw path stays synchronous —
		// true today, but it would start running mid-frame the day anything in
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

`triggerEvent` schedules the load through `defer`, so a test that fires a
trigger leaves a timer pending. `afterAll` then destroyed the application,
and the callback landed on a torn-down world:

    TypeError: Cannot read properties of undefined (reading 'set')
      World.reset      physics/world.js:266
      safeLoadLevel    level/level.js:23

Vitest reports that as a fatal unhandled error while still counting every
test as passed, so the suite reads green and the job fails. The level spec
already flushed for this reason when the deferral moved back to a timer;
the trigger spec was missed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 6, 2026 00:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Several updated JSDoc comments and the PR narrative describe microtask-based deferral, but the implementation still defers via defer/setTimeout (timer/macrotask), creating user-facing documentation and expectation mismatches that should be resolved.

Review details

Suppressed comments (5)

packages/melonjs/src/level/level.js:202

  • The JSDoc says the load is deferred to a microtask while the loop is running, but the implementation explicitly defers via utils.function.defer (setTimeout), i.e. a timer/macrotask. This mismatch is likely to mislead users reading the API docs about when the world is actually populated.
	 * While the game loop is running the load is DEFERRED to a microtask, so
	 * this returns before anything is in the world. Sequence follow-up work from
	 * `options.onLoaded`, from an `event.LEVEL_LOADED` listener, or by passing
	 * `async: true` and awaiting the promise that overload returns.

packages/melonjs/src/level/level.js:336

  • This docstring says reload is deferred to a microtask when the loop is running, but level.load/reload defer via utils.function.defer (setTimeout), i.e. a timer/macrotask. Keeping the docs consistent with the actual scheduling avoids confusion around ordering.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:375

  • This docstring says next() is deferred to a microtask when the loop is running, but the actual scheduling is via utils.function.defer (setTimeout), i.e. a timer/macrotask. The wording should match the real behavior so callers know what they can safely sequence after the call.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:416

  • This docstring says previous() is deferred to a microtask when the loop is running, but the actual scheduling is via utils.function.defer (setTimeout), i.e. a timer/macrotask. Aligning the docs with behavior matters for callers relying on precise ordering.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:257

  • The PR description/issue text says the setTimeout deferral was replaced with a microtask to avoid background-tab timer clamping, but the implementation still defers through utils.function.defer, which is setTimeout(..., 0). If the goal is to eliminate timer clamping, this scheduling strategy won’t achieve that; if the goal changed to “centralize on defer”, the PR title/description (and issue closure) should be updated to match.
		// A TIMER, through the engine's own `defer`, and deliberately not a
		// microtask. A macrotask cannot run inside another, so the load lands
		// after the current frame whatever the frame does. A microtask only
		// lands there while the whole update-and-draw path stays synchronous —
		// true today, but it would start running mid-frame the day anything in
		// that path awaits, silently and with nothing to catch it.
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

`container`, `setViewportBounds` and `flatten` are pre-existing defaults
this change does not touch, but nothing called `level.load` before, so
nothing pinned them either — and they are the contract a game gets when it
passes no options at all.

- `container` defaults to the application's world
- `setViewportBounds` defaults to TRUE on the TMX arm, which is what calls
  `viewport.setBounds`; asserted against an explicit `false` so the default
  is what is being measured rather than the code path merely running
- `flatten` defaults to the application's `mergeGroup` rather than to a
  constant, so it is pinned in both positions

Flipping each default fails a test. Replacing the container default with a
different container fails too, though loudly — the assertion is a direct
identity check against the app's world.

The TMX map fixture moves to the top-level hook. It was registered inside
a sibling describe, so the new tests only saw it through declaration
order, which is not something to rely on. That adds a third level to the
registry, so the next/previous boundary tests now name the last level
instead of assuming which one it is.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 6, 2026 04:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are correctness/documentation inconsistencies around timer-vs-microtask deferral and at least one Trigger transition sequencing edge case (reveal ordering/behavior) plus a test that awaits only microtasks despite macrotask scheduling.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (7)

Previously missed (1) — in code that hasn't changed since the last review.

packages/melonjs/src/level/level.js:180

  • The summary line for level.load says it "return[s] a promise" unconditionally, but the legacy (non-async) overload still returns a boolean. This wording is likely to confuse readers skimming docs/API reference.

packages/melonjs/src/level/level.js:202

  • Docs say the deferred load is "a microtask", but the implementation explicitly schedules via defer()/setTimeout(..., 0) (a macrotask). Please align the JSDoc wording with the actual scheduling semantics.
	 * While the game loop is running the load is DEFERRED to a microtask, so
	 * this returns before anything is in the world. Sequence follow-up work from
	 * `options.onLoaded`, from an `event.LEVEL_LOADED` listener, or by passing
	 * `async: true` and awaiting the promise that overload returns.

packages/melonjs/tests/trigger_level_change.spec.js:164

  • This test only awaits microtasks, but level.load(..., { async: true }) defers the actual load via utils.function.defer, which uses setTimeout(..., 0) (a macrotask). Without awaiting a timer tick, loaded/seen may still be unchanged when assertions run.
		// let the load's microtask and the reveal chained after it settle
		for (let i = 0; i < 4; i++) {
			await Promise.resolve();
		}

packages/melonjs/src/level/level.js:336

  • This JSDoc mentions deferring to "a microtask", but level.load currently defers via defer()/setTimeout when the loop is running. Keeping these docs consistent avoids incorrect assumptions about ordering.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:375

  • This JSDoc mentions deferring to "a microtask", but level.load currently defers via defer()/setTimeout when the loop is running. Keeping these docs consistent avoids incorrect assumptions about ordering.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:416

  • This JSDoc mentions deferring to "a microtask", but level.load currently defers via defer()/setTimeout when the loop is running. Keeping these docs consistent avoids incorrect assumptions about ordering.
	 * While the game loop is running the load is deferred to a microtask, so this
	 * returns before anything is in the world — see {@link level.load}.

packages/melonjs/src/level/level.js:257

  • The PR description/title emphasize switching the deferral to a microtask, but the implementation here explicitly chooses a timer/macrotask via defer() (setTimeout) and calls out "deliberately not a microtask". If the intended behavior is macrotask deferral, the PR description/title should be updated; if microtask deferral is still desired, the implementation/tests/docs should be revisited for consistency.
		// A TIMER, through the engine's own `defer`, and deliberately not a
		// microtask. A macrotask cannot run inside another, so the load lands
		// after the current frame whatever the frame does. A microtask only
		// lands there while the whole update-and-draw path stays synchronous —
		// true today, but it would start running mid-frame the day anything in
		// that path awaits, silently and with nothing to catch it.
  • Files reviewed: 9/9 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment on lines +178 to +182
level
.load(gotolevel, { ...settings, async: true })
.then(() => {
// re-read AFTER the load: `game.reset()` reassigns
// `app.viewport`, so a viewport captured before it
…deterministic

Four JSDoc blocks and the level spec's header still said the running-loop
load is deferred to a microtask. It is a timer again, through `defer` —
the wording was left behind by the revert.

The trigger reveal test yielded only microtasks between tween ticks while
the load it waits for is timer-deferred. It passed, but on scheduling that
nothing in the test guarantees: `loaded` reached 1 during a microtask-only
loop, which is not a property to rely on. It yields to a macrotask now, and
both mutations it exists for still fail — a stale viewport captured before
the load, and a reveal that does not wait for it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
@obiot

obiot commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Latest round.

Stale microtask wording (level.js:199, :335, :374, :415, and the spec header): correct, fixed. The deferral went back to a timer via defer and the docs did not follow. Good catch — that is exactly the kind of drift that outlives a revert.

The reveal test only yielding to microtasks: correct, and worse than it looked. The test passed, but loaded reached 1 during a microtask-only loop, which nothing in the test guarantees — it was measuring whatever the scheduler happened to do. It yields to a macrotask now. Both mutations it exists for still fail: a stale viewport captured before the load, and a reveal that does not wait for the load.

Reveal ordering vs state.restart(): the observation is right, the consequence does not follow, so no change.

The reveal did move from before state.restart() to after it. But it cannot produce a frame of unfaded level, because state.restart() schedules a requestAnimationFrame while the reveal is a promise continuation — a microtask in the same task the load ran in. Microtasks drain before the next animation frame, so the reveal is always in place before anything renders.

I wrote a test to pin that and then deleted it: asserting "no frame intervened" from outside the scheduler is racy, and the property is a language guarantee rather than engine behaviour. The existing test already pins the part that is ours — the reveal happens after the load, on the viewport that exists after it.

The other half of your point is real and intended: the caller's onLoaded now runs before the reveal rather than after. That is the interception being removed. The trigger no longer rewrites an options object it was handed, which is the defect this part of the PR exists to fix.

Copilot AI review requested due to automatic review settings September 6, 2026 07:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The implemented scheduling remains timer/macrotask-based (via defer/setTimeout), which materially conflicts with the PR title/description and issue scope claiming a microtask deferral and its background-tab unclamping benefits.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 9/9 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment on lines +252 to +256
// A TIMER, through the engine's own `defer`, and deliberately not a
// microtask. A macrotask cannot run inside another, so the load lands
// after the current frame whatever the frame does. A microtask only
// lands there while the whole update-and-draw path stays synchronous —
// true today, but it would start running mid-frame the day anything in
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
Copilot AI review requested due to automatic review settings September 6, 2026 07:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The PR title/description and linked issue state a microtask-based deferral change, but the implementation explicitly preserves timer/macrotask deferral, so the stated scope/outcome needs to be reconciled before merge.

Review details

Suppressed comments (5)

Previously missed (1) — in code that hasn't changed since the last review.

packages/melonjs/src/level/level.js:180

  • The overload doc summary reads as if level.load() always returns a promise, but the boolean overload is the default behavior. Reword the summary to make it explicit that the promise return only applies when options.async is true, so generated docs don’t contradict the non-async signature.

This issue also appears in the following locations of the same file:

  • line 323
  • line 354
  • line 395

packages/melonjs/src/level/level.js:354

  • This summary sentence implies next() always returns a promise, but it only does so when async: true is provided (otherwise it returns a boolean). Consider rewording so the async behavior is clearly conditional.
	 * load the next level, and return a promise that settles once the level is in the world.

packages/melonjs/src/level/level.js:395

  • This summary sentence implies previous() always returns a promise, but it only does so when async: true is provided (otherwise it returns a boolean). Rewording keeps the doc summary consistent with the overloads.
	 * load the previous level, and return a promise that settles once the level is in the world.

packages/melonjs/src/level/level.js:257

  • The PR title/description and linked issue describe replacing the existing timer deferral with a microtask (to avoid background-tab timer clamping), but the implementation explicitly keeps a timer-based macrotask via defer/setTimeout. Please reconcile this (update the PR/issue scope if the timer is intentional, or adjust the scheduling strategy if the microtask behavior is still the goal).
		// A TIMER, through the engine's own `defer`, and deliberately not a
		// microtask. A macrotask cannot run inside another, so the load lands
		// after the current frame whatever the frame does. A microtask only
		// lands there while the whole update-and-draw path stays synchronous —
		// true today, but it would start running mid-frame the day anything in
		// that path awaits, silently and with nothing to catch it.

packages/melonjs/src/level/level.js:323

  • This summary sentence implies reload() always returns a promise, but it only does so when async: true is provided (otherwise it returns a boolean). Making that explicit avoids misleading API docs.
	 * reload the current level, and return a promise that settles once the level is in the world.
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@obiot
obiot merged commit 6c3cba2 into master Sep 6, 2026
7 checks passed
@obiot
obiot deleted the feat/level-load-async branch September 6, 2026 07:53
@obiot

obiot commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Note for anyone arriving from the squash commit: its subject and body are stale. They were written against intermediate states that this PR later reverted, and the squash concatenated every commit message including the superseded ones.

What actually landed:

  • The deferral is a timer, through the engine's own utils.function.defer. The microtask version was reverted: a macrotask cannot run inside another, so the load lands after the current frame structurally, rather than only while the update-and-draw path happens to stay synchronous.
  • There is no loadAsync(). The switch is an async option on the existing load / reload / next / previous, preserved as JSDoc @overload pairs so const ok: boolean = level.load(id) still compiles.
  • queueMicrotask is not used anywhere.
  • The background-tab clamp is not fixed — that was the trade for going back to a timer, and the CHANGELOG entry claiming it was removed.

The code comments and the CHANGELOG in the merged tree are accurate; only the commit message is not. My miss for not retitling before the merge.

obiot added a commit that referenced this pull request Sep 6, 2026
The three 3D examples that go through the level director — forest, glTF
scene and glTF character — now use the `async` option from #1647 instead
of the `onLoaded` callback, so the setup that follows the load reads as
ordinary sequential code.

`loader.preload` deliberately keeps its callback form. Awaiting it would
delay returning the teardown function, and the forest example needs that
cleanup to exist while its 3 MB glb is still loading.

Also corrects a JSDoc default that shipped in the published types:
`LevelLoadOptions.castGroundShadow` was documented as defaulting to
`false`, but the option is tri-state — omitting it means "inherit the
application setting", which is on by default. The three sibling
declarations (Mesh, GLTFScene, GLTFModel) already said so. The emitted
type is unchanged; only the prose was wrong, in the direction that makes
someone set the flag to `true` believing it is off.


Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Level: add level.loadAsync(), and replace the 2011-era setTimeout deferral with a microtask

2 participants