matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add default behavior to contextFn at createMachine

wuarmin opened this issue · comments

commented

Hey,
great lib! Thanks. What do you think about a default implementation of contextFn at createMachine?

If somebody uses react-robot and calls useMachine(flow, { foo: "bar"}), but has no contextFn prepared, the result is an empty initial context.

BR

This is intentional, a machine gets to decide what is in its context. It does this by accepting or rejecting context passed in when interpreted. If you feel like you want your machines to always accept a passed in object as its context you can easily do that with a little functional programming.

import { createMachine as defaultCreateMachine } from 'robot3';

export const createMachine = (states, context = {}) => defaultCreateMachine(states, () => context);