singleton-pattern 1.2.0 → 1.2.4
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 +27 -2
- package/dist/index.mjs +18 -1
- package/package.json +46 -42
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
type Class = new (...args: any[]) => any;
|
|
2
|
+
|
|
3
|
+
interface SingletonifyOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Preventing the `.prototype.constructor` from being accessed
|
|
6
|
+
*
|
|
7
|
+
* Default is `true`
|
|
8
|
+
* - use Boolean Evaluation
|
|
9
|
+
* - it is **not recommended** to set this to `false`
|
|
10
|
+
* - if it is falsy, will remain the original constructor unchanged
|
|
11
|
+
* - will change `Origin.prototype.constructor` to the singletonified class
|
|
12
|
+
* - this means Origin.prototype.constructor !== origin
|
|
13
|
+
*/
|
|
14
|
+
changeProtoConstructor?: boolean;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Default is `true`
|
|
18
|
+
* - use Boolean Evaluation
|
|
19
|
+
* - it is **not recommended** to set this to `false`
|
|
20
|
+
* - if it is falsy, it will create a new singletonified class every time and will not cache it
|
|
21
|
+
* - by default, wrap a class multiple times will always return the same singletonified class
|
|
22
|
+
*/
|
|
23
|
+
onlyOnce?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
1
26
|
/**
|
|
2
27
|
* ## Usage
|
|
3
28
|
* Just wrap your class with this function to create a new class that always returns the same instance
|
|
@@ -9,9 +34,9 @@
|
|
|
9
34
|
* ## About
|
|
10
35
|
* @package SingletonPattern
|
|
11
36
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
12
|
-
* @version 1.2.
|
|
37
|
+
* @version 1.2.4 (Last Update: 2026.02.17 18:56:05.498)
|
|
13
38
|
* @license MIT
|
|
14
|
-
* @link https://github.com/baendlorel/
|
|
39
|
+
* @link git+https://github.com/baendlorel/kt-packages.git
|
|
15
40
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
16
41
|
* @description Wrap a class with proxy to make it a safe singleton constructor. Using new Class() will return the same instance.
|
|
17
42
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
const n=
|
|
1
|
+
const n = new WeakMap, t = new WeakMap;
|
|
2
|
+
|
|
3
|
+
function e(e, c) {
|
|
4
|
+
const o = Object(c), r = o.changeProtoConstructor ?? !0, s = o.onlyOnce ?? !0;
|
|
5
|
+
if (s) {
|
|
6
|
+
const n = t.get(e);
|
|
7
|
+
if (n) return n;
|
|
8
|
+
}
|
|
9
|
+
let a;
|
|
10
|
+
const u = new Proxy(e, {
|
|
11
|
+
construct: (n, t) => (a || (a = new n(...t)), a)
|
|
12
|
+
});
|
|
13
|
+
return r && (u.prototype.constructor = u), s && (n.set(u, e), t.set(e, u)), u;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const c = t => n.get(t);
|
|
17
|
+
|
|
18
|
+
export { c as getSingletonTarget, e as singletonify };
|
package/package.json
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
2
|
+
"name": "singleton-pattern",
|
|
3
|
+
"version": "1.2.4",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Kasukabe Tsumugi",
|
|
6
|
+
"email": "futami16237@gmail.com"
|
|
7
|
+
},
|
|
8
|
+
"description": "Wrap a class with proxy to make it a safe singleton constructor. Using new Class() will return the same instance.",
|
|
9
|
+
"description_zh": "使用代理包装类以使其成为安全的单例构造函数(原型的构造函数也将被修改)。使用 new Class() 将返回相同的实例",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"default": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/baendlorel/kt-packages/tree/main/packages/singleton-pattern#readme",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/baendlorel/kt-packages.git",
|
|
23
|
+
"directory": "packages/singleton-pattern"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"deep",
|
|
27
|
+
"clone",
|
|
28
|
+
"reflect",
|
|
29
|
+
"property",
|
|
30
|
+
"path",
|
|
31
|
+
"object",
|
|
32
|
+
"nested",
|
|
33
|
+
"typescript",
|
|
34
|
+
"javascript"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "clear & vitest",
|
|
38
|
+
"lint": "oxlint .",
|
|
39
|
+
"cover": "clear & vitest --coverage",
|
|
40
|
+
"build": "rimraf dist && rollup -c"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"devDependencies": {},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/baendlorel/kt-packages/issues"
|
|
46
|
+
}
|
|
47
|
+
}
|