ScheduleContext

View source

Collects callback registrations, callback groups, and ordering rules for one normal schedule label.

SchedulerBuilder uses this context to build the matching Schedule. Add callbacks and groups, resolve group relationships, assign callbacks, then sort the callbacks before running the schedule.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
class MainPhase {}
function input() {}
function render() {}

const context = new ScheduleContext(Update)
context.addGroup({ label: MainPhase, schedule: Update })
context.addSystem({ schedule: Update, systemGroup: MainPhase, system: input })
context.addSystem({ schedule: Update, after: ['input'], system: render })
context.resolveGroupParents()
context.assignSystemsToGroups()

const orderedSystems = context.sortSystems()

Constructor


constructor(label: ConstructorFn)

View source

Creates build state for a schedule label.

Parameters

NameDescriptionDefault
label

Constructor identifying the target schedule.

—

Properties


Methods


addGroup(config: object): SystemGroupRegistration

View source

Registers a callback group in this context.

The group receives the next zero-based ID and starts with no callbacks. Its parent and ordering references remain in config; parent IDs are filled by ScheduleContext#resolveGroupParents and callbacks are assigned by ScheduleContext#assignSystemsToGroups. A non-empty config.label.name is also registered as an ordering label. An empty name still registers the group but cannot be used in a name-based ordering reference.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
class MainPhase {}
function input() {}

const context = new ScheduleContext(Update)
const group = context.addGroup({ label: MainPhase, schedule: Update })
context.addSystem({ schedule: Update, systemGroup: MainPhase, system: input })

// group.id is 0; the callback is assigned after the context resolves groups.
context.resolveGroupParents()
context.assignSystemsToGroups()

Parameters

NameDescriptionDefault
config

Callback group label, schedule, and ordering options.

—

Throws

If the group label is already registered, or its non-empty name conflicts with an existing callback or group ordering label.

Returns

The internal group registration.

addSystem(config: object): SystemRegistration

View source

Registers a callback in this context.

The callback receives the next zero-based ID. A truthy config.label is used as its ordering label; otherwise the callback function’s name is used. A non-empty label is added to the ordering nodes, so before and after references can resolve it later. Ordering references remain in config until ScheduleContext#sortSystems runs. An empty label still registers the callback but cannot be resolved by name.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
function input() {}
function render() {}

const context = new ScheduleContext(Update)
context.addSystem({ schedule: Update, system: input })
context.addSystem({ schedule: Update, after: [input], system: render })

const orderedSystems = context.sortSystems()

Parameters

NameDescriptionDefault
config

Callback, schedule, group, and ordering options.

—

Throws

If the resolved non-empty label conflicts with an existing callback or group ordering label.

Returns

The internal callback registration.

setDefaultSystemGroup(defaultSystemGroup: ConstructorFn): void

View source

Sets the fallback group for callbacks without an explicit systemGroup.

This stores only the group label. It does not assign callbacks or check that the group is registered. Lookup and validation are deferred until ScheduleContext#assignSystemsToGroups runs. An explicit systemGroup takes precedence there; undefined leaves callbacks without a group unassigned.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
class MainPhase {}
function tick() {}

const context = new ScheduleContext(Update)
context.setDefaultSystemGroup(MainPhase)
context.addGroup({ label: MainPhase, schedule: Update })
context.addSystem({ schedule: Update, system: tick })
context.resolveGroupParents()
context.assignSystemsToGroups()

Parameters

NameDescriptionDefault
defaultSystemGroup

Group label used as the fallback for callbacks without an explicit group.

—

resolveGroupParents(): void

View source

Resolves callback-group parents and validates the hierarchy.

Each configured parent constructor must already be registered in this context. The matching zero-based group ID is stored as parentId, and cyclic nesting is rejected before group ordering is expanded.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
class MainPhase {}
class RenderPhase {}

const context = new ScheduleContext(Update)
context.addGroup({ label: MainPhase, schedule: Update })
context.addGroup({ label: RenderPhase, parent: MainPhase, schedule: Update })
context.resolveGroupParents()

Throws

If a parent group is missing or group nesting is cyclic.

assignSystemsToGroups(): void

View source

Assigns registered callbacks to explicit or default groups.

For each callback, systemGroup takes precedence over the fallback set with ScheduleContext#setDefaultSystemGroup. Callbacks with neither remain outside all groups. The selected group must already be registered, and its ID is appended to that group’s callback list. This method does not order callbacks.

Examples

import { ScheduleContext } from '@wimaengine/schedule'

class Update {}
class DefaultPhase {}
class ExplicitPhase {}
function update() {}
function render() {}

const context = new ScheduleContext(Update)
context.addGroup({ label: DefaultPhase, schedule: Update })
context.addGroup({ label: ExplicitPhase, schedule: Update })
context.setDefaultSystemGroup(DefaultPhase)
context.addSystem({ schedule: Update, system: update })
context.addSystem({ schedule: Update, systemGroup: ExplicitPhase, system: render })
context.assignSystemsToGroups()

Throws

If an explicit or default callback group is not registered.

sortSystems(): SystemRegistration[]

View source

Resolves ordering constraints and returns callbacks in execution order.

The context expands nested group membership into a directed graph, applies each callback and group before/after constraint, and topologically sorts the graph. Empty groups remain ordering nodes; groups with callbacks expand their constraints to those callbacks. The returned array contains callback registrations only; group nodes are not returned. The normal build sequence calls ScheduleContext#resolveGroupParents and ScheduleContext#assignSystemsToGroups before this method.

Throws

If a reference is unknown, creates a self-ordering edge, or produces cyclic callback ordering constraints.

Returns

Callback registrations ordered for the target schedule.

assertNoGroupCycles(): void

View source

Validates that group nesting is acyclic.

visitGroupHierarchy(groupId: number, state: number[]): void

View source

Parameters

NameDescriptionDefault
groupId——
state——