flatsignals 0.3.0 → 0.4.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 +2 -2
- package/dist/index.mjs +9 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +72 -72
package/README.md
CHANGED
|
@@ -70,8 +70,8 @@ yarn add flatsignals
|
|
|
70
70
|
import { signal, computed, effect } from "flatsignals";
|
|
71
71
|
|
|
72
72
|
const counter = signal(0);
|
|
73
|
-
const double = computed(() => counter.
|
|
74
|
-
const log = effect(() => console.log(double.
|
|
73
|
+
const double = computed(() => counter.get() * 2);
|
|
74
|
+
const log = effect(() => console.log(double.get()));
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
## With React
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,6 @@ let ROOT = null, COMPUTED = null, BATCHING = false, ROOT_QUEUE = null;
|
|
|
4
4
|
var FlatRoot = class {
|
|
5
5
|
autoFlush;
|
|
6
6
|
_c = [];
|
|
7
|
-
_x = [];
|
|
8
7
|
_i = 0;
|
|
9
8
|
#batch = 0;
|
|
10
9
|
constructor(autoFlush = true) {
|
|
@@ -12,21 +11,20 @@ var FlatRoot = class {
|
|
|
12
11
|
}
|
|
13
12
|
dispose() {
|
|
14
13
|
this._c = [];
|
|
15
|
-
this._x = [];
|
|
16
14
|
this._i = 0;
|
|
17
15
|
}
|
|
18
16
|
_as() {
|
|
19
17
|
return this._i++ % 32;
|
|
20
18
|
}
|
|
21
19
|
_ac(c) {
|
|
22
|
-
|
|
23
|
-
const u = this._x.pop();
|
|
24
|
-
this._c[u] = c;
|
|
25
|
-
return u;
|
|
26
|
-
} else return this._c.push(c) - 1;
|
|
20
|
+
return this._c.push(c) - 1;
|
|
27
21
|
}
|
|
28
22
|
_dc(idx) {
|
|
29
|
-
this.
|
|
23
|
+
const last = this._c.pop();
|
|
24
|
+
if (idx !== this._c.length) {
|
|
25
|
+
this._c[idx] = last;
|
|
26
|
+
last._i = idx;
|
|
27
|
+
}
|
|
30
28
|
}
|
|
31
29
|
_queue(mask) {
|
|
32
30
|
if (!this.#batch) ROOT_QUEUE ??= this;
|
|
@@ -71,19 +69,19 @@ var FlatSignal = class {
|
|
|
71
69
|
};
|
|
72
70
|
var FlatCompute = class {
|
|
73
71
|
#root;
|
|
74
|
-
#id;
|
|
75
72
|
#val;
|
|
76
73
|
#fn;
|
|
77
74
|
_effect = false;
|
|
78
75
|
_sources = 0;
|
|
79
76
|
_dirty = true;
|
|
80
77
|
_d = false;
|
|
78
|
+
_i;
|
|
81
79
|
constructor(compute, val, effect) {
|
|
82
80
|
if (!ROOT) ROOT = new FlatRoot();
|
|
83
81
|
this.#root = ROOT;
|
|
84
82
|
this.#fn = compute;
|
|
85
83
|
this.#val = val;
|
|
86
|
-
this
|
|
84
|
+
this._i = this.#root._ac(this);
|
|
87
85
|
if (effect) {
|
|
88
86
|
this._effect = effect;
|
|
89
87
|
this.get();
|
|
@@ -112,7 +110,7 @@ var FlatCompute = class {
|
|
|
112
110
|
this._sources = 0;
|
|
113
111
|
this._dirty = false;
|
|
114
112
|
this._d = true;
|
|
115
|
-
this.#root._dc(this
|
|
113
|
+
this.#root._dc(this._i);
|
|
116
114
|
}
|
|
117
115
|
};
|
|
118
116
|
function defaultEquality(a, b) {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["#batch","#root","#val","#id","#fn"],"sources":["../src/index.ts"],"sourcesContent":["/** biome-ignore-all lint/style/noNonNullAssertion: guaranteed */\n\nlet ROOT: FlatRoot | null = null,\n\tCOMPUTED: FlatCompute | null = null,\n\tBATCHING = false,\n\tROOT_QUEUE: FlatRoot | null = null;\n\nexport class FlatRoot {\n\t/* @internal computeds */\n\t_c: Array<FlatCompute> = [];\n\t/* @internal
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#batch","#root","#val","#id","#fn"],"sources":["../src/index.ts"],"sourcesContent":["/** biome-ignore-all lint/style/noNonNullAssertion: guaranteed */\n\nlet ROOT: FlatRoot | null = null,\n\tCOMPUTED: FlatCompute | null = null,\n\tBATCHING = false,\n\tROOT_QUEUE: FlatRoot | null = null;\n\nexport class FlatRoot {\n\t/* @internal computeds */\n\t_c: Array<FlatCompute> = [];\n\t/* @internal id generator */\n\t_i = 0;\n\t/* @internal batch mask */\n\t#batch: number = 0;\n\n\tconstructor(public autoFlush = true) {}\n\n\tdispose() {\n\t\tthis._c = [];\n\t\tthis._i = 0;\n\t}\n\n\t/* @internal Add source */\n\t_as() {\n\t\treturn this._i++ % 32;\n\t}\n\n\t/* @internal Add computed */\n\t_ac(c: FlatCompute) {\n\t\treturn this._c.push(c) - 1;\n\t}\n\n\t/* @internal Destroy computed */\n\t_dc(idx: number) {\n\t\tconst last = this._c.pop()!;\n\t\tif (idx !== this._c.length) {\n\t\t\tthis._c[idx] = last;\n\t\t\tlast._i = idx;\n\t\t}\n\t}\n\n\t/* @internal */\n\t_queue(mask: number) {\n\t\tif (!this.#batch) {\n\t\t\tROOT_QUEUE ??= this;\n\t\t}\n\t\tthis.#batch |= mask;\n\t\tif (this.autoFlush && !BATCHING) this.flush();\n\t}\n\n\tflush() {\n\t\tif (!this.#batch) return;\n\t\tfor (const item of this._c) {\n\t\t\tif (!item._dirty && (item._sources & this.#batch) !== 0) {\n\t\t\t\titem._dirty = true;\n\t\t\t\tif (item._effect) item.get();\n\t\t\t}\n\t\t}\n\t\tthis.#batch = 0;\n\t}\n}\n\nexport class FlatSignal<T = undefined> {\n\t#root: FlatRoot;\n\t#val: T;\n\t#id = 0;\n\tequals = defaultEquality;\n\n\tconstructor(val?: T) {\n\t\tif (!ROOT) ROOT = new FlatRoot();\n\t\tthis.#root = ROOT;\n\t\tthis.#val = val as T;\n\t\tthis.#id |= 1 << this.#root._as();\n\t}\n\n\tget(): T {\n\t\tif (COMPUTED) {\n\t\t\tCOMPUTED._sources |= this.#id;\n\t\t}\n\t\treturn this.#val as T;\n\t}\n\n\tget peek() {\n\t\treturn this.#val;\n\t}\n\n\tget root() {\n\t\treturn this.#root;\n\t}\n\n\tset(val: T) {\n\t\tif (this.equals(val, this.#val)) return;\n\t\tthis.#val = val as T;\n\t\tthis.#root._queue(this.#id);\n\t}\n}\n\nexport class FlatCompute<T = unknown> {\n\t#root: FlatRoot;\n\t#val: T;\n\t#fn: (() => T) | undefined;\n\t/* @internal */\n\t_effect: boolean = false;\n\t/* @internal */\n\t_sources: number = 0;\n\t/* @internal */\n\t_dirty = true;\n\t/* @internal destroyed */\n\t_d = false;\n\t/* @internal index */\n\t_i: number;\n\n\tconstructor(compute?: () => T, val?: T, effect?: boolean) {\n\t\tif (!ROOT) ROOT = new FlatRoot();\n\t\tthis.#root = ROOT;\n\t\tthis.#fn = compute;\n\t\tthis.#val = val!;\n\t\tthis._i = this.#root._ac(this as FlatCompute<unknown>);\n\t\tif (effect) {\n\t\t\tthis._effect = effect;\n\t\t\tthis.get();\n\t\t}\n\t}\n\n\tget(): T {\n\t\tconst prevCurrent = COMPUTED;\n\t\tif (this._dirty) {\n\t\t\tCOMPUTED = this as FlatCompute<unknown>;\n\t\t\tthis._sources = 0;\n\t\t\tthis.#val = this.#fn!();\n\t\t\tthis._dirty = false;\n\t\t\tCOMPUTED = prevCurrent;\n\t\t}\n\t\tif (prevCurrent) {\n\t\t\tprevCurrent._sources |= this._sources;\n\t\t}\n\t\treturn this.#val!;\n\t}\n\n\tget peek() {\n\t\treturn this.#val;\n\t}\n\n\tget root() {\n\t\treturn this.#root;\n\t}\n\n\tdispose() {\n\t\tif (this._d) return;\n\t\tthis._sources = 0;\n\t\tthis._dirty = false;\n\t\tthis._d = true;\n\t\tthis.#root._dc(this._i);\n\t}\n}\n\nfunction defaultEquality(a: unknown, b: unknown) {\n\treturn a === b;\n}\n\nexport function batch(fn: () => void) {\n\tif (BATCHING) return fn();\n\tBATCHING = true;\n\tfn();\n\tif (ROOT_QUEUE?.autoFlush) ROOT_QUEUE?.flush();\n\tROOT_QUEUE = null;\n\tBATCHING = false;\n}\n\nexport function scoped<T>(fn: () => T, scope?: FlatRoot): T {\n\tconst prevRoot = ROOT;\n\tROOT = scope ?? new FlatRoot();\n\tconst result = fn();\n\tROOT = prevRoot;\n\treturn result;\n}\n\nexport function signal<T>(value: T): FlatSignal<T>;\nexport function signal<T = undefined>(): FlatSignal<T | undefined>;\nexport function signal<T>(value?: T): FlatSignal<T> {\n\treturn new FlatSignal(value);\n}\n\nexport function computed<T>(val: () => T): FlatCompute<T> {\n\treturn new FlatCompute(val);\n}\n\nexport function effect<T = unknown>(fn: () => T) {\n\tconst sig = new FlatCompute(fn, undefined, true);\n\treturn sig.dispose.bind(sig);\n}\n"],"mappings":";;AAEA,IAAI,OAAwB,MAC3B,WAA+B,MAC/B,WAAW,OACX,aAA8B;AAE/B,IAAa,WAAb,MAAsB;CAQF;CANnB,KAAyB,CAAC;CAE1B,KAAK;CAEL,SAAiB;CAEjB,YAAY,YAAmB,MAAM;EAAlB,KAAA,YAAA;CAAmB;CAEtC,UAAU;EACT,KAAK,KAAK,CAAC;EACX,KAAK,KAAK;CACX;CAGA,MAAM;EACL,OAAO,KAAK,OAAO;CACpB;CAGA,IAAI,GAAgB;EACnB,OAAO,KAAK,GAAG,KAAK,CAAC,IAAI;CAC1B;CAGA,IAAI,KAAa;EAChB,MAAM,OAAO,KAAK,GAAG,IAAI;EACzB,IAAI,QAAQ,KAAK,GAAG,QAAQ;GAC3B,KAAK,GAAG,OAAO;GACf,KAAK,KAAK;EACX;CACD;CAGA,OAAO,MAAc;EACpB,IAAI,CAAC,KAAKA,QACT,eAAe;EAEhB,KAAKA,UAAU;EACf,IAAI,KAAK,aAAa,CAAC,UAAU,KAAK,MAAM;CAC7C;CAEA,QAAQ;EACP,IAAI,CAAC,KAAKA,QAAQ;EAClB,KAAK,MAAM,QAAQ,KAAK,IACvB,IAAI,CAAC,KAAK,WAAW,KAAK,WAAW,KAAKA,YAAY,GAAG;GACxD,KAAK,SAAS;GACd,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
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
2
|
+
"name": "flatsignals",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "FlatSignals is an extremely fast reactivity library (~0.5kb)",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./react": {
|
|
17
|
+
"import": "./dist/react.mjs",
|
|
18
|
+
"types": "./dist/react.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsdown",
|
|
24
|
+
"dev": "tsdown --watch",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"bench": "vitest bench",
|
|
27
|
+
"test:watch": "vitest",
|
|
28
|
+
"coverage": "vitest run --coverage",
|
|
29
|
+
"size": "size-limit"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@angular/core": "^22.0.4",
|
|
34
|
+
"@biomejs/biome": "2.5.1",
|
|
35
|
+
"@maverick-js/signals": "^6.0.0",
|
|
36
|
+
"@preact/signals-core": "^1.14.3",
|
|
37
|
+
"@reactively/core": "^0.0.8",
|
|
38
|
+
"@size-limit/preset-small-lib": "^12.1.0",
|
|
39
|
+
"@solidjs/signals": "^0.13.13",
|
|
40
|
+
"@testing-library/dom": "^10.4.1",
|
|
41
|
+
"@testing-library/react": "^16.3.2",
|
|
42
|
+
"@types/node": "^26.0.1",
|
|
43
|
+
"@types/react": "^19.2.17",
|
|
44
|
+
"@types/react-dom": "^19.2.3",
|
|
45
|
+
"@vitest/coverage-v8": "^4.1.9",
|
|
46
|
+
"@vue/reactivity": "^3.5.39",
|
|
47
|
+
"alien-signals": "^3.2.1",
|
|
48
|
+
"jsdom": "^29.1.1",
|
|
49
|
+
"react": "^19.2.7",
|
|
50
|
+
"size-limit": "^12.1.0",
|
|
51
|
+
"solid-js": "^1.9.13",
|
|
52
|
+
"tsdown": "^0.22.3",
|
|
53
|
+
"typescript": "^6.0.3",
|
|
54
|
+
"vitest": "^4.1.9"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"react": "^19.2.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"react": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"size-limit": [
|
|
65
|
+
{
|
|
66
|
+
"path": "dist/index.mjs",
|
|
67
|
+
"limit": "1 kB"
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"vite": "^8.1.2"
|
|
72
|
+
}
|
|
73
|
+
}
|