jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.69k stars 964 forks source link

Add custom attributes to transitions and lifecycle events #163

Open frapan opened 6 years ago

frapan commented 6 years ago

I have a state machine to manage the different states of a product, eg: product-inserted, product-confirmed, price-inserted, price-confirmed, etc. Different users can change the state of the product, depending on their roles, eg: logistics can insert product, marketing can confirm price, etc. I was thinking about checking the role in a generic onBeforeTransition event and declaring the allowed roles directly in the transitions array, eg:

transitions: [
  { name: 'insert-product', from: 'none', to: 'product-inserted', role: 'logistics' },
  { name: 'confirm-price, from: 'price-inserted', to: 'price-confirmed', role: 'marketing' },
]

Unfortunately, the role attribute is not passed to the lifecycle events. What do you think about passing the whole transition object to the lifecycle events instead of just name, from and to? In this way that would be a breaking change, but it could also be implemented in different ways:

I guess that custom attributes could be useful in many situations. What do you think?