singleton-pattern 1.0.0 → 1.1.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/README.md +26 -12
- 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
|
|