singleton-pattern 1.1.2 → 1.2.0
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 +4 -31
- package/dist/index.mjs +1 -1
- package/package.json +2 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
// # from: global.d.ts
|
|
2
|
-
type Class = new (...args: any[]) => any;
|
|
3
|
-
type ProxiedClass = new (...args: any[]) => any;
|
|
4
|
-
|
|
5
|
-
interface SingletonifyOptions {
|
|
6
|
-
/**
|
|
7
|
-
* Preventing the `.prototype.constructor` from being accessed
|
|
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
|
|
13
|
-
* - will change `Origin.prototype.constructor` to the singletonified class
|
|
14
|
-
* - this means Origin.prototype.constructor !== origin
|
|
15
|
-
*/
|
|
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;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// # index.d.ts
|
|
29
1
|
/**
|
|
30
2
|
* ## Usage
|
|
31
3
|
* Just wrap your class with this function to create a new class that always returns the same instance
|
|
@@ -37,13 +9,14 @@ interface SingletonifyOptions {
|
|
|
37
9
|
* ## About
|
|
38
10
|
* @package SingletonPattern
|
|
39
11
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
40
|
-
* @version 1.
|
|
12
|
+
* @version 1.2.0 (Last Update: 2026.02.13 16:46:16.089)
|
|
41
13
|
* @license MIT
|
|
42
14
|
* @link https://github.com/baendlorel/singleton-pattern
|
|
15
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
43
16
|
* @description Wrap a class with proxy to make it a safe singleton constructor. Using new Class() will return the same instance.
|
|
44
|
-
* @copyright Copyright (c)
|
|
17
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
45
18
|
*/
|
|
46
|
-
declare
|
|
19
|
+
declare function singletonify<T extends Class>(target: T, options?: SingletonifyOptions): T;
|
|
47
20
|
/**
|
|
48
21
|
* Retrieves the original class from the singletonified class
|
|
49
22
|
* @param singleton The singletonified class
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
const n=new WeakMap,t=new WeakMap;function e(e,c){const o=Object(c),r=o.changeProtoConstructor??!0,s=o.onlyOnce??!0;if(s){const n=t.get(e);if(n)return n}let a;const u=new Proxy(e,{construct:(n,t)=>(a||(a=new n(...t)),a)});return r&&(u.prototype.constructor=u),s&&(n.set(u,e),t.set(e,u)),u}const c=t=>n.get(t);export{c as getSingletonTarget,e as singletonify};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "singleton-pattern",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Kasukabe Tsumugi",
|
|
6
6
|
"email": "futami16237@gmail.com"
|
|
@@ -39,25 +39,5 @@
|
|
|
39
39
|
"build": "rimraf dist && rollup -c"
|
|
40
40
|
},
|
|
41
41
|
"license": "MIT",
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@babel/plugin-proposal-decorators": "^7.28.0",
|
|
44
|
-
"@babel/preset-env": "^7.28.3",
|
|
45
|
-
"@rollup/plugin-alias": "^5.1.1",
|
|
46
|
-
"@rollup/plugin-babel": "^6.0.4",
|
|
47
|
-
"@rollup/plugin-commonjs": "^28.0.6",
|
|
48
|
-
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
49
|
-
"@rollup/plugin-replace": "^6.0.2",
|
|
50
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
51
|
-
"@rollup/plugin-typescript": "^12.1.4",
|
|
52
|
-
"@types/node": "^24.2.1",
|
|
53
|
-
"@vitest/coverage-v8": "^3.2.4",
|
|
54
|
-
"oxlint": "^1.11.2",
|
|
55
|
-
"prettier": "^3.6.2",
|
|
56
|
-
"rimraf": "^6.0.1",
|
|
57
|
-
"rollup": "^4.46.2",
|
|
58
|
-
"rollup-plugin-dts": "^6.2.1",
|
|
59
|
-
"tslib": "^2.8.1",
|
|
60
|
-
"typescript": "^5.9.2",
|
|
61
|
-
"vitest": "^3.2.4"
|
|
62
|
-
}
|
|
42
|
+
"devDependencies": {}
|
|
63
43
|
}
|