singleton-pattern 1.1.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/dist/index.d.ts +19 -5
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
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};
|