项目作者: mtomran

项目描述 :
A modified version of Kahn's algorithm for topological sorting to resolve dependencies of asynchronous tasks.
高级语言: JavaScript
项目地址: git://github.com/mtomran/dep-resolver.git
创建时间: 2017-01-19T20:00:47Z
项目社区:https://github.com/mtomran/dep-resolver

开源协议:MIT License

下载


dep-resolver npm npm

A modified version of Kahn’s algorithm for topological sorting to resolve dependencies of asynchronous tasks.

Description

The goal of this package is to resolve dependencies of asynchronous tasks described with a Directed Acyclic Graph. Tasks are defined with functions that return promises and we are aiming to find an ordering of the tasks with the least overall wait time possible.

Usage

Define task nodes

The following defines a new node named title with a function to run with respect to task dependencies.
Provided functions are expected to return a promise when finished.

  1. addNode(title, function)

Example:

  1. const Promise= require("bluebird");
  2. const depResolver= require("dep-resolver");
  3. const a= depResolver.addNode("a", function (){ return Promise.delay(5000)});
  4. const b= depResolver.addNode("b", function (){ return Promise.delay(3000)});
  5. const c= depResolver.addNode("c", function (){ return Promise.delay(2000)});

Define task dependencies

  1. a.dependsOn(b);
  2. b.dependsOn(c);

Topological ordering of dependencies

  1. depResolver.sortAll()
  2. .then((sorted)=>{
  3. console.log("sorted nodes:", _.map(sorted, "title"));
  4. });

Licence

MIT