diff --git a/README.MD b/README.MD index 24ee63a..bce0c81 100644 --- a/README.MD +++ b/README.MD @@ -23,12 +23,14 @@ npm install singa --save ## Usage ```typescript +import { defineSingleton } from 'singa'; + class Foo { } -const singleton = createSingleton({ - create() { +const singleton = defineSingleton({ + factory() { return new Foo(); }, }); diff --git a/src/module.ts b/src/module.ts index 3f03bc9..de3c536 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,16 +1,21 @@ -import type { Singleton, SingletonCreateContext } from './types'; +import type { Factory, Singleton, SingletonCreateContext } from './types'; -export function createSingleton(context: SingletonCreateContext = {}) : Singleton { +export function defineSingleton(context: SingletonCreateContext = {}) : Singleton { let instance : T | undefined; + let factory : Factory | undefined; + if (context.factory) { + factory = context.factory; + } + return { use: () => { if (typeof instance !== 'undefined') { return instance; } - if (typeof context.create !== 'undefined') { - instance = context.create(); + if (typeof factory !== 'undefined') { + instance = factory(); return instance; } @@ -19,7 +24,10 @@ export function createSingleton(context: SingletonCreateContext = {} set: (input: T) => { instance = input; }, - isSet: () => typeof instance !== 'undefined', + setFactory: (input: Factory) => { + factory = input; + }, + has: () => typeof instance !== 'undefined', reset: () => { instance = undefined; }, diff --git a/src/types.ts b/src/types.ts index 974ac7b..31d8738 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,14 @@ +export type Factory = () => T; + export type SingletonCreateContext = { - create?: () => T, + factory?: Factory, name?: string }; export type Singleton = { use: () => T, set: (instance: T) => void, + setFactory: (factory: Factory) => void, reset: () => void, - isSet: () => boolean + has: () => boolean }; diff --git a/test/unit/index.spec.ts b/test/unit/index.spec.ts index 562c3c0..46b060c 100644 --- a/test/unit/index.spec.ts +++ b/test/unit/index.spec.ts @@ -1,4 +1,4 @@ -import { createSingleton } from '../../src'; +import { defineSingleton } from '../../src'; class Foo { @@ -6,41 +6,50 @@ class Foo { describe('src/index.ts', () => { it('should create singleton', () => { - const singleton = createSingleton({ - create() { + const singleton = defineSingleton({ + factory() { return new Foo(); }, }); - expect(singleton.isSet()).toBeFalsy(); + expect(singleton.has()).toBeFalsy(); expect(singleton.use()).toBeInstanceOf(Foo); - expect(singleton.isSet()).toBeTruthy(); + expect(singleton.has()).toBeTruthy(); }); it('should set singleton', () => { - const singleton = createSingleton(); - expect(singleton.isSet()).toBeFalsy(); + const singleton = defineSingleton(); + expect(singleton.has()).toBeFalsy(); singleton.set(new Foo()); expect(singleton.use()).toBeInstanceOf(Foo); - expect(singleton.isSet()).toBeTruthy(); + expect(singleton.has()).toBeTruthy(); }); it('should reset singleton', () => { - const singleton = createSingleton({ - create() { + const singleton = defineSingleton({ + factory() { return new Foo(); }, }); - expect(singleton.isSet()).toBeFalsy(); + expect(singleton.has()).toBeFalsy(); expect(singleton.use()).toBeInstanceOf(Foo); - expect(singleton.isSet()).toBeTruthy(); + expect(singleton.has()).toBeTruthy(); singleton.reset(); - expect(singleton.isSet()).toBeFalsy(); + expect(singleton.has()).toBeFalsy(); + }); + + it('should set factory', () => { + const singleton = defineSingleton(); + + expect(singleton.has()).toBeFalsy(); + singleton.setFactory(() => new Foo()); + expect(singleton.use()).toBeInstanceOf(Foo); + expect(singleton.has()).toBeTruthy(); }); it('should throw error', () => { - const singleton = createSingleton(); + const singleton = defineSingleton(); try { singleton.use();