Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

@rstone770/brandy-lifecycles

0.0.2 • Public • Published

Brandy Lifecycles

Add life cycle support to brandy.

Build Status npm (scoped)

npm install @rstone770/brandy-lifecycles

By default, if a lifecycle is not explicitly defined, all registrations are assumed as SINGLETON which is different from vanilla brandy.

import { createContainer } from '@rstone770/brandy'
import { lifecycles, types } from '@rstone770/brandy-lifecycles'
 
const container = createContainer(lifecycles);
 
const factory = () => Object.create(null);
 
container.factory('default', factory);
container.factory('transient', factory, { lifecycle: types.TRANSIENT });
container.factory('singleton', factory, { lifecycle: types.SINGELTON });
 
container.instance('default') === container.instance('default'); // true
container.instance('transient') !== container.instance('transient'); // true
container.instance('singleton') === container.instance('singleton'); // true

API

lifecycles()

Enhances the container to allow lifecycle control. The enhancer looks at options.lifecycle passed in as the third argument to bind and factory to determine the lifecycle.

import { createContainer } from '@rstone770/brandy'
import { lifecycles, types } from '@rstone770/brandy-lifecycles'
 
const container = createContainer(lifecycles);
 
class SomeClass {}
 
container.factory('transient', () => Object.create(null), { lifecycle: 'transient' });
container.bind('singleton', SomeClass, { lifecycle: types.SINGELTON });
 
container.instance('transient') !== container.factory('transient') // true
container.instance('singleton') === container.instance('singleton'); // true

By default, if a lifecycle is not passed in, SINGLETON is used. Lifecycles can be from the types enum or a case insensitive string represting the lifecycle.

configureLifecycle(defaultLifecycle)

Creates an enhancer that is configured to use a specific lifecycle, if one is not defined.

 
import { createContainer } from '@rstone770/brandy'
import { configureLifecycle, types } from '@rstone770/brandy-lifecycles'
 
const container = createContainer(configureLifecycle(types.Transient));

types

Lifecycle values supported by this enhancer when registering dependencies and setting the defaultLifecycle when using configureLifecycle. Case insensitive string literals can be used instead.

License

MIT

install

npm i @rstone770/brandy-lifecycles

Downloadsweekly downloads

4

version

0.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar