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

tioc

v1.0.2

Published

A simple inversion of control container

Downloads

118

Readme

TIOC - Type Inversion of Control

License JSR JSR Score NPM Minified size Zipped minified size

A simple Inversion of Control library for JavaScript.

Features

  • 3 different scopes:
    • Singleton - Instantiated once per ServiceRegistry
    • Scoped - Instantiated once per ServiceProvider
    • Transient - Instantiated every time when requested
  • Simple, only includes what's necessary
  • Tiny, if you care about size, it's only around ~1kb (minified)

Usage

Adding services

import { ServiceRegistry } from "tioc";

class Service {
  // ...
}

ServiceRegistry.create()
  // Adding a simple service 
  .add("singleton", "myService", () => new Service())

  // Alternative way to write it!
  .addSingleton("foo", () => new Service())
  .addScoped("bar", () => new Service())
  .addTransient("baz", () => new Service());

Requesting services

import { ServiceRegistry } from "tioc";

class Service {
  // ....
}

// Register a new instance of Service to the key "foo"
const registry = ServiceRegistry.create()
  .addSingleton("foo", () => new Service());

// Start a new scope. This will give you a ServiceProvider
const provider = registry.scope();

// Retreive the service by the key, "foo"
const service = provider.foo();

Dependent services

tioc allows services to depend on another. Factory functions are given a provider instance that they can use to get previously registered services.

import { ServiceRegistry } from "tioc";

class Config {
  // ...
}

class Database {
  constructor(private config: Config) {}
  // ...
}

// ✅ Register the service you want to depend on first, and then use it!
ServiceRegistry.create()
  .addSingleton("config", () => new Config())
  .addSingleton("database", (provider) => new Database(provider.config())); // `provider` is a ServiceProvider, like the one received from `ServiceRegistry.scope()`

// ❌ Doesn't work. Services can't depend on ones that arent registered yet. This prevents cyclic dependencies.
ServiceRegistry.create()
  .addSingleton("database", (provider) => new Database(provider.config())) // Error: Property 'config' does not exist on type '{}'.
  .addSingleton("config", () => new Config());

Async services

It isn't recommended to make your services async, but sometimes there is no way around it. The ServiceRegistry and ServiceProvider are built to allow for async services.

import { ServiceRegistry } from "tioc";

class Exchange {}
class Queue {}

declare class MessageQueue {
  makeExchange(): Promise<Exchange>;
  makeQueue(exchange: Exchange): Promise<Queue>;
}

const registry = ServiceRegistry.create()
  .addSingleton("mq", () => new MessageQueue())
  // Simply return a promise
  .addSingleton("exchange", (provider) => provider.mq().makeExchange())
  // Factory functions are allowed to be async.
  .addSingleton("queue", async (provider) => {
    const exchange = await provider.exchange()
    return await provider.mq().makeQueue(exchange);
  });

const provider = registry.scope();

// Async trickles down into the provider. 
// If your factory returns a Promise, your provider will.
const exchange = await provider.exchange();
const queue = await provider.queue();