| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import { createMachine, interpret,assign } from "xstate";
- export const machine = createMachine(
- {
- id: "trackTime",
- initial: "choose",
- context:{
- currentStatus:'',
- checkData:null
- },
- on: {
- RESET:{
- target:'.choose',
- actions:assign(_=>{
- return {
- currentStatus:''
- }
- })
- }
- },
- states: {
- choose: {
- on: {
- SLEEP: {
- target: "SLEEP"
- },
- FAST_SLEEP: {
- target: "FAST_SLEEP"
- },
- FAST: {
- target: "FAST"
- },
- },
- },
- SLEEP: {
- initial: "WAIT_FOR_START",
- states: {
- WAIT_FOR_START: {
- on: {
- START_SLEEP: {
- target: "ONGOING"
- },
- },
- },
- ONGOING: {
- on: {
- END_SLEEP: {
- target: "COMPLETED"
- },
- },
- },
- COMPLETED: {},
- },
- },
- FAST_SLEEP: {
- initial: "WAIT_FOR_START",
- states: {
- WAIT_FOR_START: {
- on: {
- START_FAST: {
- target: "ONGOING1"
- },
- },
- },
- ONGOING1: {
- on: {
- START_SLEEP: {
- target: "ONGOING2"
- },
- END_FAST: {
- target: "COMPLETED"
- },
- },
- },
- ONGOING2: {
- on: {
- END_FAST: {
- target: "COMPLETED"
- },
- END_SLEEP: {
- target: "ONGOING3"
- },
- },
- },
- COMPLETED: {},
- ONGOING3: {
- on: {
- END_FAST: {
- target: "COMPLETED"
- },
- },
- },
- },
- },
- FAST: {
- initial: "WAIT_FOR_START",
- states: {
- WAIT_FOR_START: {
- on: {
- START_FAST: {
- target: "ONGOING"
- },
- },
- },
- ONGOING: {
- on: {
- END_FAST: {
- target: "COMPLETED"
- },
- },
- },
- COMPLETED: {},
- },
- },
- },
- schema: {
- events: {} as
- | { type: "FAST" }
- | { type: "FAST_SLEEP" }
- | { type: "SLEEP" }
- | { type: "END_FAST" }
- | { type: "END_SLEEP" }
- | { type: "START_FAST" }
- | { type: "START_SLEEP" }
- | { type: "RESET" },
- },
- predictableActionArguments: true,
- preserveActionOrder: true,
- },
- {
- actions: {
- apple:assign({
- }),
- setCurrentStatus:assign((context, event) => {
- return {
- currentStatus: event.type
- }
- })
- },
- services: {},
- guards: {},
- delays: {},
- },
- );
- export const setSpecifiedState = (currentState, specifiedState) => {
- return assign({
- myMachine: {
- ...currentState.myMachine,
- value: specifiedState,
- },
- });
- };
- export const setSpecifiedStatus = (currentState: any, specifiedState: any) => {
- return assign({
- machine:{
- currentState,
- specifiedState
- }
- })
- }
- const service = interpret(machine).start();
- // const service = machine.withContext(initialContext).start();
- export default service;
|