CrossWorldSchedule

View source

Coordinates an ordered collection of executable callbacks that run with a target world and a source world.

Each callback is enabled when appended. Use CrossWorldSchedule#add to append callbacks and CrossWorldSchedule#run to invoke enabled callbacks in append order. Each callback receives the target world first and the source world second.

Examples

import { World } from '@wimaengine/ecs'
import { CrossWorldSchedule } from '@wimaengine/schedule'

const targetWorld = new World()
const sourceWorld = new World()
const schedule = new CrossWorldSchedule()

schedule.add((target, source) => {
  // `target` is targetWorld; `source` is sourceWorld.
  console.log('syncing the source world into the target world', source, target)
})

schedule.run(targetWorld, sourceWorld)

Constructor


constructor()

Methods


add(system: object): number

View source

Appends an executable callback to the schedule and enables it.

The returned number is the callback’s zero-based position. CrossWorldSchedule#run considers callbacks in append order. Adding a callback does not execute it immediately.

Parameters

NameDescriptionDefault
system

Executable callback called with the target world first and the source world second.

—

Returns

The zero-based id of the appended callback.

run(targetWorld: World, sourceWorld: World, errorHandler?: object): void

View source

Invokes each enabled executable callback in append order.

Each enabled callback receives targetWorld as its first argument and sourceWorld as its second argument. Disabled entries are not called.

If an enabled callback throws, errorHandler receives the normalized error and targetWorld; sourceWorld is not passed to the handler. Existing Error values are passed through; thrown strings become new Error(error), and other thrown values become new Error(String(error)). Without a handler, the default handler rethrows the normalized error. A custom handler that returns lets run continue with the next callback; if the handler throws, run exits with that handler error.

Parameters

NameDescriptionDefault
targetWorld

Target world forwarded as the first argument to each enabled callback and to the error handler.

—
sourceWorld

Source world forwarded as the second argument to each enabled callback.

—
errorHandler?

Optional handler for callback errors; it receives the normalized error and target world.

—

Throws

If the default error handler rethrows a normalized callback error or a custom error handler throws.