Gathering detailed insights and metrics for vue-xstate
Gathering detailed insights and metrics for vue-xstate
Gathering detailed insights and metrics for vue-xstate
Gathering detailed insights and metrics for vue-xstate
npm install vue-xstate
Typescript
Module System
75
Supply Chain
99.5
Quality
75.7
Maintenance
100
Vulnerability
100
License
TypeScript (70.98%)
JavaScript (29.02%)
Total Downloads
4,504
Last Day
21
Last Week
21
Last Month
61
Last Year
438
MIT License
7 Stars
95 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Apr 04, 2022
Minified
Minified + Gzipped
Latest Version
2.1.8
Package Id
vue-xstate@2.1.8
Unpacked Size
206.57 kB
Size
42.98 kB
File Count
10
Cumulative downloads
Total Downloads
Last Day
0%
21
Compared to previous day
Last Week
425%
21
Compared to previous week
Last Month
24.5%
61
Compared to previous month
Last Year
12.9%
438
Compared to previous year
1
1
20
This project aims to give users of VueJS a easy access platform to use xState library in their project.
If you're not familiar with xState check first their site before using this!
In this version I've moved away from the mixin structure for three reasons:
With the new approach of a class it can be explicitly initialized, doesn't overwrite any property and it can be instantiated anywhere and shared through props/imports. This also adds an extra benefit, this class can now be used in other projects that don't use vue.
To migrate your components from v1, you need to remove the mixin from it and the initialization of the machine from your created() hook. To simplify a bit your work, you can add this piece of code in your component to have a map of your data to the new machine.
Note that the subscription system has now been removed as now being a class you can initiate, it can be passed as a property and shared the instance through multiple components.
You will have to replace the types with the ones on your state machine
1public machine: StateMachine<TContext, TStateSchema, TEvents> = new StateMachine(initialContext, machineConfig); 2 3public get context(): TContext { 4 return this.machine.context; 5} 6 7public get currentState(): StateMachineStateName<TStateSchema> { 8 return this.machine.state; 9} 10 11public get stateHash(): StateMachineStateName<TStateSchema> { 12 return this.machine.stateHash; 13} 14 15public dispatch(action: TEvents): void { 16 this.machine.dispatch(action); 17}
yarn add vue-xstate
or
npm i vue-xstate
:warning: Remember, since version 2.1.0, xstate is a peer dependency and you will have to install it in your project to use with this library.
Using the documentation of XState, define your context, states, events and state machine. For this case you will only define the objects, you don't need to use any of the methods indicated in that documentation as the mixin takes care of all internally.
I do recommend to keep the types and the machine in a separated file to make it easier to read and maintain. I also recommend to follow the nomenclature pattern. In the future I will try to add a small scaffolding tool to use with npm to generate the base files and types.
Here is an example state machine file:
1/// traffic-light.machine.ts 2 3import { MachineConfig, StateNodeConfig } from 'xstate'; 4import { assign } from 'xstate/lib/actions'; 5 6// State machine context interface 7export interface TrafficLightContext { 8 carCount: number; 9 finedPlates: string[]; 10} 11 12// Initial context used in the xStateInit() method 13export const TrafficLigtInitialContext: TrafficLightContext = { 14 carCount: 0, 15 finedPlates: [], 16}; 17 18// Possible states of the machine 19export enum TrafficLightStates { 20 RED = 'RED', 21 AMBER = 'AMBER', 22 GREEN = 'GREEN', 23} 24 25// Possible actions of the whole machine 26export enum TrafficLightActions { 27 GO_GREEN = 'GO_GREEN', 28 GO_AMBER = 'GO_AMBER', 29 GO_RED = 'GO_RED', 30 COUNT_CAR = 'COUNT_CAR', 31 FINE_CAR = 'FINE_CAR', 32} 33 34// Utility interface to get proper types on our config 35export interface TrafficLightSchema { 36 states: { 37 [state in TrafficLightStates]: StateNodeConfig<TrafficLightContext, TrafficLightSchema, TrafficLightEvent>; 38 }; 39} 40 41// Events that will be sent on the dispatch with their payload definition 42export type TrafficLightEvent = 43 | { type: TrafficLightActions.GO_GREEN } 44 | { type: TrafficLightActions.GO_AMBER } 45 | { type: TrafficLightActions.GO_RED } 46 | { type: TrafficLightActions.COUNT_CAR } 47 | { type: TrafficLightActions.FINE_CAR; plate: string }; 48 49// Definition of the state machine 50export const TrafficLightMachineConfig: MachineConfig<TrafficLightContext, TrafficLightSchema, TrafficLightEvent> = { 51 initial: TrafficLightStates.RED, 52 states: { 53 [TrafficLightStates.RED]: { 54 on: { 55 [TrafficLightActions.GO_GREEN]: { 56 target: TrafficLightStates.GREEN, 57 }, 58 [TrafficLightActions.FINE_CAR]: { 59 actions: assign((ctx, event) => ({ 60 finedPlates: [...ctx.finedPlates, event.plate], 61 })), 62 }, 63 }, 64 }, 65 [TrafficLightStates.AMBER]: { 66 on: { 67 [TrafficLightActions.GO_RED]: { 68 target: TrafficLightStates.RED, 69 }, 70 }, 71 }, 72 [TrafficLightStates.GREEN]: { 73 on: { 74 [TrafficLightActions.GO_AMBER]: { 75 target: TrafficLightStates.AMBER, 76 }, 77 [TrafficLightActions.COUNT_CAR]: { 78 actions: assign((ctx, event) => ({ 79 carCount: ctx.carCount + 1, 80 })), 81 }, 82 }, 83 }, 84 }, 85};
Remember that in the machine configuration you can use any functionality provided by xState
Once you have your class component created do the following:
1import { Component, Vue } from 'vue-property-decorator'; 2import { StateMachine } from 'vue-xstate'; 3import { 4 TrafficLightContext, 5 TrafficLightStateSchema, 6 TrafficLightEvents, 7 TrafficLigtInitialContext, 8 TrafficLightMachineConfig, 9} from './TrafficLight.machine.ts'; 10 11@Component 12class TrafficLight extends Vue { 13 // Your class definition 14 readonly machine: StateMachine<TrafficLightContext, TrafficLightStateSchema, TrafficLightEvents> = new StateMachine( 15 TrafficLigtInitialContext, 16 TrafficLightMachineConfig, 17 ); 18}
This will create and initiate the state machine with the defined initial state. Then you can use the StateMachine properties and methods to render and operate your machine. All the properties are reactive in the normal vue flow, which allows you to use them directly in your template/methods.
Now that you have machine initialized, you will have access to the following data, methods and props:
All the properties listed here are getter to prevent you to accidentally modify them. In any case you should always observe the principle of immutability when designing your states.
Name | Type | Description |
---|---|---|
context | TContext | Contain the context of the state machine, it will change as you move through the actions |
state | string | Current state name based on your StateSchema definition |
stateHash | string | State hash will provide a unique UUID every time there's a state change in the state machine |
The constructor method initialize the state machine.
1new StateMachine(
2 initialContext: TContext,
3 machineConfig: MachineConfig<TContext, TStateSchema, TEvents>,
4 configOptions?: Partial<MachineOptions<TContext, TEvents>>
5)
Name | Type | Optional | Description |
---|---|---|---|
initialContext | TContext | false | Initial context that will be accessible through context data |
machineConfig | MachineConfig<TContext, TStateSchema, TEvents> | false | xState machine configuration here |
configOptions | Partial<MachineOptions<TContext, TEvents> | true | xState machine options here |
This method allows to dispatch events to the state machine
1dispatch(action: TEvents): void
Name | Type | Description |
---|---|---|
action | TEvents | Action object with its payload |
yarn install
yarn run build
yarn run test
yarn run lint
MIT License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
74 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More