(
rootActor: AnyActorRef,
options: {
clock: Clock;
logger: (...args: any[]) => void;
snapshot?: unknown;
}
)
| 89 | |
| 90 | let idCounter = 0; |
| 91 | export 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[ |
no test coverage detected
searching dependent graphs…