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

tasker-lib

v2.0.0

Published

A simple library for defining and executing JavaScript task trees.

Downloads

23

Readme

tasker-lib

A simple library for defining and executing JavaScript task trees.

Build Status npm dependencies Coverage Status npm version

Features

  • Support for synchronous, asynchronous, and promise-based tasks.
  • Automatic result passing between dependent tasks.
  • Rich API for building and modifying task trees.
  • Small, dependency-free, and available as both ES2015 and CommonJS libraries.

Installation

$ npm install tasker-lib

Note: tasker-lib requires a Promise implementation available on the global object.

Usage

Have you previously used a task-based build system? You'll feel right at home with the tasker-lib API.

Using tasker-lib is quick and easy:

  1. Create a new TaskRunner, optionally supplying configuration.
  2. Add tasks through the addTask method.
  3. Optionally modify the task tree by using removeTask, addDependencies, and removeDependencies. These may only be called while tasks are not being executed.
  4. Optionally use the dependency results with the results object. Each task only has access to the results of its direct dependencies.
  5. Run a task, and all its dependencies, with the run method. All methods are locked during execution.

General

import {TaskRunner} from "tasker-lib";

const taskRunner = new TaskRunner();

// This sync task returns 1, which becomes available to tasks that depend on it through the "results" object.
taskRunner.addTask("dependency", () => 1);

taskRunner.addTask("root", ["dependency"], (results) => {
   const dependencyResult = results["dependency"];
   console.log(`Result of "dependency" is: ${dependencyResult}`); // 1
   
   return dependencyResult + 1;
});

taskRunner.run("root").then((finalResult) => {
    console.log(`Final results are ${finalResult}`); // finalResult === 2
});

With Promises

// The dependency list is optional for tasks that have no dependencies.
taskRunner.addTask("promise-task", (results) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("some-async-result");
        }, 1000);
    });
});

With async callback

taskRunner.addTask("async-task", (results, done) => {
    setTimeout(() => {
        done("some-async-result");
    }, 1000);
});

With addDependencies, removeDependencies

Dependencies can be added or removed to any task that has already been added. Calling addDependency multiple times with the same taskName and dependencies will be ignored after the first call.

taskRunner.addTask("root", () => 3);

// Dependencies may be added before they exist, but the parent task must exist at this point.
taskRunner.addDependencies("root", ["child1"]);

taskRunner.addTask("child1", () => 1);
taskRunner.addTask("child2", () => 2);

taskRunner.addDependencies("child1", "child2"); // A string may be used for sole dependencies.

taskRunner.removeDependencies("root", "child1"); // child2 is still a dependency of child1, but not of root.

With throwOnOverwrite = false

const options = {
    throwOnOverwrite: false // defaults to true
};

const taskRunner = new TaskRunner(options);
taskRunner.addTask("root", () => 1);
taskRunner.addTask("root", () => 1); // This would throw if throwOnOverwrite is true.

With onTaskStart, onTaskEnd, onTaskCancel, and onTaskFail callbacks

Before each task starts, onTaskStart will be called with the task name and dependency list. Then, if there are any dependencies, they will be executed and onTaskStart will likewise be called with their name and dependency list.

onTaskEnd will be called with the task name once the task has completed.

onTaskCancel will be called with the task name if a dependent task prevented it from executing.

onTaskFail will be called with the task name if an error occurred during task execution.

const options = {
    onTaskStart: (taskName, taskDependencies) => {
        console.log(`Task started: '${taskName}'. Depends on ${taskDependencies}.`);  
    },
    
    onTaskEnd: (taskName) => {
        console.log(`Task ended: '${taskName}'`);
    }
};

const taskRunner = new TaskRunner(options);

taskRunner.addTask("child1", () => 1);
taskRunner.addTask("root", ["child1"], () => console.log(" - Running! - "));
taskRunner.run("root");

// Task started: 'root'. Depends on [].
//  - Running! -
// Task ended: 'root'

Full API

Command Line

For command line usage, see tasker-cli.

Versioning

tasker-lib uses SemVer for versioning. All releases will be available on both GitHub and npm.

License

MIT