npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@d3v0ps/xstate-decorators

v2.1.1

Published

TypeScript decorators for creating XState machines

Downloads

18

Readme

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.