CrossWorldScheduleContext

View source

Collects cross-world callback registrations and ordering rules for one schedule label.

SchedulerBuilder uses this context to order callbacks before populating the runtime CrossWorldSchedule.

Examples

import { CrossWorldScheduleContext } from '@wimaengine/schedule'

class Synchronize {}
function copy(target, source) {}
function flush(target, source) {}

const context = new CrossWorldScheduleContext(Synchronize)
context.addSystem({ schedule: Synchronize, system: copy })
context.addSystem({ schedule: Synchronize, after: [copy], system: flush })

const orderedSystems = context.sortSystems()

Constructor


constructor(label: ConstructorFn)

View source

Creates build state for a cross-world schedule label.

Parameters

NameDescriptionDefault
label

Constructor identifying the target schedule.

—

Properties


systems: CrossWorldSystemRegistration[]

View source

Cross-world callbacks registered for this schedule, in registration order.

Each registration receives a zero-based ID equal to its position when it is added. The builder later uses the ordered registrations to populate the matching CrossWorldSchedule.

Default: []

Methods


addSystem(config: object): CrossWorldSystemRegistration

View source

Registers a cross-world callback in this context.

The returned registration receives the next zero-based ID. A truthy config.label, or the callback function’s name when it is absent or empty, becomes the ordering label. A non-empty label is registered for before and after references. Duplicate non-empty labels are rejected immediately. Ordering references are stored in config and resolved later by CrossWorldScheduleContext#sortSystems.

Examples

import { CrossWorldScheduleContext } from '@wimaengine/schedule'

class Synchronize {}
function copy(target, source) {}
function flush(target, source) {}

const context = new CrossWorldScheduleContext(Synchronize)
context.addSystem({ schedule: Synchronize, system: copy })
context.addSystem({ schedule: Synchronize, after: [copy], system: flush })

Parameters

NameDescriptionDefault
config

Callback, schedule, and ordering options.

—

Throws

If the resolved non-empty label is already registered.

Returns

The internal callback registration.

sortSystems(): CrossWorldSystemRegistration[]

View source

Resolves ordering constraints and returns callbacks in execution order.

The context expands each registered callback and its before/after references into a directed graph, then applies a topological sort. The returned array contains callback registrations only. Cross-world contexts do not expand callback groups.

Missing labels and self-referential edges are rejected while the graph is expanded. If the resolved graph contains a cycle, this method throws instead of returning a partial order.

Throws

If a label is missing, an edge is self-referential, or the ordering graph is cyclic.

Returns

Callback registrations ordered for the schedule.

expandScheduleGraph(): object

View source

Expands registered callbacks and ordering constraints into a directed graph.

One graph node is created for each callback, and systemsByGraphId maps graph nodes back to their registrations. Each before reference adds an edge from the current callback to the referenced callback; each after reference adds the reverse edge. Duplicate edges are ignored. This method builds the graph but does not sort it; use CrossWorldScheduleContext#sortSystems for the ordered registrations.

Throws

If an ordering label is missing or creates a self-reference.

Returns

The graph and its registration lookup.

addNodeOrdering(graph: Graph<CrossWorldSystemRegistration, undefined>, graphIdsBySystemId: Map<number, number>, edges: Set<string>, source: ScheduleNodeRef, before: CrossWorldSystemOrderReference[], after: CrossWorldSystemOrderReference[]): void

View source

Adds graph edges for one callback’s ordering constraints.

Each before reference creates an edge from source to the referenced callback. Each after reference creates an edge from the referenced callback to source. References are converted to labels and resolved through CrossWorldScheduleContext#resolveNode; duplicate edges are ignored by CrossWorldScheduleContext#addExpandedEdge.

Parameters

NameDescriptionDefault
graph

Ordering graph being built.

—
graphIdsBySystemId

Registration IDs to graph IDs.

—
edges

Directed edges already added to the graph.

—
source

Callback whose constraints are being applied.

—
before

Callbacks that must run after source.

—
after

Callbacks that must run before source.

—

Throws

If a reference is missing or creates a self-referential edge.

addExpandedEdge(graph: Graph<CrossWorldSystemRegistration, undefined>, graphIdsBySystemId: Map<number, number>, edges: Set<string>, from: ScheduleNodeRef, to: ScheduleNodeRef, targetLabel: CrossWorldSystemOrderReference): void

View source

Adds one directed graph edge between two registered callbacks.

The endpoint registrations are converted to graph node IDs. An edge that has already been added is ignored. An edge whose endpoints are the same callback is rejected because it creates a self-referential ordering.

Parameters

NameDescriptionDefault
graph

Ordering graph being built.

—
graphIdsBySystemId

Registration IDs to graph IDs.

—
edges

Directed edges already added to the graph.

—
from

Source callback node.

—
to

Destination callback node.

—
targetLabel

Original reference used for error reporting.

—

Throws

If the edge points from a callback to itself.

expandNodeToOrderingNode(node: ScheduleNodeRef, graphIdsBySystemId: Map<number, number>): number

View source

Resolves a registered callback node to its graph node ID.

Parameters

NameDescriptionDefault
node

Registered callback node reference.

—
graphIdsBySystemId

Registration IDs to graph IDs.

—

Throws

If the registration has no graph node in the current expansion.

Returns

The graph node ID.