与 Ember 一起使用

The most straightforward way of using XState with Ember.js is through the ember-statecharts-addon. You can also write a custom integration layer yourself if you want to.

The machine used should always be decoupled from implementation details; e.g., it should never know that it is in Ember.js (or React, or Vue, etc.):

  1. import { createMachine } from 'xstate';
  2. // This machine is completely decoupled from Ember
  3. export const toggleMachine = createMachine({
  4. id: 'toggle',
  5. context: {
  6. /* some data */
  7. },
  8. initial: 'inactive',
  9. states: {
  10. inactive: {
  11. on: { TOGGLE: 'active' }
  12. },
  13. active: {
  14. on: { TOGGLE: 'inactive' }
  15. }
  16. }
  17. });

ember-statecharts

Using ember-statecharts makes it easy to use XState in your Ember.js codebase.

The addon provides the useMachine-API that you can use to interpret and use XState machines:

  1. import Component from '@glimmmer/component';
  2. import { action } from '@ember/object';
  3. import { useMachine, matchesState } from 'ember-statecharts';
  4. // @use (https://github.com/emberjs/rfcs/pull/567) is still WIP - polyfill it
  5. import { use } from 'ember-usable';
  6. import toggleMachine from './path/to/toggleMachine';
  7. export default class ToggleComponent extends Component {
  8. @use statechart = useMachine(toggleMachine);
  9. @matchesState('active')
  10. isActive;
  11. @matchesState('inactive')
  12. isInactive;
  13. @action
  14. toggle() {
  15. this.statechart.send('TOGGLE');
  16. }
  17. }

ember-statechart-component

Using ember-statechart-component, you can define an XState statechart in place of a component class. This also offers a deeper integration with Ember, in that actions and guards have a way to get access to the outer ember context via getService.

ember-statechart-component provides the most isolation from the framework, while still encouraging managing most of your state within the statechart’s context itself, rather than accidentally leaking state between your component and your statechart, and trying to keep things in-sync.

For example, an <AuthenticatedToggle /> component may look like:

  1. // app/components/authenticated-toggle.js
  2. import { getService } from 'ember-statechart-component';
  3. import { createMachine } from 'xstate';
  4. export default createMachine({
  5. initial: 'inactive',
  6. states: {
  7. inactive: {
  8. on: {
  9. TOGGLE: [
  10. {
  11. target: 'active',
  12. cond: 'isAuthenticated',
  13. },
  14. { actions: ['notify'] },
  15. ],
  16. },
  17. },
  18. active: { on: { TOGGLE: 'inactive' } },
  19. },
  20. }, {
  21. actions: {
  22. notify: (ctx) => {
  23. getService(ctx, 'toasts').notify('You must be logged in');
  24. },
  25. },
  26. guards: {
  27. isAuthenticated: (ctx) => getService(ctx, 'session').isAuthenticated,
  28. },
  29. });

and used:

  1. <AuthenticatedToggle as |state send|>
  2. {{state.value}}
  3. <button {{on 'click' (fn send 'TOGGLE')}}>
  4. Toggle
  5. </button>
  6. </AuthenticatedToggle>

Additionally, with Ember 3.25+, you can import statecharts from other locations and invoke them directly:

  1. import Component from '@glimmer/component';
  2. import { createMachine } from 'xstate';
  3. import SomeMachine from '...somewhere...';
  4. export default class extends Component {
  5. MyLocalMachine = SomeMachine;
  6. CustomMachine = createMachine(...);
  7. }
  1. <this.MyLocalMachine as |state send|>
  2. </this.MyLocalMachine>
  3. <this.CustomMachine as |state send|>
  4. </this.CustomMachine>

It is recommended to also have ember-could-get-used-to-this installed so that you can use state.matches and other xstate-provided APIs from the template (at least until the Default Helper Manager RFC lands and is implemented.)

Custom integration

To integrate XState into your Ember.js codebase without using an addon you can follow a similar pattern to Vue:

  • The machine can be defined externally;
  • The service is placed as a property of the component;
  • State changes are observed via interpreter.onTransition(state => ...), where you set some data property to the next state;
  • The machine’s context can be referenced as an external data store by the app. Context changes are also observed via interpreter.onTransition(state => ...), where you set another data property to the updated context;
  • The interpreter is started (interpreter.start()) when the component is created constructor();
  • Events are sent to the interpreter via interpreter.send(event).

::: tip This example is based on Ember Octane features (Ember 3.13+) :::

  1. <button type="button" {{on "click" (fn this.transition "TOGGLE")}}>
  2. {{if this.isInactive "Off" "On"}}
  3. </button>
  1. import Component from '@glimmer/component';
  2. import { tracked } from '@glimmer/tracking';
  3. import { action } from '@ember/object';
  4. import { interpret } from 'xstate';
  5. import { toggleMachine } from '../path/to/toggleMachine';
  6. export default class ToggleButton extends Component {
  7. @tracked current;
  8. get context() {
  9. return this.current.context;
  10. }
  11. get isInactive() {
  12. return this.current.matches('inactive');
  13. }
  14. constructor() {
  15. super(...arguments);
  16. this.toggleInterpreter = interpret(toggleMachine);
  17. this.toggleInterpreter
  18. .onTransition((state) => (this.current = state))
  19. .start();
  20. }
  21. willDestroy() {
  22. super.willDestroy(...arguments);
  23. this.toggleInterpreter.stop();
  24. }
  25. @action
  26. transition(...args) {
  27. this.toggleInterpreter.send(...args);
  28. }
  29. }