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

execution-line

v1.0.0

Published

a hook system to execute functions sequentially, like "pipe" or "compose"

Downloads

3

Readme

execution-line

Ensures a sequential execution of functions, helps you escape callback hell, comes in handy when writing long and complex tasks. Works exactly as expressjs middlewares.

Installation

$ npm install execution-line

Browser

<script type="text/javascript" src="/path/to/execution-line.min.js"></script>

API

Constructor

ExecutionLine(context, myObject)

Parameters

Name | Type | Required | Description --------- | ------ | ---------| -- context | Object | Optional | thisArg for all functions on the line myObject | Object | Optional | a custom object to use across all functions

Usage

You must always use an instance.

let line = new ExecutionLine();

line.hook(fn)

Hook functions to the line.

Parameters

Name | Type | Required | Description --------- | ------ | ---------| -- fn | Function or Array | Required | if a Array is passed it must be an Array of Functions, otherwise it will throw TypeError

Usage

line.hook([
  function(next){ /* step 1 */ next(); },
  function(){ /* step 2 */ }
]);

line.exec()

Executes all hooked functions. Each function is removed from the line to be executed, if all functions were executed the line will be clear again. If exec() is called and the line is clear it will return false.

line.clear()

Clear line, removes all hooked functions.

line.onexecutionend

Executed after all functions, it shares the same context as your other functions. Only works if the last function calls next().

Parameters

Name | Type | Description --------- | ------ | -- myObject | Object or Null | a custom object to use across all functions

Usage

line.onexecutionend = function(myObject) {}

Usage

Example 1

Basic usage

// only needed on nodejs
let ExecutionLine = require('execution-line');

let lineA = new ExecutionLine();

function firstA(next) { 
  console.log('line A - first function');
  next();
}

function secondA() { 
  console.log('line A - second function')
}

lineA.hook(firstA);
lineA.hook(secondA);

let lineB = new ExecutionLine();

function firstB(next) {
  console.log('line B - first function');
  next();
}

function secondB(){
  console.log('line B - second function')
}

lineB.hook(firstB)
lineB.hook(secondB);

lineA.exec();
lineB.exec();

// outputs
// line A - first function
// line A - second function
// line B - first function
// line B - second function

Example 2

Changing context, thisArg

// only needed on nodejs
let ExecutionLine = require('execution-line');

let line  = new ExecutionLine({ e: 'element'});
line.hook(function(next){
  console.log(this.e);
});

line.exec();

// outputs
// element

Example 3

Passing a object to functions

// only needed on nodejs
let ExecutionLine = require('execution-line');

let line  = new ExecutionLine({}, { prop: 'string'});
function a(obj, next){
  console.log(obj.prop);
  next();
}
line.hook(a);
line.hook(a);

line.exec();

// outputs
// string
// string

Example 4

Using onexecutionend

// only needed on nodejs
let ExecutionLine = require('execution-line');

let line  = new ExecutionLine();
line.hook(function(next){
  console.log('step 1');
  // not calling next()
});

line.onexecutionend = function() {
  console.log('final step');
}

line.exec();

// outputs
// step 1

// hooking again because after execution the line is clear
line.hook(function(next) {
  console.log('step 1');
  // calling next()
  next();
});

// outputs
// step 1
// final step

Example 5

Escaping callback hell, silly example using callback:

element.onclick(function(event){
  event.target.disabled = true;
  xmlhttp.onload = function() {
    some.SDK.call(function() {
      anotherXmlHttp.onload = function() {
        event.target.disabled = false;
        // now its over
      }
      anotherXmlHttp.send();
    });
  }
  xmlhttp.send()
});

Same example using ExecutionLine:

function clickHandler(next) {
  this.disabled = true;
  next();
}

function ajaxCall(next) {
  xmlhttp.onload = function() {
    next();
  }
  xmlhttp.send();
}

function doSomething(next) {
  some.SDK.call(function(){
    next();
  });
}

function lastAjaxCall() {
  xmlhttp.onload = function() {
    this.disabled = false;
  }
  xmlhttp.send();
}

element.onclick = function() {
  let line  = new ExecutionLine(element);
  line.hook(clickHandler);
  line.hook(ajaxCall);
  line.hook(doSomething);
  line.hook(lastAjaxCall);
  line.exec();
}

License

MIT