Function createSharedMachine

  • Create a shared state machine.

    Type Parameters

    • D extends {
          $schema?: Schema<D, ["$schema"], "$schema" extends keyof D
              ? D[keyof D & "$schema"] extends undefined
                  ? undefined
                  : D[keyof D & "$schema"]
              : undefined, "strict" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "strict"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "strict"]
              : undefined, "events" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "events"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "events"]
              : undefined, "context" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "context"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "context"]
              : undefined>;
          initial: IsPlainObject<"states" extends keyof D
                  ? D[keyof D & "states"] extends undefined
                      ? undefined
                      : D[keyof D & "states"]
                  : undefined> extends false
              ? "Error: States must be a plain object."
              : [keyof ("states" extends keyof D
                      ? D[keyof (...) & "states"] extends undefined
                          ? undefined
                          : D[keyof (...) & "states"]
                      : undefined)] extends [never]
                  ? "Error: No states defined."
                  : IsStringLiteral<keyof ("states" extends keyof D
                          ? D[(...) & (...)] extends undefined
                              ? undefined
                              : D[(...) & (...)]
                          : undefined)> extends false
                      ? "Error: States have no literal string value."
                      : keyof ("states" extends keyof D
                          ? D[keyof (...) & "states"] extends undefined
                              ? undefined
                              : D[keyof (...) & "states"]
                          : undefined);
          on?: On<D, ["on"], never>;
          states: IsPlainObject<"states" extends keyof D
                  ? D[keyof D & "states"] extends undefined
                      ? undefined
                      : D[keyof D & "states"]
                  : undefined> extends false
              ? "Error: States must be a plain object."
              : [keyof ("states" extends keyof D
                      ? D[keyof (...) & "states"] extends undefined
                          ? undefined
                          : D[keyof (...) & "states"]
                      : undefined)] extends [never]
                  ? {
                      [NEVER]?: undefined;
                  }
                  : IsStringLiteral<keyof ("states" extends keyof D
                          ? D[(...) & (...)] extends undefined
                              ? undefined
                              : D[(...) & (...)]
                          : undefined)> extends false
                      ? "Error: State values must be literal strings."
                      : {
                          readonly [V in string | number | symbol]: V extends ReservedKeyword
                              ? `Error: State value '${V<V>}' is reserved.`
                              : Definition.State<D, ["states", V], never, never>
                      };
      } & ({
          context: Context<D>;
      } | {
          context?: Context<D>;
      })

      The type of shared state machine definition.

    Parameters

    Returns SharedMachine<D>

    The shared state machine.

  • Create a shared state machine.

    This function is used in conjunction with useSharedMachine. It cannot be used with useMachine or useSyncedMachine. In those cases, use createMachine instead.

    Type Parameters

    • D extends {
          $schema?: Schema<D, ["$schema"], "$schema" extends keyof D
              ? D[keyof D & "$schema"] extends undefined
                  ? undefined
                  : D[keyof D & "$schema"]
              : undefined, "strict" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "strict"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "strict"]
              : undefined, "events" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "events"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "events"]
              : undefined, "context" extends keyof ("$schema" extends keyof D
                  ? D[keyof (...) & "$schema"] extends undefined
                      ? undefined
                      : D[keyof (...) & "$schema"]
                  : undefined)
              ? ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "context"] extends undefined
                  ? undefined
                  : ("$schema" extends keyof D
                      ? D[(...) & (...)] extends undefined
                          ? undefined
                          : D[(...) & (...)]
                      : undefined)[keyof ("$schema" extends keyof (...)
                      ? (...) extends (...)
                          ? (...)
                          : (...)
                      : undefined) & "context"]
              : undefined>;
          initial: IsPlainObject<"states" extends keyof D
                  ? D[keyof D & "states"] extends undefined
                      ? undefined
                      : D[keyof D & "states"]
                  : undefined> extends false
              ? "Error: States must be a plain object."
              : [keyof ("states" extends keyof D
                      ? D[keyof (...) & "states"] extends undefined
                          ? undefined
                          : D[keyof (...) & "states"]
                      : undefined)] extends [never]
                  ? "Error: No states defined."
                  : IsStringLiteral<keyof ("states" extends keyof D
                          ? D[(...) & (...)] extends undefined
                              ? undefined
                              : D[(...) & (...)]
                          : undefined)> extends false
                      ? "Error: States have no literal string value."
                      : keyof ("states" extends keyof D
                          ? D[keyof (...) & "states"] extends undefined
                              ? undefined
                              : D[keyof (...) & "states"]
                          : undefined);
          on?: On<D, ["on"], G>;
          states: IsPlainObject<"states" extends keyof D
                  ? D[keyof D & "states"] extends undefined
                      ? undefined
                      : D[keyof D & "states"]
                  : undefined> extends false
              ? "Error: States must be a plain object."
              : [keyof ("states" extends keyof D
                      ? D[keyof (...) & "states"] extends undefined
                          ? undefined
                          : D[keyof (...) & "states"]
                      : undefined)] extends [never]
                  ? {
                      [NEVER]?: undefined;
                  }
                  : IsStringLiteral<keyof ("states" extends keyof D
                          ? D[(...) & (...)] extends undefined
                              ? undefined
                              : D[(...) & (...)]
                          : undefined)> extends false
                      ? "Error: State values must be literal strings."
                      : {
                          readonly [V in string | number | symbol]: V extends ReservedKeyword
                              ? `Error: State value '${V<V>}' is reserved.`
                              : Definition.State<D, ["states", V], G, E>
                      };
      } & ({
          context: Context<D>;
      } | {
          context?: Context<D>;
      })

      The type of shared state machine definition.

    • const G extends string = never

      The type of guards for shared state machine functions.

    • const E extends string = never

      The type of effects for shared state machine functions.

    Parameters

    Returns SharedMachine<D>

    The shared state machine.