matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possibility to extend a machines invoke functions?

Avocher opened this issue · comments

Is there a way to extend machines like in xstate: https://xstate.js.org/docs/guides/machines.html#extending-machines. I would specifically be interested in overriding my invoked functions so i can reuse my machines.

My example use case is a form machines fetching data from different apis that have very different reponse formats.

With Robot reuse comes through composition. So I would suggest defining something like:

function formsMachine(fetch) {
  return createMachine({
    one: invoke(fetch) // ...
  })
}

const usersPageMachine = formsMachine(() => fetch('/api/foo'));
const adminPageMachine = formsMachine(() => fetch('/api/bar'));

That will work nicely, thx!