2.1.1 ā€¢ Published 3 years ago

@d3v0ps/xstate-decorators v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

TypeScript decorators for creating XState machines

šŸ  Homepage


Install

npm install @d3v0ps/xstate-decorators

Usage

Getting Started

Machine Example taken from XState

State Machine Executor

The state machine executor allows you to define a basic state machine with a transition method

import {
  StateMachine,
  StateMachineExecutor,
} from '@d3v0ps/xstate-decorators';

export interface DoorMachineContext {
  level: string;
  alert: boolean; // alert when intrusions happen
}

@StateMachine<DoorMachineContext>({
  id: 'door',
  initial: 'closed',
  context: {
    level: 'user',
    alert: false,
  },
  states: {
    ...
  },
})
export class DoorMachineExecutor extends StateMachineExecutor<DoorMachineContext> {

  openTheDoor() {
    return this.transition('closed', 'OPEN')
  }
}

const doorMachineExecutor = new StateMachineExecutor();

doorMachineExecutor.transition('closed', 'OPEN');

State Machine Interpreter

The state machine interpreter allows you to define a state machine interpreter with a send method

import {
  Interpreter,
  StateMachineInterpreter,
} from '@d3v0ps/xstate-decorators';

export interface DoorMachineContext {
  level: string;
  alert: boolean; // alert when intrusions happen
}

@Interpreter<DoorMachineContext>({
  id: 'door',
  initial: 'closed',
  context: {
    level: 'user',
    alert: false,
  },
  states: {
    ...
  },
})
export class DoorMachineInterpreter extends StateMachineInterpreter<DoorMachineContext> {

  openTheDoor() {
    return this.send('OPEN')
  }
}

const doorMachineInterpreter = new DoorMachineInterpreter();

doorMachineInterpreter.send('OPEN');

Guards

You can define your guards inside the class using the @Guard decorator

import {
  Interpreter,
  Guard,
  StateMachineInterpreter,
} from '@d3v0ps/xstate-decorators';

...

@Interpreter<DoorMachineContext>({
  ...
})
export class DoorMachineInterpreter extends StateMachineInterpreter<DoorMachineContext> {

  @Guard('isAdmin')
  isAdmin(context: DoorMachineContext) {
    return context.level === 'admin';
  }
}

Actions

You can define your actions inside the class using the @Action decorator

import {
  Action,
  Interpreter,
  StateMachineInterpreter,
} from '@d3v0ps/xstate-decorators';

...

@Interpreter<DoorMachineContext>({
  ...
})
export class DoorMachineInterpreter extends StateMachineInterpreter<DoorMachineContext> {

  @Action('logEvent')
  logEvent(context: DoorMachineContext, event) {
    console.log(event);
  }
}

Activities

You can define your activities inside the class using the @Activity decorator

import {
  Activity,
  Interpreter,
  StateMachineInterpreter,
} from '@d3v0ps/xstate-decorators';

...

@Interpreter<DoorMachineContext>({
  ...
})
export class DoorMachineInterpreter extends StateMachineInterpreter<DoorMachineContext> {

  @Activity('sayHello')
  sayHello() {
    this.helloService.sayHello();
  }
}

Services

You can define your services inside the class using the @Service decorator

import {
  Interpreter,
  Service
  StateMachineInterpreter,
} from '@d3v0ps/xstate-decorators';

...

@Interpreter<DoorMachineContext>({
  ...
})
export class DoorMachineInterpreter extends StateMachineInterpreter<DoorMachineContext> {

  @Service('getDoor')
  async getDoor(context: DoorMachineContext): Promise<DoorMachineContext> {
    return this.doorService.getDoor(context);
  }
}

Using different instances on each call (different contexts)

i.e loading it from db or/and with Dependency Injection

Executor:

const doorMachineExecutor = new DoorMachineExecutor();
const doorFromDb = {
  state: 'closed',
  context: {
    level: 'user',
    alert: false,
  },
};

doorMachineExecutor.transition(doorFromDb.state, 'OPEN', doorFromDb.context);

Interpreter:

const doorMachineInterpreter = new DoorMachineInterpreter();
const { state, context } = {
  state: 'closed',
  context: {
    level: 'user',
    alert: false,
  },
};

doorMachineInterpreter.send('OPEN', null, {
  state,
  context,
});

Using with Dependency Injection

Note: If you are using a dependency injection provider you probably will want to use different instances on each call see Using different instances on each call (different contexts)

Angular

Just import the Injectable decorator and add it before your class

import { StateMachine, Guard, StateMachineExecutor } from '@d3v0ps/xstate-decorators';
import { Injectable } from '@angular/core';

@StateMachine<DoorMachineContext>({
  ...
})
@Injectable({ providedIn: 'root' })
export class DoorMachineExecutor extends StateMachineExecutor<DoorMachineContext> {

  constructor(private checkIsAdmin: CheckIsAdmin) {
    super();
  }

  @Guard('isAdmin')
  isAdmin(contex: DoorMachineContextt) {
    return this.checkIsAdmin.with(context);
  }

}

NestJS

Just import the Injectable decorator and add it before your class

import { StateMachine, Guard, StateMachineExecutor } from '@d3v0ps/xstate-decorators';
import { Injectable } from '@nestjs/common';

@StateMachine<DoorMachineContext>({
  ...
})
@Injectable()
export class DoorMachineExecutor extends StateMachineExecutor<DoorMachineContext> {

  constructor(private checkIsAdmin: CheckIsAdmin) {
    super();
  }

  @Guard('isAdmin')
  isAdmin(contex: DoorMachineContextt) {
    return this.checkIsAdmin.with(context);
  }

}

Run tests

npm test

Author

šŸ‘¤ d3v0ps

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ā­ļø if this project helped you!

Sponsors

šŸ“ License

Copyright Ā© 2021 d3v0ps. This project is MIT licensed.

2.1.1

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago