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

Method start

packages/core/src/createActor.ts:513–596  ·  view source on GitHub ↗

Starts the Actor from the initial state

()

Source from the content-addressed store, hash-verified

511
512 /** Starts the Actor from the initial state */
513 public start(): this {
514 if (this._processingStatus === ProcessingStatus.Running) {
515 // Do not restart the service if it is already started
516 return this;
517 }
518
519 if (this._syncSnapshot) {
520 this.subscribe({
521 next: (snapshot: Snapshot<unknown>) => {
522 if (snapshot.status === 'active') {
523 this.system._relay(this, this._parent!, {
524 type: `xstate.snapshot.${this.id}`,
525 snapshot
526 });
527 }
528 },
529 error: () => {}
530 });
531 }
532
533 this.system._register(this.sessionId, this);
534 if (this.systemId) {
535 this.system._set(this.systemId, this);
536 }
537 this._processingStatus = ProcessingStatus.Running;
538
539 // TODO: this isn't correct when rehydrating
540 const initEvent = createInitEvent(this.options.input);
541
542 this.system._sendInspectionEvent({
543 type: '@xstate.event',
544 sourceRef: this._parent,
545 actorRef: this,
546 event: initEvent
547 });
548
549 const status = (this._snapshot as any).status;
550
551 switch (status) {
552 case 'done':
553 // a state machine can be "done" upon initialization (it could reach a final state using initial microsteps)
554 // we still need to complete observers, flush deferreds etc
555 this.update(
556 this._snapshot,
557 initEvent as unknown as EventFromLogic<TLogic>
558 );
559 // TODO: rethink cleanup of observers, mailbox, etc
560 return this;
561 case 'error':
562 this._error((this._snapshot as any).error);
563 return this;
564 }
565
566 if (!this._parent) {
567 this.system.start();
568 }
569
570 if (this.logic.start) {

Callers

nothing calls this directly

Calls 6

subscribeMethod · 0.95
updateMethod · 0.95
_errorMethod · 0.95
attachDevToolsMethod · 0.95
createInitEventFunction · 0.90
startMethod · 0.65

Tested by

no test coverage detected