MCPcopy
hub / github.com/statelyai/xstate / createSystem

Function createSystem

packages/core/src/system.ts:91–247  ·  view source on GitHub ↗
(
  rootActor: AnyActorRef,
  options: {
    clock: Clock;
    logger: (...args: any[]) => void;
    snapshot?: unknown;
  }
)

Source from the content-addressed store, hash-verified

89
90let idCounter = 0;
91export function createSystem<T extends ActorSystemInfo>(
92 rootActor: AnyActorRef,
93 options: {
94 clock: Clock;
95 logger: (...args: any[]) => void;
96 snapshot?: unknown;
97 }
98): ActorSystem<T> {
99 const children = new Map<string, AnyActorRef>();
100 const keyedActors = new Map<keyof T['actors'], AnyActorRef | undefined>();
101 const reverseKeyedActors = new WeakMap<AnyActorRef, keyof T['actors']>();
102 const inspectionObservers = new Set<Observer<InspectionEvent>>();
103 const timerMap: { [id: ScheduledEventId]: number } = {};
104 const { clock, logger } = options;
105
106 const scheduler: Scheduler = {
107 schedule: (
108 source,
109 target,
110 event,
111 delay,
112 id = Math.random().toString(36).slice(2)
113 ) => {
114 const scheduledEvent: ScheduledEvent = {
115 source,
116 target,
117 event,
118 delay,
119 id,
120 startedAt: Date.now()
121 };
122 const scheduledEventId = createScheduledEventId(source, id);
123 system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
124
125 const timeout = clock.setTimeout(() => {
126 delete timerMap[scheduledEventId];
127 delete system._snapshot._scheduledEvents[scheduledEventId];
128
129 system._relay(source, target, event);
130 }, delay);
131
132 timerMap[scheduledEventId] = timeout;
133 },
134 cancel: (source, id: string) => {
135 const scheduledEventId = createScheduledEventId(source, id);
136 const timeout = timerMap[scheduledEventId];
137
138 delete timerMap[scheduledEventId];
139 delete system._snapshot._scheduledEvents[scheduledEventId];
140
141 if (timeout !== undefined) {
142 clock.clearTimeout(timeout);
143 }
144 },
145 cancelAll: (actorRef) => {
146 for (const scheduledEventId in system._snapshot._scheduledEvents) {
147 const scheduledEvent =
148 system._snapshot._scheduledEvents[

Callers 2

constructorMethod · 0.90
createInertActorScopeFunction · 0.90

Calls 10

toObserverFunction · 0.90
createScheduledEventIdFunction · 0.85
nowMethod · 0.80
cancelMethod · 0.80
getMethod · 0.80
_sendMethod · 0.80
scheduleMethod · 0.80
setTimeoutMethod · 0.65
clearTimeoutMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…