Git Product home page Git Product logo

Comments (3)

cassiozen avatar cassiozen commented on May 17, 2024 1

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.

jonathanj avatar jonathanj commented on May 17, 2024 1

Thanks for the clear and detailed reply, @cassiozen!

from usestatemachine.

cassiozen avatar cassiozen commented on May 17, 2024

Closing for now, please feel free to reopen.

from usestatemachine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.