Class
Command
View sourceDefines a deferred mutation that a concrete command can apply to a
World.
Extend this class and implement Command#execute for a specific
operation. Add command instances to a CommandQueue when the
application wants to apply those mutations later; the application controls
when queued commands run.
Undo is application-managed. Override Command#undo when an inverse
operation is needed; the base methods are no-ops.
Examples
import { Command, CommandQueue } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'
class Score {
value = 0
}
class AddScoreCommand extends Command {
constructor(amount) {
super()
this.amount = amount
}
execute(world) {
world.getResource(Score).value += this.amount
}
undo(world) {
world.getResource(Score).value -= this.amount
}
}
const world = new World()
world.setResource(new Score())
const queue = new CommandQueue()
world.setResource(queue)
const command = new AddScoreCommand(10)
queue.add(command)
for (const queuedCommand of queue.drain()) queuedCommand.execute(world)
command.undo(world) // Optional application-managed reversal.Constructor
Methods
execute(_world: World): void
View sourceApplies the command’s deferred mutation to a world.
Override this method in a concrete command. The base implementation does nothing.
undo(_world: World): void
View sourceReverses the command’s mutation when undo is supported by the application.
Override this method in a concrete command that participates in an application-managed undo flow. The base implementation does nothing.
