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

@rs-box/ez-flow

v1.1.2

Published

Library for a workflow engine

Downloads

4

Readme

ez-flow

npm version build status npm bundle size

It's a library implementing a simple workflow engine.

This library was heavily inspired to j-easy/easy-flows in the Java world.

Installation

npm install @rs-box/ez-flow

Usage

A Workflow is a collection of Work Units must be run tu achieve a given purpose.

Each Work unit implements a behaviour and interacts with outer environment and the other units by the means of a Context exchanged between them. You can place each unit in a flow applying some logic to control it.

That is you can place unit in a sequential flow, in a conditional one, in a parallel one and so on.

At the end of this "chain" we obtain the final result.

In this library are actually available the following flow constructs:

  • Sequential Flow
  • Conditional Flow
  • Iterative Flow
  • Parallel Flow

flows

How to apply a flow?

Suppose you have defined a work unit called PrintMessageWork that implements Work. It takes a message and its call method prints it:

import {
  Work,
  WorkContext,
  WorkReport,
  DefaultWorkReport,
  WorkStatus,
} from '@rs-box/ez-flow';

export class PrintMessageWork implements Work {
  private message: string;

  constructor(message: string) {
    this.message = message;
  }

  getName() {
    return 'print message';
  }

  async call(workContext: WorkContext): Promise<WorkReport> {
    console.log(this.message);
    return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
  }
}

Now suppose to create the following workflow:

  • Print "foo" 3 times
  • Then print "hello" and "world" in parallel
  • Then if both "hello" and "world" have been successfully printed, finally print "ok", otherwise print "nok".

This workflow can be illustrated as follows:

sample workflow

  • flow1 is a RepeatFlow of work1 which prints "foo" 3 times.
  • flow2 is a ParallelFlow of work2 and work3 which prints "hello" and "world" in parallel
  • flow3 is a ConditionalFlow. It first executes flow2, then if flow2 is completed, it executes work4 (print "ok"), otherwise executes work5 (print "nok")
  • flow4 is a SequentialFlow. It executes flow1 then flow3 in sequence

This is a code snippet for the above example:

import {
  ConditionalFlow,
  SequentialFlow,
  RepeatFlow,
  ParallelFlow,
  WorkFlowEngine,
  WorkStatus,
  WorkContext,
  WorkFlowEngineBuilder,
  WorkReport,
} from '@rs-box/ez-flow';
import { PrintMessageWork } from './print-message-work';

// 1. Build work units
const work1: PrintMessageWork = new PrintMessageWork('foo');
const work2: PrintMessageWork = new PrintMessageWork('hello');
const work3: PrintMessageWork = new PrintMessageWork('world');
const work4: PrintMessageWork = new PrintMessageWork('ok');
const work5: PrintMessageWork = new PrintMessageWork('nok');

// 2. Build workflow
const workflow = SequentialFlow.Builder.newFlow() // flow 4
  .addWork(
    RepeatFlow.Builder.newFlow() // flow 1
      .withName('print foo')
      .withWork(work1)
      .withTimes(3)
      .build(),
  )
  .addWork(
    ConditionalFlow.Builder.newFlow() // flow 3
      .withWork(
        ParallelFlow.Builder.newFlow() // flow 2
          .withName('print hello world')
          .addWork(work2)
          .addWork(work3)
          .build(),
      )
      .then(work4)
      .otherwise(work5)
      .build(),
  )
  .build();

// set needed attributes in workContext
const workContext = new WorkContext();

// 3. Run workflow
const workFlowEngine: WorkFlowEngine = WorkFlowEngineBuilder.newBuilder().build();

workFlowEngine.run(workflow, workContext).then(
  (finalReport: WorkReport) => {
    if (finalReport.getWorkStatus() === WorkStatus.COMPLETED) {
      // Completed successfully
      console.log('Completed successfully');
    } else {
      // There was a failure
      const err = finalReport.getError();
      // Show error...
      console.error('error: ', err);
    }
  },
  err => {
    console.error('general error: ', err);
  },
);