From 65c780a48ec0da9af32fb28b204a163b4f27042e Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Mon, 17 Aug 2026 23:07:42 +0300 Subject: [PATCH 1/3] Add atomic resource command --- packages/core/src/commands/addresource.js | 26 ++++++++++++++ packages/core/src/commands/index.js | 5 ++- packages/core/src/commands/removeresource.js | 27 ++++++++++++++ .../core/src/commands/setresourcealias.js | 35 +++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/commands/addresource.js create mode 100644 packages/core/src/commands/removeresource.js create mode 100644 packages/core/src/commands/setresourcealias.js diff --git a/packages/core/src/commands/addresource.js b/packages/core/src/commands/addresource.js new file mode 100644 index 00000000..2665f056 --- /dev/null +++ b/packages/core/src/commands/addresource.js @@ -0,0 +1,26 @@ +import { Command } from '@wimaengine/command' +import { World } from '@wimaengine/ecs' + +export class AddResourceCommand extends Command { + + /** + * @readonly + * @type {object} + */ + resource + + /** + * @param {object} resource + */ + constructor(resource) { + super() + this.resource = resource + } + + /** + * @param {World} world + */ + execute(world) { + world.setResource(this.resource) + } +} diff --git a/packages/core/src/commands/index.js b/packages/core/src/commands/index.js index 4821bf12..192d1f85 100644 --- a/packages/core/src/commands/index.js +++ b/packages/core/src/commands/index.js @@ -1,2 +1,5 @@ -export * from './spawn' +export * from './addresource' export * from './despawn' +export * from './removeresource' +export * from './setresourcealias' +export * from './spawn' diff --git a/packages/core/src/commands/removeresource.js b/packages/core/src/commands/removeresource.js new file mode 100644 index 00000000..13ba6732 --- /dev/null +++ b/packages/core/src/commands/removeresource.js @@ -0,0 +1,27 @@ +/** @import {Constructor} from '@wimaengine/type' */ +import { Command } from '@wimaengine/command' +import { World } from '@wimaengine/ecs' + +export class RemoveResourceCommand extends Command { + + /** + * @readonly + * @type {Constructor} + */ + resourceType + + /** + * @param {Constructor} resourceType + */ + constructor(resourceType) { + super() + this.resourceType = resourceType + } + + /** + * @param {World} world + */ + execute(world) { + world.removeResource(this.resourceType) + } +} diff --git a/packages/core/src/commands/setresourcealias.js b/packages/core/src/commands/setresourcealias.js new file mode 100644 index 00000000..2b31c0bd --- /dev/null +++ b/packages/core/src/commands/setresourcealias.js @@ -0,0 +1,35 @@ +/** @import {Constructor, TypeId} from '@wimaengine/type' */ +import { Command } from '@wimaengine/command' +import { World } from '@wimaengine/ecs' + +export class SetResourceAliasCommand extends Command { + + /** + * @readonly + * @type {TypeId} + */ + id + + /** + * @readonly + * @type {Constructor} + */ + alias + + /** + * @param {TypeId} id + * @param {Constructor} alias + */ + constructor(id, alias) { + super() + this.id = id + this.alias = alias + } + + /** + * @param {World} world + */ + execute(world) { + world.setResourceAlias(this.id, this.alias) + } +} From 5132a90cdb6fb55433e6b8f007f16da59e73ad05 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Mon, 17 Aug 2026 23:07:58 +0300 Subject: [PATCH 2/3] Add resource commands --- packages/core/src/core/index.js | 1 + packages/core/src/core/resourcecommands.js | 51 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 packages/core/src/core/resourcecommands.js diff --git a/packages/core/src/core/index.js b/packages/core/src/core/index.js index b8f31ea8..b4768a1a 100644 --- a/packages/core/src/core/index.js +++ b/packages/core/src/core/index.js @@ -1,6 +1,7 @@ export * from './schedules' export * from './runner' export * from './entity' +export * from './resourcecommands' export * from './systemgroups' export * from './snapshot' export * from './worlds' diff --git a/packages/core/src/core/resourcecommands.js b/packages/core/src/core/resourcecommands.js new file mode 100644 index 00000000..0ffe65d9 --- /dev/null +++ b/packages/core/src/core/resourcecommands.js @@ -0,0 +1,51 @@ +/** @import {Constructor, TypeId} from '@wimaengine/type' */ +import { CommandQueue } from '@wimaengine/command' +import { World } from '@wimaengine/ecs' +import { AddResourceCommand, RemoveResourceCommand, SetResourceAliasCommand } from '../commands' + +export class ResourceCommands { + + /** + * @private + * @type {CommandQueue} + */ + buffer + + /** + * @param {World} world + */ + constructor(world) { + this.buffer = world.getResource(CommandQueue) + } + + /** + * @param {object} resource + * @returns {this} + */ + add(resource) { + this.buffer.add(new AddResourceCommand(resource)) + + return this + } + + /** + * @param {Constructor} resourceType + * @returns {this} + */ + remove(resourceType) { + this.buffer.add(new RemoveResourceCommand(resourceType)) + + return this + } + + /** + * @param {TypeId} id + * @param {Constructor} alias + * @returns {this} + */ + setAlias(id, alias) { + this.buffer.add(new SetResourceAliasCommand(id, alias)) + + return this + } +} From 43ca54307be06ea78189104b8612679639fee31e Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Mon, 17 Aug 2026 23:08:09 +0300 Subject: [PATCH 3/3] Add resource commands tests --- packages/core/tests/resourcecommands.test.js | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 packages/core/tests/resourcecommands.test.js diff --git a/packages/core/tests/resourcecommands.test.js b/packages/core/tests/resourcecommands.test.js new file mode 100644 index 00000000..3ad06fb9 --- /dev/null +++ b/packages/core/tests/resourcecommands.test.js @@ -0,0 +1,53 @@ +import { strictEqual } from 'node:assert' +import { describe, test } from 'vitest' +import { CommandQueue } from '@wimaengine/command' +import { World } from '@wimaengine/ecs' +import { typeid } from '@wimaengine/type' +import { ResourceCommands } from '../src/index.js' +import { executeCommands } from '../src/systems/index.js' + +class TestResource { } +class TestAlias extends TestResource { } + +describe('Testing `ResourceCommands`', () => { + test('`add()` adds a resource when commands execute', () => { + const world = new World() + const resource = new TestResource() + + world.setResource(new CommandQueue()) + const commands = new ResourceCommands(world) + + commands.add(resource) + executeCommands(world) + + strictEqual(world.getResource(TestResource), resource) + }) + + test('`remove()` removes a resource when commands execute', () => { + const world = new World() + + world.setResource(new CommandQueue()) + world.setResource(new TestResource()) + const commands = new ResourceCommands(world) + + commands.remove(TestResource) + executeCommands(world) + + strictEqual(world.hasResource(TestResource), false) + }) + + test('`setAlias()` resolves a resource through an alias when commands execute', () => { + const world = new World() + const resource = new TestResource() + const id = typeid(TestResource) + + world.setResource(resource) + world.setResource(new CommandQueue()) + const commands = new ResourceCommands(world) + + commands.setAlias(id, TestAlias) + executeCommands(world) + + strictEqual(world.getResource(TestAlias), resource) + }) +})