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

@libreworks/container

v0.1.36

Published

A simple dependency injection container and event target

Downloads

313

Readme

@libreworks/container

MIT npm GitHub Workflow Status (branch) GitHub release (latest SemVer) codecov

A simple asynchronous dependency injection container and event target.

Installation

npm install @libreworks/container

This library conforms to ECMAScript Modules (ESM). You can import this module using ESM or TypeScript syntax.

import { Builder, Container, Producer } from "@libreworks/container";

If you're using CommonJS, you must use dynamic imports instead.

Usage

You can use this library to create a graph of objects with intertwined dependencies. These objects and values can even be produced asynchronously.

Container

A container provides named values.

import { Foobar } from "your-package-name-here";

// Assume we created a Container.
declare const container: Container;
// Retrieve a value by its name.
const myObject: Foobar = await container.get("MyObject");
// Retrieve multiple values by their names.
const allObjects: Foobar[] = await container.getAll(["MyObject", "Another"]);
// Retrieve values by tag.
const taggedByMe: Foobar[] = await container.getAllTagged("my-tag");

Events

The Container class extends the EventTarget class (see MDN docs).

Builder

In order to put together a Container, you can use the Builder class.

const builder = new Builder()
  // Register a constant
  .constant("ExampleValue", "Lorem ipsum dolor sit amet")
  // Register an array.
  .constant("my.numbers", [1, 2, 3, 5, 8, 13, 21])
  // Register an object created synchronously.
  .register("RandomName", () => AnotherClass.doSomething())
  // Register an object created asynchronously.
  .register(
    "an-async-value",
    async (c) => {
      const myNumbers = await c.get("my.numbers");
      return await createNewThingy(myNumbers);
    },
    ["my-tag", "AnotherTag", "Yet another"]
  );

const container = await builder.build();

// Use the get method's generic type if you prefer.
const anAsyncValue = await container.get<MyClassName>("an-async-value");

You provide a name and a "factory" function when you call the register method. The factory function needs to return the value you want to register; it can be asynchronous or return a Promise as well.

Once the container is built, a call to get using the same name will invoke the factory function. The container provides itself as the first argument. That way, you can recursively locate other dependencies immediately before constructing the return value.

Since the container is an EventTarget, the factory function could broadcast an event, or the value returned by the factory function can register itself as an event listener. This feature allows objects inside the container to communicate in a loosely-coupled way.

The factory function will only ever be invoked once, no matter how many times get is invoked.

Eager Creation

Normally, values are lazy-loaded; they are created on-demand. However, you can provide the @eager tag when you call the register method to ensure your objects initialize themselves when the container is created.

const builder = new Builder().register("FooBar", () => new Thingy(), [
  "@eager",
]);
const container = await builder.build();
// Thingy has already been instantiated.