stent icon indicating copy to clipboard operation
stent copied to clipboard

Added entry & exit function support

Open rkoshy opened this issue 5 years ago • 5 comments

The changes allow each state to have an _entry and an _exit handler that acts as a special transition that executes on entry into, or exit from a state.

rkoshy avatar Apr 20 '20 06:04 rkoshy

I like the @enter/@exit naming schema.

Would it also make sense to omit those internal-use actions from the final machine methods, i.e., the machine should not have machine.enter() and machine.exit() methods?

illarionvk avatar Apr 22 '20 09:04 illarionvk

@illarionvk what are your concerns about having those defined. They'll be actually @enter and @exit.

krasimir avatar Apr 22 '20 09:04 krasimir

The camelcasing transform would rename the methods to .enter() and .exit(), wouldn't it?

I think having literal non-camelcased machine['@enter'] methods is ok, as long as the developer can define their own, explicit, enter/exit methods too:

const noop = function() {}

const config = {
  state: IDLE,
  transitions: {
    [IDLE]: {
      "@exit": function() { ... },
      init: function() { ... }
    },
    [INITIALIZING]: {
      "@enter": function() { ... },
      noop
    },
    [READY_TO_PROCESS]: {
      enter: function() { ... }
    },
    [PROCESSING]: {
      exit: function() { ... }
    },
    [DONE]: {
      noop
    }
  }
};

// The config should produce an instance similar to the type below
type MachineInstance {
  ...Machine,
  '@enter': () => void,
  '@exit': () => void,
  'enter': () => void,
  'exit': () => void,
  'init': () => void,
  'noop': () => void,
}

illarionvk avatar Apr 22 '20 10:04 illarionvk

I'm fine dealing with the last bit but can you add a test coverage of the changes.

Yes @krasimir I will add the tests.

rkoshy avatar Apr 22 '20 21:04 rkoshy

@illarionvk is completely right. We have to think about how the API will look like. The helper here will indeed remove the @, which is not necessary a bad thing but it shouldn't be exposed the same way the other actions are. I'll need to think it through. @rkoshy I'll try finding some time to explore more your PR.

krasimir avatar Apr 23 '20 08:04 krasimir