immu-js 0.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 ADDED
@@ -0,0 +1,90 @@
1
+ # immu-js
2
+
3
+ A tiny, framework-independent immutable state library for JavaScript/TypeScript.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install immu-js
9
+ ```
10
+
11
+ ## API
12
+
13
+ ### `create(Class)`
14
+
15
+ Creates a reactive store from a class definition. Properties become reactive (tracked via getters/setters). Methods are bound to the store.
16
+
17
+ ```typescript
18
+ import { create } from "immu-js";
19
+
20
+ class Counter {
21
+ count = 0;
22
+ incr() { this.count++; }
23
+ }
24
+
25
+ const store = create(Counter);
26
+ store.count; // 0
27
+ store.incr(); // count = 1
28
+ ```
29
+
30
+ **Store extras:**
31
+
32
+ - `store._version` — increments on every state change
33
+ - `store._subscribe(cb, prop?)` — subscribe to changes. Pass a prop name to only listen to that prop. Returns an unsubscribe function.
34
+ - `store._reset()` — resets the store to initial class values
35
+ - `store._saveVersion(label?)` — snapshots current state (default label: `"default"`)
36
+ - `store._loadVersion(label?)` — restores a saved snapshot
37
+
38
+ ### `memo(fn)`
39
+
40
+ Creates a memoized computed function. Tracks which stores are accessed and caches the result. Recomputes only when store state or arguments change.
41
+
42
+ ```typescript
43
+ import { memo } from "immu-js";
44
+
45
+ const doubled = memo((multiplier: number) => store.count * multiplier);
46
+ doubled(2); // computes
47
+ doubled(2); // cached
48
+ store.incr();
49
+ doubled(2); // recomputes
50
+ ```
51
+
52
+ ### `run(cb, deps?)`
53
+
54
+ Runs a callback reactively whenever tracked stores change. Returns a dispose function.
55
+
56
+ ```typescript
57
+ import { run } from "immu-js";
58
+
59
+ const dispose = run(() => {
60
+ console.log("count is", store.count);
61
+ });
62
+
63
+ store.incr(); // logs "count is 1"
64
+ dispose(); // stops reacting
65
+ ```
66
+
67
+ With optional deps for fine-grained control:
68
+
69
+ ```typescript
70
+ run(
71
+ () => console.log(store.count),
72
+ () => [store.count] // only re-run when these deps change
73
+ );
74
+ ```
75
+
76
+ ## Build
77
+
78
+ ```bash
79
+ npm run build
80
+ ```
81
+
82
+ ## Test
83
+
84
+ ```bash
85
+ npm test
86
+ ```
87
+
88
+ ## License
89
+
90
+ MIT
@@ -0,0 +1,40 @@
1
+ export declare class Counter {
2
+ count: number;
3
+ incr(): void;
4
+ decr(): void;
5
+ }
6
+ export declare class UserStore {
7
+ users: string[];
8
+ addUser(user: string): void;
9
+ }
10
+ export declare class AddressStore {
11
+ address: {
12
+ city: string;
13
+ zip: string;
14
+ };
15
+ changeCity(city: string): void;
16
+ }
17
+ export declare class ArrowStore {
18
+ count: number;
19
+ incr: () => void;
20
+ getCount: () => number;
21
+ }
22
+ export declare class Multi {
23
+ a: number;
24
+ b: number;
25
+ }
26
+ export declare class Empty {
27
+ }
28
+ export declare class Base {
29
+ baseCount: number;
30
+ baseName: string;
31
+ incrBase(): void;
32
+ }
33
+ export declare class Child extends Base {
34
+ childCount: number;
35
+ incrChild(): void;
36
+ }
37
+ export declare class GrandChild extends Child {
38
+ grandValue: string;
39
+ setGrand(val: string): void;
40
+ }
@@ -0,0 +1,4 @@
1
+ export interface Computed<T> {
2
+ readonly value: T;
3
+ }
4
+ export declare function computed<T>(fn: () => T): Computed<T>;
@@ -0,0 +1,2 @@
1
+ import type { Constructor, Store } from "./types";
2
+ export declare function create<T extends object>(Cls: Constructor<T>): Store<T>;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=new WeakMap;function h(n){let r=m.get(n);return r||(r={subscribers:[],version:0,dirtyProps:new Set},m.set(n,r)),r}let d=new Set,g=!1;function M(n,r){const t=h(n);t.version++,t.dirtyProps.add(r),d.add(n),g||(g=!0,queueMicrotask(k))}function k(){const n=d;d=new Set,g=!1;for(const r of n){const t=h(r),s=t.dirtyProps;t.dirtyProps=new Set;const f=s.has("*"),l=t.subscribers.filter(c=>f||c.prop===void 0||s.has(c.prop));for(const c of l)c.callback()}}const p=[];function b(){p.push(new Set)}function y(){return p.pop()??new Set}function v(n){p.length>0&&p[p.length-1].add(n)}function S(n){const r=new Set,t=[Object.prototype,Array.prototype,Function.prototype,String.prototype,Number.prototype,Boolean.prototype,Symbol.prototype,Date.prototype,RegExp.prototype,Map.prototype,Set.prototype,WeakMap.prototype,WeakSet.prototype,Promise.prototype,Error.prototype];let s=n;for(;s&&!t.includes(s);){for(const f of Reflect.ownKeys(s))f!=="constructor"&&r.add(f);s=Object.getPrototypeOf(s)}return Array.from(r)}function P(n){const r=new n,t=r,s=new Map,f=[],l=new Map,c=h(t),u=S(r);for(const e of u){if(typeof e=="symbol")continue;const o=e,i=r[o];typeof i!="function"&&(s.set(o,i),f.push(o))}for(const e of f)delete r[e],Object.defineProperty(t,e,{get(){return v(t),s.get(e)},set(o){s.set(e,o),M(t,e)},enumerable:!0,configurable:!0});return Object.defineProperty(t,"_version",{get(){return c.version},enumerable:!1,configurable:!1}),Object.defineProperty(t,"_subscribe",{value:(e,o)=>{const i={callback:e,prop:o};return c.subscribers.push(i),()=>{c.subscribers=c.subscribers.filter(a=>a!==i)}},enumerable:!1,configurable:!1,writable:!1}),Object.defineProperty(t,"_reset",{value:()=>{const e=new n,o=S(e);for(const i of o){if(typeof i=="symbol")continue;const a=i,w=e[a];typeof w!="function"&&(t[a]=w)}},enumerable:!1,configurable:!1,writable:!1}),Object.defineProperty(t,"_saveVersion",{value:(e="default")=>{const o=[];for(const[i,a]of s)o.push([i,structuredClone(a)]);l.set(e,o)},enumerable:!1,configurable:!1,writable:!1}),Object.defineProperty(t,"_loadVersion",{value:(e="default")=>{const o=l.get(e);if(!o)throw new Error(`No saved version found for label "${e}"`);for(const[i,a]of o)t[i]=structuredClone(a)},enumerable:!1,configurable:!1,writable:!1}),t}function _(n){let r,t=new Map,s,f=!1;function l(u){for(const[e,o]of u)if(e._version!==o)return!1;return!0}function c(u){for(const[e]of u)v(e)}return(...u)=>{const e=JSON.stringify(u);if(f&&e===r&&l(t))return c(t),s;b();const o=n(...u),i=y();t=new Map;for(const a of i)t.set(a,a._version);return r=e,s=o,f=!0,c(t),o}}function O(n){let r=new Map,t,s=!1;function f(c){for(const[u,e]of c)if(u._version!==e)return!1;return!0}function l(c){for(const[u]of c)v(u)}return{get value(){if(s&&f(r))return l(r),t;b();const c=n(),u=y();r=new Map;for(const e of u)r.set(e,e._version);return t=c,s=!0,l(r),c}}}function j(n,r){let t=!0,s=[],f=[],l;function c(){if(!t)return;if(r){const e=r();if(l&&A(l,e))return;l=e}for(const e of f)e();f=[],b(),n();const u=y();s=Array.from(u);for(const e of s){const o=e._subscribe(()=>{c()});f.push(o)}if(r){b(),r();const e=y();for(const o of Array.from(e))if(!s.includes(o)){const i=o._subscribe(()=>{c()});f.push(i)}}}return c(),()=>{t=!1;for(const u of f)u();f=[]}}function A(n,r){if(n.length!==r.length)return!1;for(let t=0;t<n.length;t++)if(n[t]!==r[t])return!1;return!0}exports._flush=k;exports.computed=O;exports.create=P;exports.memo=_;exports.run=j;
@@ -0,0 +1,228 @@
1
+ const k = /* @__PURE__ */ new WeakMap();
2
+ function g(n) {
3
+ let r = k.get(n);
4
+ return r || (r = { subscribers: [], version: 0, dirtyProps: /* @__PURE__ */ new Set() }, k.set(n, r)), r;
5
+ }
6
+ let d = /* @__PURE__ */ new Set(), h = !1;
7
+ function m(n, r) {
8
+ const t = g(n);
9
+ t.version++, t.dirtyProps.add(r), d.add(n), h || (h = !0, queueMicrotask(M));
10
+ }
11
+ function M() {
12
+ const n = d;
13
+ d = /* @__PURE__ */ new Set(), h = !1;
14
+ for (const r of n) {
15
+ const t = g(r), s = t.dirtyProps;
16
+ t.dirtyProps = /* @__PURE__ */ new Set();
17
+ const f = s.has("*"), l = t.subscribers.filter(
18
+ (c) => f || c.prop === void 0 || s.has(c.prop)
19
+ );
20
+ for (const c of l)
21
+ c.callback();
22
+ }
23
+ }
24
+ const p = [];
25
+ function b() {
26
+ p.push(/* @__PURE__ */ new Set());
27
+ }
28
+ function y() {
29
+ return p.pop() ?? /* @__PURE__ */ new Set();
30
+ }
31
+ function v(n) {
32
+ p.length > 0 && p[p.length - 1].add(n);
33
+ }
34
+ function S(n) {
35
+ const r = /* @__PURE__ */ new Set(), t = [
36
+ Object.prototype,
37
+ Array.prototype,
38
+ Function.prototype,
39
+ String.prototype,
40
+ Number.prototype,
41
+ Boolean.prototype,
42
+ Symbol.prototype,
43
+ Date.prototype,
44
+ RegExp.prototype,
45
+ Map.prototype,
46
+ Set.prototype,
47
+ WeakMap.prototype,
48
+ WeakSet.prototype,
49
+ Promise.prototype,
50
+ Error.prototype
51
+ ];
52
+ let s = n;
53
+ for (; s && !t.includes(s); ) {
54
+ for (const f of Reflect.ownKeys(s))
55
+ f !== "constructor" && r.add(f);
56
+ s = Object.getPrototypeOf(s);
57
+ }
58
+ return Array.from(r);
59
+ }
60
+ function _(n) {
61
+ const r = new n(), t = r, s = /* @__PURE__ */ new Map(), f = [], l = /* @__PURE__ */ new Map(), c = g(t), u = S(r);
62
+ for (const e of u) {
63
+ if (typeof e == "symbol") continue;
64
+ const o = e, i = r[o];
65
+ typeof i != "function" && (s.set(o, i), f.push(o));
66
+ }
67
+ for (const e of f)
68
+ delete r[e], Object.defineProperty(t, e, {
69
+ get() {
70
+ return v(t), s.get(e);
71
+ },
72
+ set(o) {
73
+ s.set(e, o), m(t, e);
74
+ },
75
+ enumerable: !0,
76
+ configurable: !0
77
+ });
78
+ return Object.defineProperty(t, "_version", {
79
+ get() {
80
+ return c.version;
81
+ },
82
+ enumerable: !1,
83
+ configurable: !1
84
+ }), Object.defineProperty(t, "_subscribe", {
85
+ value: (e, o) => {
86
+ const i = { callback: e, prop: o };
87
+ return c.subscribers.push(i), () => {
88
+ c.subscribers = c.subscribers.filter((a) => a !== i);
89
+ };
90
+ },
91
+ enumerable: !1,
92
+ configurable: !1,
93
+ writable: !1
94
+ }), Object.defineProperty(t, "_reset", {
95
+ value: () => {
96
+ const e = new n(), o = S(e);
97
+ for (const i of o) {
98
+ if (typeof i == "symbol") continue;
99
+ const a = i, w = e[a];
100
+ typeof w != "function" && (t[a] = w);
101
+ }
102
+ },
103
+ enumerable: !1,
104
+ configurable: !1,
105
+ writable: !1
106
+ }), Object.defineProperty(t, "_saveVersion", {
107
+ value: (e = "default") => {
108
+ const o = [];
109
+ for (const [i, a] of s)
110
+ o.push([i, structuredClone(a)]);
111
+ l.set(e, o);
112
+ },
113
+ enumerable: !1,
114
+ configurable: !1,
115
+ writable: !1
116
+ }), Object.defineProperty(t, "_loadVersion", {
117
+ value: (e = "default") => {
118
+ const o = l.get(e);
119
+ if (!o)
120
+ throw new Error(`No saved version found for label "${e}"`);
121
+ for (const [i, a] of o)
122
+ t[i] = structuredClone(a);
123
+ },
124
+ enumerable: !1,
125
+ configurable: !1,
126
+ writable: !1
127
+ }), t;
128
+ }
129
+ function O(n) {
130
+ let r, t = /* @__PURE__ */ new Map(), s, f = !1;
131
+ function l(u) {
132
+ for (const [e, o] of u)
133
+ if (e._version !== o) return !1;
134
+ return !0;
135
+ }
136
+ function c(u) {
137
+ for (const [e] of u)
138
+ v(e);
139
+ }
140
+ return (...u) => {
141
+ const e = JSON.stringify(u);
142
+ if (f && e === r && l(t))
143
+ return c(t), s;
144
+ b();
145
+ const o = n(...u), i = y();
146
+ t = /* @__PURE__ */ new Map();
147
+ for (const a of i)
148
+ t.set(a, a._version);
149
+ return r = e, s = o, f = !0, c(t), o;
150
+ };
151
+ }
152
+ function j(n) {
153
+ let r = /* @__PURE__ */ new Map(), t, s = !1;
154
+ function f(c) {
155
+ for (const [u, e] of c)
156
+ if (u._version !== e) return !1;
157
+ return !0;
158
+ }
159
+ function l(c) {
160
+ for (const [u] of c)
161
+ v(u);
162
+ }
163
+ return {
164
+ get value() {
165
+ if (s && f(r))
166
+ return l(r), t;
167
+ b();
168
+ const c = n(), u = y();
169
+ r = /* @__PURE__ */ new Map();
170
+ for (const e of u)
171
+ r.set(e, e._version);
172
+ return t = c, s = !0, l(r), c;
173
+ }
174
+ };
175
+ }
176
+ function A(n, r) {
177
+ let t = !0, s = [], f = [], l;
178
+ function c() {
179
+ if (!t) return;
180
+ if (r) {
181
+ const e = r();
182
+ if (l && P(l, e))
183
+ return;
184
+ l = e;
185
+ }
186
+ for (const e of f)
187
+ e();
188
+ f = [], b(), n();
189
+ const u = y();
190
+ s = Array.from(u);
191
+ for (const e of s) {
192
+ const o = e._subscribe(() => {
193
+ c();
194
+ });
195
+ f.push(o);
196
+ }
197
+ if (r) {
198
+ b(), r();
199
+ const e = y();
200
+ for (const o of Array.from(e))
201
+ if (!s.includes(o)) {
202
+ const i = o._subscribe(() => {
203
+ c();
204
+ });
205
+ f.push(i);
206
+ }
207
+ }
208
+ }
209
+ return c(), () => {
210
+ t = !1;
211
+ for (const u of f)
212
+ u();
213
+ f = [];
214
+ };
215
+ }
216
+ function P(n, r) {
217
+ if (n.length !== r.length) return !1;
218
+ for (let t = 0; t < n.length; t++)
219
+ if (n[t] !== r[t]) return !1;
220
+ return !0;
221
+ }
222
+ export {
223
+ M as _flush,
224
+ j as computed,
225
+ _ as create,
226
+ O as memo,
227
+ A as run
228
+ };
@@ -0,0 +1,7 @@
1
+ export type { Store } from "./types";
2
+ export type { Computed } from "./computed";
3
+ export { create } from "./create";
4
+ export { memo } from "./memo";
5
+ export { computed } from "./computed";
6
+ export { run } from "./run";
7
+ export { flush as _flush } from "./scheduler";
package/dist/memo.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function memo<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
@@ -0,0 +1,2 @@
1
+ import type { StoreInternal } from "./types";
2
+ export declare function getInternal(store: object): StoreInternal;
package/dist/run.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function run(cb: () => void, deps?: () => unknown[]): () => void;
@@ -0,0 +1,3 @@
1
+ export declare function scheduleNotify(store: object, prop: string): void;
2
+ export declare function scheduleNotifyAll(store: object): void;
3
+ export declare function flush(): void;
@@ -0,0 +1,4 @@
1
+ import type { Store } from "./types";
2
+ export declare function startTracking(): void;
3
+ export declare function stopTracking(): Set<Store<unknown>>;
4
+ export declare function trackStore(store: Store<unknown>): void;
@@ -0,0 +1,18 @@
1
+ export type Constructor<T> = new () => T;
2
+ export type Subscriber = {
3
+ callback: () => void;
4
+ prop?: string;
5
+ };
6
+ export type VersionEntry = [string, unknown][];
7
+ export type StoreInternal = {
8
+ subscribers: Subscriber[];
9
+ version: number;
10
+ dirtyProps: Set<string>;
11
+ };
12
+ export type Store<T> = T & {
13
+ readonly _version: number;
14
+ _subscribe: (callback: () => void, prop?: string) => () => void;
15
+ _reset: () => void;
16
+ _saveVersion: (label?: string) => void;
17
+ _loadVersion: (label?: string) => void;
18
+ };
@@ -0,0 +1 @@
1
+ export declare function getAllProps(obj: object): PropertyKey[];
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "immu-js",
3
+ "version": "0.1.0",
4
+ "description": "Independent immutable state library for JavaScript/TypeScript",
5
+ "type": "module",
6
+ "main": "./dist/immu-js.cjs",
7
+ "module": "./dist/immu-js.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/immu-js.js",
12
+ "require": "./dist/immu-js.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "vite build && tsc --emitDeclarationOnly",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
23
+ "prepublishOnly": "npm run build"
24
+ },
25
+ "keywords": [
26
+ "state",
27
+ "immutable",
28
+ "store",
29
+ "reactive",
30
+ "memo",
31
+ "typescript"
32
+ ],
33
+ "license": "MIT",
34
+ "devDependencies": {
35
+ "@vitest/coverage-v8": "^1.6.1",
36
+ "typescript": "^5.3.3",
37
+ "vite": "^5.0.12",
38
+ "vitest": "^1.2.2"
39
+ }
40
+ }