项目作者: ankitasharmace

项目描述 :
An easy-to-use dependency resolver using topological sorting.
高级语言: JavaScript
项目地址: git://github.com/ankitasharmace/toposort-dependency-resolver.git
创建时间: 2018-06-12T15:06:06Z
项目社区:https://github.com/ankitasharmace/toposort-dependency-resolver

开源协议:MIT License

下载


Overview

An easy-to-use dependency resolver using topological sorting.

Installation

  1. npm install toposort-dependency-resolver

then in your code:

  1. dependencyResolver = require('toposort-dependency-resolver')

Usage

This module resolves dependencies between various components and tells us the sequential order in which these components can be safely accessed.
The underlying algorithm being used is topological-sorting via a Directed Acyclic Graph(DAG).

DAG

API

  • resolveDependency(data, callback)

Examples :

1

  1. dependencyResolver.resolveDependency(
  2. [
  3. {
  4. component : "1",
  5. dependsOn : [],
  6. },
  7. {
  8. component : "2",
  9. dependsOn : ["1"],
  10. },
  11. {
  12. component : "3",
  13. dependsOn : ["1","2"],
  14. }
  15. ],function(err, result) {})
  1. If the input forms a directed acyclic graph,
  2. err : null
  3. result: [{"component" : 1"}, {"component" : "2"}, {"component" : "3"}]

2

  1. dependencyResolver.resolveDependency(
  2. [
  3. {
  4. component : "A",
  5. dependsOn : ["B"],
  6. },
  7. {
  8. component : "B",
  9. dependsOn : ["A"],
  10. },
  11. {
  12. component : "C",
  13. dependsOn : ["B"],
  14. }
  15. ],function(err, result) {})
  1. err.message : "Dependencies are cyclic"
  • resolveDependencyAndMakeApiCalls(data, callback)

Example :

  1. dependencyResolver.resolveDependencyAndMakeApiCalls(
  2. [
  3. {
  4. component : "A",
  5. dependsOn : [],
  6. options : {
  7. url : 'http://localhost:8080/api/bears',
  8. method:'GET',
  9. headers:{},
  10. data:{}
  11. }
  12. },
  13. {
  14. component : "B",
  15. dependsOn : ["A"],
  16. options : {
  17. url : 'http://localhost:8080/api/bears/',
  18. method : 'POST',
  19. headers : {},
  20. data : {}
  21. }
  22. },
  23. {
  24. component : "C",
  25. dependsOn : ["B","A"],
  26. options : {
  27. url : 'http://localhost:8080/api/bears/5b30774ebebd0f1700a39acf',
  28. method : 'PUT',
  29. headers : {},
  30. data : {}
  31. }
  32. }
  33. ],function(err) {})
  1. If dependencies are cyclic, this function returns err("Dependencies are cyclic").
  2. If it is a directed acyclic graph and all the API calls are successful, err is null.

Unit Tests and coverage

Unit test have been added for all functionalities.

  1. # Run tests
  2. npm run test
  3. # Run test along with code coverage
  4. npm run coverage

Coverage

Future Enhancements

  • Return callback errors based on HTTP status codes of API calls.
  • Save the progress of API calls made when an error occurs.
  • Add retries to API calls.

Contributing

Contributions, questions and comments are all welcome and encouraged. For code contributions submit a pull request with unit tests.

License

This project is licensed under the MIT License

Meta

Ankita Sharma – ankitasharma.ce@gmail.com