fn-machine
Advanced tools
Comparing version 0.0.16 to 0.0.17
{ | ||
"name": "fn-machine", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "a tiny functional state machine", | ||
@@ -5,0 +5,0 @@ "main": "index", |
@@ -14,3 +14,3 @@ # fn-machine | ||
The third function is what would traditionally be called a `send()` function. This function is returned whenever `machine(...)` is called. | ||
The third function is what would traditionally be called a `send()` function. This function is returned by calling `machine(...)`. | ||
@@ -26,4 +26,4 @@ #### Setting up a machine | ||
loading: false, | ||
users: [] | ||
} | ||
users: [], | ||
}; | ||
@@ -36,3 +36,3 @@ function loadUsers() { | ||
}, 1000); | ||
} | ||
}; | ||
@@ -44,8 +44,6 @@ // initialize a machine | ||
loadData: (detail, context) => { | ||
// a transition method should return the new state, as well as the optional context. | ||
// here we return {state:'loadingData'} to signify we want the state to now be 'loadingData', and | ||
// that the context.loading property should be true. | ||
// a transition should return the new state, as well as the optional context. | ||
// here we return {state:'loadingData'} to signify we want the state to now be 'loadingData'. | ||
return { | ||
state:'loadingData', | ||
context: {...context, ...{loading: true}} | ||
} | ||
@@ -61,6 +59,7 @@ } | ||
} | ||
}, context => {// call loadUsers when this state is entered | ||
}, context => {// call loadUsers when this state is entered, and return the new context. | ||
loadUsers(); | ||
return {...context, ...{loading: true}}; | ||
}), | ||
state('loadedData', {}) // 'loaded' is an empty state. There are no transitions. | ||
state('loadedData', {}) // 'loadedData' is an empty/final state. There are no transitions. | ||
], 'initial', initialContext, newState => { | ||
@@ -71,3 +70,3 @@ console.log('myMachine state changed:', newState.state, newState.context); | ||
``` | ||
As you can see in the `loadUsers()` function above, we invoke the third function provided by fn-machine, which is the send function. The send function takes a string as the first parameter, which is the name of a transition we'd like to invoke, and optionally a `detail` object, which might contain some data we want the machine to work with. | ||
In the `loadUsers()` function above, we invoke the third function provided by fn-machine, which is the send function. The send function takes a string as the first parameter, which is the name of a transition we'd like to invoke, and optionally a `detail` object, which contains some data we want the machine to work with, and/or update the context with. | ||
@@ -74,0 +73,0 @@ You can also define transitions using a short-hand syntax like so: |
@@ -17,3 +17,2 @@ /** @typedef {import('./fn-state').CurrentState} CurrentState */ | ||
let context = Object.assign({}, initialContext); | ||
const currentState = {state: current, context}; | ||
@@ -25,3 +24,3 @@ return function send(event, detail = {}) { | ||
loggerFn(`no event. returning currentState`); | ||
return currentState; | ||
return {state:current, context}; | ||
} | ||
@@ -76,10 +75,8 @@ // get the current/active state | ||
} | ||
return next; | ||
return {state:current, context}; | ||
} | ||
loggerFn('could not find active state'); | ||
return currentState; | ||
return {state:current, context}; | ||
} | ||
} | ||
14702
277
120