From 39505f0d785e067a6595fdadd643de79a2fa9a18 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Wed, 26 Aug 2026 07:34:02 +0300 Subject: [PATCH 1/3] Add optional resource getters --- packages/ecs/src/world.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/ecs/src/world.js b/packages/ecs/src/world.js index 58e23f23..99bf1fce 100644 --- a/packages/ecs/src/world.js +++ b/packages/ecs/src/world.js @@ -366,6 +366,15 @@ export class World { return this.getResourceByTypeId(typeid(resourceType)) } + /** + * @template T + * @param {Constructor} resourceType + * @returns {T | undefined} + */ + getOptionalResource(resourceType) { + return this.getOptionalResourceByTypeId(typeid(resourceType)) + } + /** * @template T * @param {TypeId} id From 7c4b6d3e5ae409390f41da928c298311835b9c77 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Wed, 26 Aug 2026 07:35:02 +0300 Subject: [PATCH 2/3] Refactor resource getters --- packages/ecs/src/world.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/ecs/src/world.js b/packages/ecs/src/world.js index 99bf1fce..0a39d840 100644 --- a/packages/ecs/src/world.js +++ b/packages/ecs/src/world.js @@ -381,9 +381,23 @@ export class World { * @returns {T} */ getResourceByTypeId(id) { + const resource = this.getOptionalResourceByTypeId(id) + + assert(resource, `The resource or resource alias \`${id}\` is non existent.`) + + // SAFETY: The typeid should match the type, caller's responsibility + return /** @type {T}*/(resource) + } + + /** + * @template T + * @param {TypeId} id + * @returns {T | undefined} + */ + getOptionalResourceByTypeId(id) { const resource = this.resources.get(id) - if (resource) { + if (resource !== undefined) { // SAFETY: The typeid should match the type, caller's responsibility return /** @type {T}*/(resource) @@ -391,11 +405,11 @@ export class World { const aliasedid = this.resourceAliases.get(id) - assert(aliasedid, `The resource or resource alias \`${id}\` is non existent.`) + if (aliasedid === undefined) return undefined const aliasedResource = this.resources.get(aliasedid) - assert(aliasedResource, `The resource alias \`${id}\` points to a non-existent resource \`${aliasedid}\`.`) + if (aliasedResource === undefined) return undefined // SAFETY: The aliased typeid should match the type, caller's responsibility return /** @type {T}*/(aliasedResource) From fcf1a86b94c1b9c0b9f967a8d8a703417e185940 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Wed, 26 Aug 2026 07:35:19 +0300 Subject: [PATCH 3/3] Add tests for optional resource getters --- packages/ecs/tests/world.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/ecs/tests/world.test.js b/packages/ecs/tests/world.test.js index 8777b8d8..1da09efc 100644 --- a/packages/ecs/tests/world.test.js +++ b/packages/ecs/tests/world.test.js @@ -1,5 +1,5 @@ import { test, describe } from "vitest"; -import { deepStrictEqual, throws } from "node:assert"; +import { deepStrictEqual, strictEqual, throws } from "node:assert"; import { World } from "../src"; import { typeid } from "@wimaengine/type"; import { EntityHandle } from "../src/entities"; @@ -109,6 +109,30 @@ describe("Testing `World`", () => { deepStrictEqual(resource, new TestResource()) }) + test('Get optional correct resource on world.', () => { + const world = new World() + world.setResource(new TestResource()) + const resource = world.getOptionalResource(TestResource) + + deepStrictEqual(resource, new TestResource()) + }) + + test('Optional resource lookup on world returns aliased resources.', () => { + const world = new World() + world.setResource(new TestResource()) + world.setResourceAlias(typeid(TestResource), TestAlias) + const resource = world.getOptionalResource(TestAlias) + + deepStrictEqual(resource, new TestResource()) + }) + + test('Optional resource lookup on world returns undefined when missing.', () => { + const world = new World() + + strictEqual(world.getOptionalResource(TestResource), undefined) + strictEqual(world.getOptionalResource(TestAlias), undefined) + }) + test('World has resource.', () => { const world = new World() world.setResource(new TestResource())