Skip to content

3D engine that AI agents drive

Reject a non-function runtime clock with a clear error (#2619)

Veeramanikandanr487f5ceca7053c
diff --git a/changes/2619.md b/changes/2619.md
new file mode 100644
index 00000000..8ce0be26
--- /dev/null
+++ b/changes/2619.md
@@ -0,0 +1 @@
+- `createRuntime` rejects a non-function `clock` with a clear error when the runtime is created (#2619)
diff --git a/docs/runtime.md b/docs/runtime.md
index b8bd5e95..ffeb34ed 100644
--- a/docs/runtime.md
+++ b/docs/runtime.md
@@ -49,6 +49,8 @@ Two guards stop a slow frame from spiralling. A frame longer than `maximumFrameS
| `idleSleep` | `true` | `false` keeps drawing every frame even when `isIdle` says idle |
| `scheduler`, `clock`, `visibility`, `observe` | browser | `{ request(callback), cancel(handle) }`, `() => milliseconds`, `{ hidden, subscribe(callback) }` and `(element, callback(onscreen)) => stop`, injected by tests and headless runs |
+`clock` must be a function returning milliseconds; leave it out to use `performance.now()`. A non-function is rejected when the runtime is created, rather than failing on the first frame.
+
The first argument is any object with `render(frame)`, such as `CrucibleRenderer`.
## Runtime methods
diff --git a/src/runtime.js b/src/runtime.js
index 44dbe1c7..63598bce 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -74,6 +74,9 @@ function validateOptions(target, options) {
if (options.frame !== undefined && typeof options.frame !== 'function') {
throw new TypeError('createRuntime frame must be a function (context) => frame for target.render, or left out.');
}
+ if (options.clock !== undefined && typeof options.clock !== 'function') {
+ throw new TypeError(`createRuntime clock must be a function () => milliseconds or left out, got ${describe(options.clock)}.`);
+ }
for (const name of ['pauseWhenHidden', 'pauseWhenOffscreen', 'idleSleep']) {
if (typeof (options[name] ?? true) !== 'boolean') throw new TypeError(`createRuntime ${name} must be a boolean.`);
}
diff --git a/tests/runtime.test.js b/tests/runtime.test.js
index fd735203..08d4fd4e 100644
--- a/tests/runtime.test.js
+++ b/tests/runtime.test.js
@@ -245,6 +245,9 @@ test('input is validated with messages that say what to pass', () => {
assert.throws(() => createRuntime(target, { scheduler, systems: [{ update() {} }] }), /unique name/);
assert.throws(() => createRuntime(target, { scheduler, systems: [{ name: 'x', update: 3 }] }), /update must be a function/);
assert.throws(() => createRuntime(target, { scheduler, frame: 3 }), /frame must be a function/);
+ assert.throws(() => createRuntime(target, { scheduler, clock: 'x' }), /clock must be a function/);
+ assert.doesNotThrow(() => createRuntime(target, { scheduler, clock: () => 0 }));
+ assert.doesNotThrow(() => createRuntime(target, { scheduler }));
assert.throws(() => createRuntime(target), /requestAnimationFrame/);
});