4

Take the following finite-state machine:

const machine = Machine({
  initial: "foo",
  states: {
    foo: {
      on: {
        BAZ: "baz",
        QUX: "qux",
      },
    },
    bar: {
      on: {
        BAZ: "baz",
        QUX: "qux",
      },
    },
    baz: {
      on: {
        FOO: "foo",
        BAR: "bar",
      },
    },
    qux: {
      on: {
        FOO: "foo",
        BAR: "bar",
      },
    },
  },
});

Notice that there are two duplicated sets of state transitions:

on: {
  FOO: "foo",
  bar: "bar",
}
on: {
  BAZ: "baz",
  QUX: "qux",
}

Other than defining the state transitions as good ol' JavaScript objects outside of the Machine definition, is there an idiosyncratic way to do this?

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114

1 Answers1

6

Other than defining the state transitions as good ol' JavaScript objects outside of the Machine definition

You answered your own question! You already know how to deduplicate this data, so define these as external objects and reference them within the machine. There doesn't need to be a special API to do this.

David Khourshid
  • 4,918
  • 1
  • 11
  • 10
  • 1
    Good to know! Other paradigms like GraphQL have their custom API for encouraging DRY-ness in your codebase, that is, [fragments](https://www.apollographql.com/docs/react/data/fragments/). – Paul Razvan Berg Nov 19 '19 at 11:29