Coder Social home page Coder Social logo

alfreema / vue-state-machine Goto Github PK

View Code? Open in Web Editor NEW
29.0 29.0 2.0 30 KB

This plugin is intended to be an easy to use finite state machine for vue based on vuex and David Khourshid's xstate package. It allows you to have multiple state machines and tracks each machine's current state.

JavaScript 100.00%

vue-state-machine's People

Contributors

alfreema avatar remcoplasmeyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vue-state-machine's Issues

Example of scoping for the re-entrant call of FSM transitions from within Vuex actions

In the readme "Other Thoughts" you say "I have found it easiest to place those calls within the actions of my vuex store. From there, it's easy to change the state of your state machine, based on the results of the calls".

I am trying to do exactly that with Axios, for a simple login form - tracking the success/fail of the login action and changing the state and storing session variables.

I can't figure out how to re-target the FSM again, from within the scope of the Axios callbacks, inside the action handler, which is called from a transition, which was called from a Vue method attached to a button.

In LoginPage.vue:

import authStore from './../components/Login/store' ;
import vsm from 'vue-state-machine'
...
  created() {
    // register store module for Auth FSM in the main vuex store
    this.$store.registerModule('AuthenticationFSM', authStore)
    this.$vsm.add('AuthenticationFSM', authStore.machine)
    this.$vsm.set('AuthenticationFSM')
  },
  methods: {
    loginClicked() {
        // Send payload through FSM to store-action: it will turn up in payload.params
        this.$vsm.transition('LOGIN_CLICKED', {username:this.username, password:this.password})

This calls the FSM fine, via this transition in store.js

    states: {
      auth_none: {
        on: {
            LOGIN_CLICKED: {
              auth_validating: {
              actions: ['LOGIN_POST']
            }
          }
        }
      },

and this also actions fine, calling my API

  actions: {
    LOGIN_POST(context, payload) {
      axios({
        method: 'post', url: "...",
        data: {username:payload.params.username, password:payload.params.password},
      })
      .then(response => {
          // HOW DO I RE-TARGET THE FSM HERE?
          vsm.transition('AUTH_SUCCESS');
      })
      .catch(function (error) {
        vsm.transition('AUTH_FAILURE');
      });
    },

I tried just re-importing like import vsm from 'vue-state-machine' to see if the module is a singleton but I get an error vue_state_machine__WEBPACK_IMPORTED_MODULE_1__.default.transition is not a function so I think I'm not understanding the module loader properly.

So I wondered if you had an example of how you achieved this re-entrancy.

thanks.

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.