@ptolemy2002/immutability-utils 1.0.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 +164 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +56 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Immutability Utils
|
|
2
|
+
A TypeScript library that wraps mutable classes and makes them immutable using proxies and cloning. This library allows you to work with classes designed with a mutable API while automatically maintaining immutability through copy-on-write semantics.
|
|
3
|
+
|
|
4
|
+
## Features
|
|
5
|
+
|
|
6
|
+
- **Automatic Copy-on-Write**: Any mutation (property assignment or method call) automatically creates a clone of the object
|
|
7
|
+
- **Proxy-Based**: Uses JavaScript Proxies to intercept and handle mutations transparently
|
|
8
|
+
- **Zero Refactoring**: Works with existing mutable classes without requiring changes to the class implementation
|
|
9
|
+
- **Type-Safe**: Full TypeScript support with proper type inference
|
|
10
|
+
- **Efficient**: Groups multiple mutations within a single method call into one clone operation
|
|
11
|
+
- **Toggle Support**: Can temporarily disable immutability when needed
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @ptolemy2002/immutability-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
Your class must implement a `clone()` method that returns a deep copy of the instance.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Basic Example
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { immutable, ImmutableRef } from '@ptolemy2002/immutability-utils';
|
|
29
|
+
|
|
30
|
+
class Counter {
|
|
31
|
+
value: number;
|
|
32
|
+
|
|
33
|
+
constructor(value: number) {
|
|
34
|
+
this.value = value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
increment(amount: number): Counter {
|
|
38
|
+
this.value += amount;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
clone(): Counter {
|
|
43
|
+
return new Counter(this.value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Create an immutable reference
|
|
48
|
+
const counterRef = immutable(new Counter(0));
|
|
49
|
+
|
|
50
|
+
// This creates a new instance with value = 5
|
|
51
|
+
counterRef.current.increment(5);
|
|
52
|
+
console.log(counterRef.current.value); // 5
|
|
53
|
+
|
|
54
|
+
// The old instance is preserved
|
|
55
|
+
const oldCounter = counterRef.current;
|
|
56
|
+
counterRef.current.increment(3);
|
|
57
|
+
console.log(oldCounter.value); // 5 (unchanged)
|
|
58
|
+
console.log(counterRef.current.value); // 8 (new instance)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### How It Works
|
|
62
|
+
|
|
63
|
+
1. **Method Calls**: When you call a method that might mutate the object, the library first clones the object, then calls the method on the clone, and finally updates the reference to point to the new clone.
|
|
64
|
+
|
|
65
|
+
2. **Property Assignments**: When you set a property, the library clones the object first, applies the change to the clone, and updates the reference.
|
|
66
|
+
|
|
67
|
+
3. **Read Operations**: Getters and property reads do not trigger cloning.
|
|
68
|
+
|
|
69
|
+
4. **Chained Calls**: Multiple mutations within a single method call are treated as one mutation (only one clone is created).
|
|
70
|
+
|
|
71
|
+
### Advanced Features
|
|
72
|
+
|
|
73
|
+
#### Temporarily Disable Immutability
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const dataRef = immutable(new MyClass());
|
|
77
|
+
|
|
78
|
+
// Disable immutability temporarily
|
|
79
|
+
dataRef.enabled = false;
|
|
80
|
+
dataRef.current.someProperty = "mutated in place"; // No clone created
|
|
81
|
+
dataRef.enabled = true;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## API Reference
|
|
85
|
+
|
|
86
|
+
### Types
|
|
87
|
+
|
|
88
|
+
#### `Cloneable<T>`
|
|
89
|
+
|
|
90
|
+
A type representing an object that can be cloned. It's essentially `T` with a `clone()` method that returns `Cloneable<T>`.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
type Cloneable<T=object> = Override<T, { clone(): Cloneable<T> }>;
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### `ImmutableRef<T>`
|
|
97
|
+
|
|
98
|
+
A reference object containing the current immutable instance and an enabled flag.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
type ImmutableRef<T=object> = {
|
|
102
|
+
current: Cloneable<T>, // The current instance
|
|
103
|
+
enabled: boolean // Whether immutability is enabled
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Functions
|
|
108
|
+
|
|
109
|
+
#### `immutable<T>(obj: Cloneable<T>): ImmutableRef<T>`
|
|
110
|
+
|
|
111
|
+
Creates an immutable reference wrapper around the provided object.
|
|
112
|
+
|
|
113
|
+
**Parameters:**
|
|
114
|
+
- `obj`: An object that implements the `clone()` method
|
|
115
|
+
|
|
116
|
+
**Returns:**
|
|
117
|
+
- An `ImmutableRef<T>` object containing:
|
|
118
|
+
- `current`: The proxied instance (initially the object you passed in)
|
|
119
|
+
- `enabled`: A boolean flag (initially `true`) that controls whether immutability is active
|
|
120
|
+
|
|
121
|
+
**Example:**
|
|
122
|
+
```typescript
|
|
123
|
+
const ref = immutable(new MyClass());
|
|
124
|
+
ref.current.mutate(); // Creates a new instance
|
|
125
|
+
console.log(ref.enabled); // true
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## How Mutations Are Handled
|
|
129
|
+
|
|
130
|
+
1. **Property Setters**: When you set a property (e.g., `obj.prop = value`), the library:
|
|
131
|
+
- Clones the object
|
|
132
|
+
- Temporarily disables immutability during the setter execution
|
|
133
|
+
- Sets the property on the clone
|
|
134
|
+
- Re-enables immutability
|
|
135
|
+
- Updates `current` to point to the new clone
|
|
136
|
+
|
|
137
|
+
2. **Method Calls**: When you call a method (except `clone()`), the library:
|
|
138
|
+
- Clones the object
|
|
139
|
+
- Temporarily disables immutability during method execution
|
|
140
|
+
- Calls the method on the clone
|
|
141
|
+
- Re-enables immutability
|
|
142
|
+
- Updates `current` to point to the new clone
|
|
143
|
+
- Returns the method's result
|
|
144
|
+
|
|
145
|
+
3. **Read Operations**: Property reads and getters do not trigger cloning and return the value directly.
|
|
146
|
+
|
|
147
|
+
4. **The `clone()` Method**: Calling `clone()` explicitly does not trigger the immutability mechanism and returns a clone directly.
|
|
148
|
+
|
|
149
|
+
## Peer Dependencies
|
|
150
|
+
|
|
151
|
+
## Commands
|
|
152
|
+
The following commands exist in the project:
|
|
153
|
+
|
|
154
|
+
- `npm run uninstall` - Uninstalls all dependencies for the library
|
|
155
|
+
- `npm run reinstall` - Uninstalls and then Reinstalls all dependencies for the library
|
|
156
|
+
- `npm run example-uninstall` - Uninstalls all dependencies for the example app
|
|
157
|
+
- `npm run example-install` - Installs all dependencies for the example app
|
|
158
|
+
- `npm run example-reinstall` - Uninstalls and then Reinstalls all dependencies for the example app
|
|
159
|
+
- `npm run example-start` - Starts the example app after building the library
|
|
160
|
+
- `npm run build` - Builds the library
|
|
161
|
+
- `npm run release` - Publishes the library to npm without changing the version
|
|
162
|
+
- `npm run release-patch` - Publishes the library to npm with a patch version bump
|
|
163
|
+
- `npm run release-minor` - Publishes the library to npm with a minor version bump
|
|
164
|
+
- `npm run release-major` - Publishes the library to npm with a major version bump
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Override } from '@ptolemy2002/ts-utils';
|
|
2
|
+
export type Cloneable<T = object> = Override<T, {
|
|
3
|
+
clone(): Cloneable<T>;
|
|
4
|
+
}>;
|
|
5
|
+
export type ImmutableRef<T = object> = {
|
|
6
|
+
current: Cloneable<T>;
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare function immutable<T>(obj: Cloneable<T>): ImmutableRef<T>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.immutable = immutable;
|
|
7
|
+
const is_callable_1 = __importDefault(require("is-callable"));
|
|
8
|
+
function immutable(obj) {
|
|
9
|
+
const imRef = { current: obj, enabled: true };
|
|
10
|
+
// Wrap in a Proxy to override mutations
|
|
11
|
+
function applyProxy(target) {
|
|
12
|
+
return new Proxy(target, {
|
|
13
|
+
get(o, prop, receiver) {
|
|
14
|
+
const value = Reflect.get(o, prop, receiver);
|
|
15
|
+
if (!imRef.enabled)
|
|
16
|
+
return value;
|
|
17
|
+
const isFunction = (0, is_callable_1.default)(value);
|
|
18
|
+
if (isFunction && prop !== 'clone') {
|
|
19
|
+
// The function could mutate the object, so we need to clone it first
|
|
20
|
+
return function (...args) {
|
|
21
|
+
const cloned = o.clone();
|
|
22
|
+
// Disable for the duration of this call.
|
|
23
|
+
// All mutations in this call will be treated
|
|
24
|
+
// as one mutation.
|
|
25
|
+
imRef.enabled = false;
|
|
26
|
+
const result = cloned[prop](...args);
|
|
27
|
+
imRef.enabled = true;
|
|
28
|
+
// Sync the reference to the new cloned object
|
|
29
|
+
// and reapply the proxy
|
|
30
|
+
imRef.current = applyProxy(cloned);
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
},
|
|
36
|
+
set(o, prop, value, receiver) {
|
|
37
|
+
if (!imRef.enabled)
|
|
38
|
+
return Reflect.set(o, prop, value, receiver);
|
|
39
|
+
// On any mutation, clone the object first.
|
|
40
|
+
const cloned = o.clone();
|
|
41
|
+
// Disable for the duration of this call (it could be a setter that triggers more mutations).
|
|
42
|
+
// All mutations in this call will be treated
|
|
43
|
+
// as one mutation.
|
|
44
|
+
imRef.enabled = false;
|
|
45
|
+
const result = Reflect.set(cloned, prop, value, cloned);
|
|
46
|
+
imRef.enabled = true;
|
|
47
|
+
// Sync the reference to the new cloned object
|
|
48
|
+
// and reapply the proxy
|
|
49
|
+
imRef.current = applyProxy(cloned);
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
imRef.current = applyProxy(imRef.current);
|
|
55
|
+
return imRef;
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ptolemy2002/immutability-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"/dist"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/Ptolemy2002/immutability-utils",
|
|
13
|
+
"directory": "lib"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"prepare": "npx ts-patch install -s",
|
|
17
|
+
"build": "bash ./scripts/build.sh",
|
|
18
|
+
"_build": "tsc --project ./tsconfig.json",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"postinstall": "npx typesync",
|
|
21
|
+
"uninstall": "bash ./scripts/uninstall.sh",
|
|
22
|
+
"reinstall": "bash ./scripts/reinstall.sh",
|
|
23
|
+
"example-uninstall": "bash ./scripts/example-uninstall.sh",
|
|
24
|
+
"example-install": "bash ./scripts/example-install.sh",
|
|
25
|
+
"example-reinstall": "bash ./scripts/example-reinstall.sh",
|
|
26
|
+
"example-start": "bash ./scripts/example-start.sh",
|
|
27
|
+
"release": "bash ./scripts/release.sh",
|
|
28
|
+
"release-patch": "bash ./scripts/release.sh patch",
|
|
29
|
+
"release-minor": "bash ./scripts/release.sh minor",
|
|
30
|
+
"release-major": "bash ./scripts/release.sh major"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"ts-patch": "^3.3.0",
|
|
34
|
+
"tsconfig-paths": "^4.2.0",
|
|
35
|
+
"typescript-transform-paths": "^3.5.3"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@ptolemy2002/ts-utils": "^3.2.1",
|
|
39
|
+
"@types/is-callable": "^1.1.2",
|
|
40
|
+
"is-callable": "^1.2.7"
|
|
41
|
+
}
|
|
42
|
+
}
|