Class
CommandQueue
View sourceWorld-scoped resource that stores deferred Command.
Commands are stored in FIFO order until a consumer drains the queue.
Draining returns the current batch and starts an empty queue, so commands
added afterward belong to the next batch. CommandsPlugin provides
this resource for an app’s default world; applications can also create and
install one directly.
Examples
import { Command, CommandQueue } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'
class PrintCommand extends Command {
execute() {
console.log('executed')
}
}
const world = new World()
const queue = new CommandQueue()
world.setResource(queue)
const first = new PrintCommand()
const second = new PrintCommand()
queue.add(first)
queue.add(second)
console.log(queue.size()) // 2
for (const command of queue.drain()) command.execute(world)
console.log(queue.size()) // 0: draining starts an empty batch.Constructor
Methods
add(command: Command): void
View sourceAppends a command to the end of the FIFO queue.
Commands remain in this order when the queue is drained.
drain(): Command[]
View sourceReturns the queued commands and clears the queue immediately.
The returned array contains the commands queued at the time of the call.
It is detached from the queue: commands added after drain returns are
stored in a new queue and are not included in that array.
size(): number
View sourceReturns the number of commands currently waiting to be drained.
