singleton-pattern 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -12
- package/dist/index.d.ts +19 -5
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# singleton-pattern
|
|
2
2
|
|
|
3
3
|
🦄 Wrap your class to make it a true singleton!
|
|
4
4
|
|
|
5
|
-
> Note
|
|
5
|
+
> **Note**: Your environment must support ES6 Proxies.
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
|
-
`singleton-pattern` is a lightweight TypeScript/JavaScript utility that wraps a class constructor so that every `new` call returns the same instance. It uses Proxy to ensure singleton safety, and provides options for prototype
|
|
9
|
+
`singleton-pattern` is a lightweight TypeScript/JavaScript utility that wraps a class constructor so that every `new` call returns the same instance. It uses Proxy to ensure singleton safety, and provides options for prototype and proxy reuse.
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
13
|
- Make any class a singleton with one line
|
|
14
14
|
- Optionally control prototype.constructor behavior
|
|
15
|
+
- Optionally reuse proxy for the same class (`options.onlyOnce`)
|
|
15
16
|
- Retrieve the original class from a singletonified class
|
|
16
17
|
- Fully type-safe
|
|
17
18
|
|
|
@@ -28,25 +29,35 @@ pnpm add singleton-pattern
|
|
|
28
29
|
```typescript
|
|
29
30
|
import { singletonify, getSingletonTarget } from 'singleton-pattern';
|
|
30
31
|
|
|
31
|
-
class
|
|
32
|
+
class Target {
|
|
32
33
|
value: number;
|
|
33
34
|
constructor(v: number) {
|
|
34
35
|
this.value = v;
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
const
|
|
39
|
+
// Basic singleton
|
|
40
|
+
const S = singletonify(Target);
|
|
41
|
+
const a = new S(1);
|
|
42
|
+
const b = new S(2);
|
|
41
43
|
console.log(a === b); // true
|
|
42
44
|
console.log(a.value); // 1
|
|
43
45
|
|
|
46
|
+
// Retrieve original class
|
|
47
|
+
console.log(getSingletonTarget(S)); // Target
|
|
48
|
+
|
|
44
49
|
// Option: keep original prototype.constructor
|
|
45
|
-
const
|
|
46
|
-
console.log(
|
|
50
|
+
const S2 = singletonify(Target, { changeProtoConstructor: false });
|
|
51
|
+
console.log(S2.prototype.constructor === Target); // true
|
|
47
52
|
|
|
48
|
-
//
|
|
49
|
-
|
|
53
|
+
// Option: onlyOnce (reuse proxy for same class)
|
|
54
|
+
const S3 = singletonify(Target); // equivalent to (Target, { onlyOnce: true })
|
|
55
|
+
const S4 = singletonify(Target); // equivalent to (Target, { onlyOnce: true })
|
|
56
|
+
console.log(S3 === S4);
|
|
57
|
+
|
|
58
|
+
const S5 = singletonify(Target, { onlyOnce: false });
|
|
59
|
+
const S6 = singletonify(Target, { onlyOnce: false });
|
|
60
|
+
console.log(S5 !== S6);
|
|
50
61
|
```
|
|
51
62
|
|
|
52
63
|
## API
|
|
@@ -56,7 +67,10 @@ console.log(getSingletonTarget(Singleton)); // MyClass
|
|
|
56
67
|
Wraps a class constructor so that all `new` calls return the same instance.
|
|
57
68
|
|
|
58
69
|
- `target`: The class to wrap
|
|
59
|
-
- `options.
|
|
70
|
+
- `options.changeProtoConstructor` (default: `true`): Boolean. If `true`, sets `prototype.constructor` to the singletonified class. If `false`, keeps the original constructor.
|
|
71
|
+
- `options.onlyOnce` (default: `true`): Boolean. If `true`, reuses the same proxy for the same class. If `false`, creates a new proxy each time.
|
|
72
|
+
|
|
73
|
+
> **Note**: the options object uses Boolean Evaluation(aka. Boolean(x) / if (x))
|
|
60
74
|
|
|
61
75
|
### `getSingletonTarget<T extends Class>(singleton: T): T | undefined`
|
|
62
76
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
// # from: global.d.ts
|
|
2
2
|
type Class = new (...args: any[]) => any;
|
|
3
|
+
type ProxiedClass = new (...args: any[]) => any;
|
|
3
4
|
|
|
4
5
|
interface SingletonifyOptions {
|
|
5
6
|
/**
|
|
6
7
|
* Preventing the `.prototype.constructor` from being accessed
|
|
7
|
-
*
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
|
+
* Default is `true`
|
|
10
|
+
* - use Boolean Evaluation
|
|
11
|
+
* - it is **not recommended** to set this to `false`
|
|
12
|
+
* - if it is falsy, will remain the original constructor unchanged
|
|
9
13
|
* - will change `Origin.prototype.constructor` to the singletonified class
|
|
10
14
|
* - this means Origin.prototype.constructor !== origin
|
|
11
15
|
*/
|
|
12
|
-
|
|
16
|
+
changeProtoConstructor?: boolean;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Default is `true`
|
|
20
|
+
* - use Boolean Evaluation
|
|
21
|
+
* - it is **not recommended** to set this to `false`
|
|
22
|
+
* - if it is falsy, it will create a new singletonified class every time and will not cache it
|
|
23
|
+
* - by default, wrap a class multiple times will always return the same singletonified class
|
|
24
|
+
*/
|
|
25
|
+
onlyOnce?: boolean;
|
|
13
26
|
}
|
|
14
27
|
|
|
15
28
|
// # index.d.ts
|
|
@@ -18,12 +31,13 @@ interface SingletonifyOptions {
|
|
|
18
31
|
* Just wrap your class with this function to create a new class that always returns the same instance
|
|
19
32
|
* @param target The class to be wrapped
|
|
20
33
|
* @param options Advanced options
|
|
21
|
-
* -
|
|
34
|
+
* - changeProtoConstructor(default: `true`) will set `target.prototype.constructor` to the singletonified class
|
|
35
|
+
* - onlyOnce(default: `true`) will cache the input and always return the same proxied class
|
|
22
36
|
*
|
|
23
37
|
* ## About
|
|
24
38
|
* @package SingletonPattern
|
|
25
39
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
26
|
-
* @version 1.
|
|
40
|
+
* @version 1.1.1 (Last Update: 2025.08.15 11:07:44.767)
|
|
27
41
|
* @license MIT
|
|
28
42
|
* @link https://github.com/baendlorel/singleton-pattern
|
|
29
43
|
* @description Wrap a class with proxy to make it a safe singleton constructor. Using new Class() will return the same instance.
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const n=
|
|
1
|
+
const t=Proxy,n=Object;const e=new class{p2c=new WeakMap;c2p=new WeakMap;singletonify(e,o){const{changeProtoConstructor:s=!0,onlyOnce:c=!0}=n(o);if(c&&this.c2p.has(e))return this.c2p.get(e);let r;const i=new t(e,{construct:(t,n)=>(r||(r=new t(...n)),r)});return s&&(i.prototype.constructor=i),c&&(this.p2c.set(i,e),this.c2p.set(e,i)),i}getSingletonTarget(t){return this.p2c.get(t)}},o=e.singletonify.bind(e),s=e.getSingletonTarget.bind(e);export{s as getSingletonTarget,o as singletonify};
|