Skip to content

Commit

Permalink
feat: rename main fn to singa
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 4, 2024
1 parent 806203e commit b30f3e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
47 changes: 44 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
[![Known Vulnerabilities][snyk-src]][snyk-href]
[![Conventional Commits][conventional-src]][conventional-href]

This is a package to create and manage singleton instances.
This is a tiny library to create and manage singleton instances.

**Table of Contents**
- [Installation](#installation)
- [Usage](#usage)
- [Types](#types)
- [Contributing](#contributing)
- [License](#license)

Expand All @@ -22,14 +23,18 @@ npm install singa --save

## Usage

The **singa** function returns an object of type [Singa](#singa),
with all input parameters, including name and factory, being entirely optional.

```typescript
import { defineSingleton } from 'singa';
import { singa } from 'singa';

class Foo {

}

const singleton = defineSingleton({
const singleton = singa({
name: 'singleton',
factory() {
return new Foo();
},
Expand All @@ -38,6 +43,42 @@ const singleton = defineSingleton({
const instance = singleton.use();
```

## Types

### Singa
```typescript
declare type Singa<T> = {
/**
* Create or us existing singleton instance.
*/
use: () => T,
/**
* Set the singleton instance.
*
* @param instance
*/
set: (instance: T) => void,
/**
* Set factory fn for instance creation.
*
* @param factory
*/
setFactory: (factory: Factory<T>) => void,
/**
* Reset the singleton instance.
*/
reset: () => void,
/**
* Check if the singleton instance is set.
*/
has: () => boolean,
/**
* Check if a factory fn is set.
*/
hasFactory: () => boolean
};
```

## Contributing

Before starting to work on a pull request, it is important to review the guidelines for
Expand Down
5 changes: 3 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Factory, Singleton, SingletonCreateContext } from './types';
import type { Factory, Singa, SingaCreateContext } from './types';

export function defineSingleton<T = any>(context: SingletonCreateContext<T> = {}) : Singleton<T> {
export function singa<T = any>(context: SingaCreateContext<T> = {}) : Singa<T> {
let instance : T | undefined;

let factory : Factory<T> | undefined;
Expand Down Expand Up @@ -28,6 +28,7 @@ export function defineSingleton<T = any>(context: SingletonCreateContext<T> = {}
factory = input;
},
has: () => typeof instance !== 'undefined',
hasFactory: () => typeof factory !== 'undefined',
reset: () => {
instance = undefined;
},
Expand Down
29 changes: 26 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
export type Factory<T = any> = () => T;

export type SingletonCreateContext<T> = {
export type SingaCreateContext<T> = {
factory?: Factory<T>,
name?: string
};

export type Singleton<T> = {
export type Singa<T> = {
/**
* Create or us existing singleton instance.
*/
use: () => T,
/**
* Set the singleton instance.
*
* @param instance
*/
set: (instance: T) => void,
/**
* Set factory fn for instance creation.
*
* @param factory
*/
setFactory: (factory: Factory<T>) => void,
/**
* Reset the singleton instance.
*/
reset: () => void,
has: () => boolean
/**
* Check if the singleton instance is set.
*/
has: () => boolean,
/**
* Check if a factory fn is set.
*/
hasFactory: () => boolean
};
12 changes: 6 additions & 6 deletions test/unit/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineSingleton } from '../../src';
import { singa } from '../../src';

class Foo {

}

describe('src/index.ts', () => {
it('should create singleton', () => {
const singleton = defineSingleton({
const singleton = singa({
factory() {
return new Foo();
},
Expand All @@ -18,15 +18,15 @@ describe('src/index.ts', () => {
});

it('should set singleton', () => {
const singleton = defineSingleton();
const singleton = singa();
expect(singleton.has()).toBeFalsy();
singleton.set(new Foo());
expect(singleton.use()).toBeInstanceOf(Foo);
expect(singleton.has()).toBeTruthy();
});

it('should reset singleton', () => {
const singleton = defineSingleton({
const singleton = singa({
factory() {
return new Foo();
},
Expand All @@ -40,7 +40,7 @@ describe('src/index.ts', () => {
});

it('should set factory', () => {
const singleton = defineSingleton();
const singleton = singa();

expect(singleton.has()).toBeFalsy();
singleton.setFactory(() => new Foo());
Expand All @@ -49,7 +49,7 @@ describe('src/index.ts', () => {
});

it('should throw error', () => {
const singleton = defineSingleton();
const singleton = singa();

try {
singleton.use();
Expand Down

0 comments on commit b30f3e4

Please sign in to comment.