CommandQueue

View source

World-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


constructor()

Methods


add(command: Command): void

View source

Appends a command to the end of the FIFO queue.

Commands remain in this order when the queue is drained.

Parameters

NameDescriptionDefault
command——

drain(): Command[]

View source

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

Returns

The commands in FIFO order.

size(): number

View source

Returns the number of commands currently waiting to be drained.