Comments (3)
Hi, good question. This is by design - Ideally, changing the state machine context is always a consequence of the state machine itself transitioning. It should never come from the outside.
My suggestion is, as you noted, internalise the effect:
const [state, send] = useStateMachine({firebaseValue: …})({
initial: 'loading',
states: {
loading: {
on: {
SUCCESS: 'loaded',
FAILURE: 'error',
},
effect(send, update) {
const success = firebaseValue => {
update(context => ({firebaseValue, ...context}))
send('SUCCESS')
}
const failure = error => {
update(context => ({error, ...context}))
send('FAILURE')
}
const ref = firebase.database().ref('some/path')
// Manage the subscriber lifecycle.
ref.on('value', success, failure)
return () => {
ref.off('value', success)
}
},
},
// …
},
})
But this will unsubscribe as soon as the first piece of data is fetched. Another strategy would be using nested states, which this library doesn't support yet (working on it). A nested state would allow you to do something like this:
const [state, send] = useStateMachine({firebaseValue: …})({
initial: 'subscribe',
states: {
subscribe: {
initial: 'loading',
on: {
SUCCESS: 'active',
FAILURE: 'error',
},
states: {
loading: {},
active: {},
error: {}
},
effect(send, update) {
const success = value => {
update(context => ({value, ...context}))
send('SUCCESS')
}
const failure = error => {
update(context => ({error, ...context}))
send('FAILURE')
}
const ref = firebase.database().ref('some/path')
// Manage the subscriber lifecycle.
ref.on('value', success, failure)
return () => {
ref.off('value', success)
}
},
},
// …
},
})
This would start as subscribe > loading, then would either transition to subscribe > error or subscribe > active. The effect on the parent "subscribe" state would still be active, sending updates.
Right now, I would suggest using XState for this, and keep an eye on the progress on #21
from usestatemachine.
Thanks for the clear and detailed reply, @cassiozen!
from usestatemachine.
Closing for now, please feel free to reopen.
from usestatemachine.
Related Issues (20)
- Suggestion: mayTransition function to check if a `send()` would pass guards. HOT 6
- Suggestion: Have an explicit initial event? HOT 1
- Types Roadmap HOT 26
- `nextEvents.includes` inferred to `never` HOT 9
- DX for typed events HOT 7
- "$$initial" event discussion. HOT 11
- Document the difference between nextEvents & nextEventsT HOT 1
- Update references to init event on state
- Documentation discussion HOT 9
- Prevent memory leaks HOT 3
- Disallow extra events in schema
- How to use external machine config with correct type (using TypeScript)? HOT 8
- Make "creating" and "using" a machine seperate HOT 5
- Migrate away form TSDX
- useMachine return undefined in production environment HOT 5
- [Question] How to update context without changing state? HOT 1
- How to expose send func with useImperativeHandle?
- Async Guard functions?
- How to get the type for state and send?
- Issues with Typescript v5.4 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from usestatemachine.