Command

View source

Defines 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


constructor()

Methods


execute(_world: World): void

View source

Applies the command’s deferred mutation to a world.

Override this method in a concrete command. The base implementation does nothing.

Parameters

NameDescriptionDefault
_world

World to mutate.

—

undo(_world: World): void

View source

Reverses 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.

Parameters

NameDescriptionDefault
_world

World to mutate.

—