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

sequential-workflow-machine

v0.7.1

Published

Powerful sequential workflow machine for frontend and backend applications.

Downloads

516

Readme

Sequential Workflow Machine

Build Status License: MIT View this project on NPM

The powerful sequential workflow machine for frontend and backend applications. It provides a simple API for creating its own step execution handlers (activities). It supports multiple types of activities. Internally it uses the xstate library.

This machine uses the same data model as the Sequential Workflow Designer. So you can create a workflow definition in the designer and then run it by this machine easily.

📝 Check the documentation for more details.

🚀 Installation

Install the following packages by NPM command:

npm i sequential-workflow-model sequential-workflow-machine

🎬 Usage

You can use the machine in a JavaScript or TypeScript application. We recommend using TypeScript because a workflow uses a lot of data structures and it's hard to maintain data integrity.

At the beginning, you need to define the type of your workflow definition.

import { Definition } from 'sequential-workflow-model';

interface MyDefinition extends Definition {
  properties: {
    verbose: boolean;
  };
}

Next, define your step types.

import { Step } from 'sequential-workflow-model';

interface DownloadHtmlStep extends Step {
  componentType: 'task';
  type: 'downloadHtml';
  properties: {
    pageUrl: string;
  };
}

// ...

Prepare the workflow definition.

const definition: MyDefinition = {
  properties: {
    verbose: true,
  },
  sequence: [
    {
      id: '0x00001',
      componentType: 'task',
      type: 'downloadHtml',
      name: 'Download google.com',
      properties: {
        pageUrl: 'https://www.google.com',
      },
    },
  ],
};

Prepare the global state interface.

interface WorkflowGlobalState {
  html: string | null;
}

Prepare activities for your steps. The machine supports multiple types of activities. The basic activity is the atom activity. It's a simple handler that executes an atomic step and updates the global state.

import { createAtomActivity } from 'sequential-workflow-machine';

interface DownloadHtmlStepState {
  attempt: number;
}

const downloadHtmlActivity = createAtomActivity<DownloadHtmlStep, WorkflowGlobalState, DownloadHtmlStepState>('downloadHtml', {
  init: () => ({
    attempt: 0,
  }),
  handler: async (step: DownloadHtmlStep, globalState: WorkflowGlobalState, activityState: DownloadHtmlStepState) => {
    globalState.html = await downloadHtml(step.properties.pageUrl);
    activityState.attempt++;
  },
});

Now we can create the activity set. The activity set is a collection of all supported activities.

import { createActivitySet } from 'sequential-workflow-machine';

const activitySet = createActivitySet<WorkflowGlobalState>([
  downloadHtmlActivity,
]);

Finally, we can create the workflow machine and run it.

import { createWorkflowMachineBuilder } from 'sequential-workflow-machine';

const builder = createWorkflowMachineBuilder<WorkflowGlobalState>(activitySet);
const machine = builder.build(definition);
const interpreter = machine.create({
  init: () => {
    return {
      html: null,
    };
  }
});
interpreter.onChange(() => { /* ... */ });
interpreter.onDone(() => { /* ... */ });
interpreter.start();

That's it!

💡 License

This project is released under the MIT license.