Description
Ditox.js has a very simple API which works with containers, tokens, values and value factories. There are no class decorators, field injectors and other magic stuff. Only explicit binding and resolving. Ditox.js supports a container hierarchy, "scoped" containers and multi-value tokens.
Ditox.js alternatives and similar libraries
Based on the "Misc" category.
Alternatively, view Ditox.js alternatives based on common mentions on social networks and blogs.
-
list.js
The perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on existing HTML. -
InversifyJS
A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript. -
Autotrack
DISCONTINUED. Automatic and enhanced Google Analytics tracking for common user interactions on the web. -
mixitup
DISCONTINUED. A high-performance, dependency-free library for animated filtering, sorting, insertion, removal and more -
surveyjs
Free Open-Source JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout that lets you load and run multiple web forms, or build your own self-hosted form management system, retaining all sensitive data on your servers. You have total freedom of choice as to the backend, because any server + database combination is fully compatible.
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Ditox.js or a related project?
README
Ditox.js
Detoxed dependency injection container.
Table of Contents
<!-- toc -->
<!-- tocstop -->
Features
- Simple, functional API
- Container hierarchy
- Scoped containers
- Multi-value tokens
- Typescript and Flow typings
- Supports Node.js, Deno and browsers
Installation
Install with npm
npm install ditox --save
Or yarn
yarn add ditox
You can also use the UMD build from unpkg
<script src="https://unpkg.com/ditox/dist/index.browser.js" />
<script>
const container = Ditox.createContainer();
</script>
Usage with Deno:
import {createContainer} from 'https://deno.land/x/ditox/mod.ts';
const container = createContainer();
Usage
Ditox.js has a very simple API which works with containers, tokens, values and value factories. There are no class decorators, field injectors and other magic stuff. Only explicit binding and resolving.
In general, all you need is to do the following:
- Create binding tokens.
- Create a container.
- Bind some values or factories with the container.
- Resolve and use values.
import {createContainer, injectable, optional, token} from 'ditox';
// This is app code, some factory functions and classes:
function createStorage(config) {}
function createLogger(config) {}
class UserService {
constructor(storage, logger) {}
}
// Define tokens for injections.
// Token can be optional to provide default values.
const TOKENS = {
StorageConfig: optional(token(), {name: 'default storage'}),
Storage: token('Token description for debugging'),
Logger: token(),
UserService: token(),
};
// Create the container.
const container = createContainer();
// Provide a value to the container.
container.bindValue(TOKENS.StorageConfig, {name: 'custom storage'});
// Dynamic values are provided by factories.
// A factory can be decorated with `injectable()` to resolve its arguments.
// By default, a factory has `scoped` lifetime.
container.bindFactory(
TOKENS.Storage,
injectable(createStorage, TOKENS.StorageConfig),
);
// A factory can have `transient` lifetime to create a value on each resolving.
container.bindFactory(TOKENS.Logger, createLogger, {scope: 'transient'});
// Classes are provided by factories.
container.bindFactory(
TOKENS.UserService,
injectable(
(storage, logger) => new UserService(storage, logger),
TOKENS.Storage,
// A token can be made optional to resolve with a default value
// when it is not found during resolving.
optional(TOKENS.Logger),
),
{
// `scoped` and `singleton` scopes can have `onRemoved` callback.
// It is called when a token is removed from the container.
scope: 'singleton',
onRemoved: (userService) => userService.destroy(),
},
);
// Get a value from the container, it returns `undefined` in case a value is not found.
const logger = container.get(TOKENS.Logger);
// Resolve a value, it throws `ResolverError` in case a value is not found.
const userService = container.resolve(TOKENS.userService);
// Remove a value from the container.
container.remove(TOKENS.Logger);
// Clean up the container.
container.removeAll();
Container Hierarchy
Ditox.js supports "parent-child" hierarchy. If the child container cannot to resolve a token, it asks the parent container to resolve it:
import {creatContainer, token} from 'ditox';
const V1 = token();
const V2 = token();
const parent = createContainer();
parent.bindValue(V1, 10);
parent.bindValue(V2, 20);
const container = createContainer(parent);
container.bindValue(V2, 21);
container.resolve(V1); // 10
container.resolve(V2); // 21
Factory Lifetimes
Ditox.js supports managing the lifetime of values which are produced by factories. There are the following types:
scoped
- This is the default. The value is created and cached by the container which starts resolving.singleton
- The value is created and cached by the container which registered the factory.transient
- The value is created every time it is resolved.
scoped
This is the default scope. "Scoped" lifetime allows to have sub-containers with own instances of some services which can be disposed. For example, a context during HTTP request handling, or other unit of work:
import {creatContainer, token} from 'ditox';
const TAG = token();
const LOGGER = token();
const createLogger = (tag) => (message) => console.log(`[${tag}] ${message}`);
const parent = createContainer();
// `scoped` is default scope and can be omitted.
parent.bindFactory(LOGGER, injectable(createLogger, TAG, {scope: 'scoped'}));
const container1 = createContainer(parent);
container1.bindValue(TAG, 'container1');
const container2 = createContainer(parent);
container2.bindValue(TAG, 'container2');
parent.resolve(LOGGER)('xyz'); // throws ResolverError, the parent does not have TAG value.
container1.resolve(LOGGER)('foo'); // [container1] foo
container2.resolve(LOGGER)('bar'); // [container2] bar
// Dispose a container.
container1.removeAll();
singleton
"Singleton" allows to cache a produced value by a parent container which registered the factory:
import {creatContainer, token} from 'ditox';
const TAG = token();
const LOGGER = token();
const createLogger = (tag) => (message) => console.log(`[${tag}] ${message}`);
const parent = createContainer();
parent.bindValue(TAG, 'parent');
parent.bindFactory(LOGGER, injectable(createLogger, TAG, {scope: 'singleton'}));
const container1 = createContainer(parent);
container1.bindValue(TAG, 'container1');
const container2 = createContainer(parent);
container2.bindValue(TAG, 'container2');
parent.resolve(LOGGER)('xyz'); // [parent] xyz
container1.resolve(LOGGER)('foo'); // [parent] foo
container2.resolve(LOGGER)('bar'); // [parent] bar
transient
"Transient" makes to a produce values by the factory for each resolving:
import {creatContainer, token} from 'ditox';
const TAG = token();
const LOGGER = token();
const createLogger = (tag) => (message) => console.log(`[${tag}] ${message}`);
const parent = createContainer();
parent.bindValue(TAG, 'parent');
parent.bindFactory(LOGGER, injectable(createLogger, TAG, {scope: 'singleton'}));
const container1 = createContainer(parent);
container1.bindValue(TAG, 'container1');
const container2 = createContainer(parent);
container2.bindValue(TAG, 'container2');
parent.resolve(LOGGER)('xyz'); // [parent] xyz
container1.resolve(LOGGER)('foo'); // [container1] foo
container2.resolve(LOGGER)('bar'); // [container2] bar
parent.bindValue(TAG, 'parent-rebind');
parent.resolve(LOGGER)('xyz'); // [parent-rebind] xyz
API Reference
See API reference [here](./docs/api/README.md)
© 2020 Mikhail Nasyrov, [MIT license](./LICENSE)
*Note that all licence references and agreements mentioned in the Ditox.js README section above
are relevant to that project's source code only.