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

dag-maker

v1.0.2

Published

DAG maker

Downloads

9

Readme

DAG Maker

DAG maker utilize topological sorting to make directed acyclic graphs.

It can be used to solve following problems:

  • Dependency resolution & injection
  • Figure out services start & stop order
  • Create & destroy objects in reasonable order

Installation

npm i dag-maker

Usage

Suppose we have two classes A and B. The dependency graph as follows:

A <- B

To instantiate them, first we create an instance of A, then pass the instance to B's constructor. Since there are only two classes, it's quite straightforward.

However, when it comes to a dozen classes with complex dependencies, it's no longer feasible to figure out the proper construction order manually. Not to mention the destruction order.

Here is an example (in TypeScript) presents how to solve this kind of problem with dag-maker:

import { dependencies, DagMaker } from 'dag-maker';

class A {
  static async create() {
    console.log('create A');
    return new A();
  }
  static async destroy(a: A) {
    console.log('destroy A:', a);
  }
}

@dependencies({
  a: A,
})
class B {
  constructor(a: A) {
    this.a = a;
  }
  static async create(options: { a: A }) {
    console.log('create B');
    return new B(options.a);
  }
  static async destroy(b: B) {
    console.log('destroy B:', b);
  }
}

const dagMaker = new DagMaker(A, B);
console.log(dagMaker.orderBy('dependencies'));
// [ [ 'A' ], [ 'B' ] ]

console.log(dagMaker.orderBy('dependents'));
// [ [ 'B' ], [ 'A' ] ]

const dag = await dagMaker.create();
// create A
// create B

console.log(dag);
// Map(2) { 'A' => A {}, 'B' => B { a: A {} } }

await dagMaker.destroy(dag);
// destroy B: B { a: A {} }
// destroy A: A {}

In this example, we implement factory methods create() and destroy() right inside class A and B, and declare B's dependencies with a decorator @dependencies. If you don't want to use decorator, declare a static variable named dependencies could achieve the same semantic. After then, we create a DagMaker for class A and B. Eventually, the DAG maker will inspect dependencies property and figure out how to construct A and B.