* Creates a new actor instance for the given logic with the provided options, * if any. * * @param logic The logic to create an actor from * @param options Actor options
(
public logic: TLogic,
options?: ActorOptions<TLogic>
)
| 140 | * @param options Actor options |
| 141 | */ |
| 142 | constructor( |
| 143 | public logic: TLogic, |
| 144 | options?: ActorOptions<TLogic> |
| 145 | ) { |
| 146 | const resolvedOptions = { |
| 147 | ...defaultOptions, |
| 148 | ...options |
| 149 | }; |
| 150 | |
| 151 | const { clock, logger, parent, syncSnapshot, id, systemId, inspect } = |
| 152 | resolvedOptions; |
| 153 | |
| 154 | this.system = parent |
| 155 | ? parent.system |
| 156 | : createSystem(this, { |
| 157 | clock, |
| 158 | logger |
| 159 | }); |
| 160 | |
| 161 | if (inspect && !parent) { |
| 162 | // Always inspect at the system-level |
| 163 | this.system.inspect(toObserver(inspect)); |
| 164 | } |
| 165 | |
| 166 | this.sessionId = this.system._bookId(); |
| 167 | this.id = id ?? this.sessionId; |
| 168 | this.logger = options?.logger ?? this.system._logger; |
| 169 | this.clock = options?.clock ?? this.system._clock; |
| 170 | this._parent = parent; |
| 171 | this._syncSnapshot = syncSnapshot; |
| 172 | this.options = resolvedOptions as ActorOptions<TLogic> & |
| 173 | typeof defaultOptions; |
| 174 | this.src = resolvedOptions.src ?? logic; |
| 175 | this.ref = this; |
| 176 | this._actorScope = { |
| 177 | self: this, |
| 178 | id: this.id, |
| 179 | sessionId: this.sessionId, |
| 180 | logger: this.logger, |
| 181 | defer: (fn) => { |
| 182 | this._deferred.push(fn); |
| 183 | }, |
| 184 | system: this.system, |
| 185 | stopChild: (child) => { |
| 186 | if (child._parent !== this) { |
| 187 | throw new Error( |
| 188 | `Cannot stop child actor ${child.id} of ${this.id} because it is not a child` |
| 189 | ); |
| 190 | } |
| 191 | (child as any)._stop(); |
| 192 | }, |
| 193 | emit: (emittedEvent) => { |
| 194 | const listeners = this.eventListeners.get(emittedEvent.type); |
| 195 | const wildcardListener = this.eventListeners.get('*'); |
| 196 | if (!listeners && !wildcardListener) { |
| 197 | return; |
| 198 | } |
| 199 | const allListeners = [ |
nothing calls this directly
no test coverage detected