singleton-dictionary
v1.0.1
Published
**Size: 203 B** (minified and gzipped). No dependencies. [Size Limit](https://github.com/ai/size-limit) controls the size.
Downloads
1
Readme
Singleton Dictionary
Size: 203 B (minified and gzipped). No dependencies. Size Limit controls the size.
It's a data structure that allows you to store signletons. It has such interface:
interface ISingletonDictionary<K extends string | number, V> {
acquire(k: K): V;
release(k: K): number;
}
Example of usage:
import { SingletonDictionary } from 'singleton-dictionary';
function factory(id: number) {
console.log('created');
return {
id,
};
}
const dict = new SingletonDictionary(factory);
dict.acquire(10);
//> 'created'
//> { id: 10 }
dict.acquire(10);
//> { id: 10 }
// The same object
dict.release(10);
//> 1 - remaining amount of reference to the singleton with key 10
dict.release(10);
//> 0 - remaining amount of reference to the singleton with key 10
// At this point garbage collector can collect this singleton
dict.acquire(10);
//> 'created'
//> { id: 10 }
// This is a new object