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

pipe-line

v1.2.1

Published

Pipelines to divide large tasks to, sequence of smaller and independent tasks.

Downloads

3

Readme

Pipe-line Build Status

A task assembler, to divide large tasks into independent and testable small task, and pipe them!

Usage

Install Pipe-line.

npm install --save pipe-line

Registering and executing tasks with pipeline.


// Import Pipeline
let Pipeline = require('pipe-line').Pipeline;

let pipe = new Pipeline('myPipe', null);

let something = getValue();

pipe.register((input, next) => { // A custom task with param input and next Function
  // Do something.
  setTimeout(() => {
    if (!something) {
      return next(new Error('Something is undefined'));
    }
    // Pass value to the next task.
    // First param - error
    // Second param - value to be passed to the next task
    return next(null, 10);
  }, 2000);
});

// Use ES5 function, to get a hold of the current task scope.
// 'this' scope will be unavailable when using ES6 arrow function.

// Actions parameter is an object containing exit & next Function.
pipe.register(function(input, next, actions) {
  let _self = this; // Task Scope. 
  
  if (someCondition) {
    return actions.exit(input + 2); // For Graceful pipeline exit.
  }
  
  _self.log('My Log message', 'and more message'); // Triggers the event 'log'
});

// Handling events

// Error event.
// params -> err, and name of the task it failed.
pipe.on('error', (err, taskName) => {
  // Handle stuff
});

// Log event.
pipe.on('log', (message, taskName) => {
  myLogger.info(message, taskName);
});


// Task start event.
// Triggers when a task starts.
pipe.on('task:start', (taskName) => {
  myLogger.info(`${taskName} started...`);
});

// Task Complete event.
// Triggers when a particular task finished executing.
// params -> taskName and ElapsedTime - Time elapsed for the task.
pipe.on('task:complete', (taskName, elapsedTime) => {
  myLogger.info(`${taskName} completed in ${elapsedTime}`);
});

// Done event.
// triggered when the pipeline completes executing all the tasks.
pipe.on('done', (result) => {
  // Result of the pipeline.
});

// Can register multiple tasks, each task's output will be input for the next task.
// Registering tasks can be chained.
// pipe.register(fn).register(fn).register(fn) ....

// Execute the pipeline containing the registered tasks, with an initial parameter.
// it returns a promise.
let pipePromise = pipe.execute(10); // this will be passed on to (input, next) => console.log(input); // 10;

// Any error thrown inside the task handler, will be handled in promise's catch block 
pipePromise.then((result) => console.log(result)).catch(console.error);

Using a dispatcher

A dispatcher is basically a conditional pipeline runner, Dispatcher takes in array of Pipelines as constructor param, and a dispatch function takes first param as a predicate and second param as the initial input just like Pipeline.execute(input)

  // Import dispatcher.
  const Dispatcher = require('pipe-line').Dispatcher;
  
  // Import pipeline
  const Pipeline = require('pipe-line').Pipeline;
  
  // Create pipelines that executes different tasks.
  let pipeOne = new Pipeline('pipeOne');
  let pipeTwo = new Pipeline('pipeTwo');
  
  // Load the pipelines into the dispatcher in the form of an array.
  let dispatcher = new Dispatcher([pipeOne, pipeTwo]);
  
  
  // First param - a predicate function that returns the name of a pipeline.
  // Second param - the initial input value, that needs to be passed onto the selected pipeline.
  // It also executes the pipeline and returns a promise with the result of the pipeline.
  // Returns a promise.
  dispatcher.dispatch(function() {
    if (someCondition) {
      return 'pipeOne'; // Pipeline name
    } else {
      return 'pipeTwo';
    } 
  }, 10).then((result) => {
    console.log(result);
  });;