@hoosk/minisignals 1.0.3
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/computed.d.ts +17 -0
- package/dist/computed.d.ts.map +1 -0
- package/dist/computed.js +44 -0
- package/dist/effect.d.ts +14 -0
- package/dist/effect.d.ts.map +1 -0
- package/dist/effect.js +30 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/signal.d.ts +10 -0
- package/dist/signal.d.ts.map +1 -0
- package/dist/signal.js +25 -0
- package/package.json +19 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface Computed<T> {
|
|
2
|
+
readonly value: T;
|
|
3
|
+
/**
|
|
4
|
+
* Stops the internal effect from observing dependencies.
|
|
5
|
+
* Call this when the computed signal is no longer needed to prevent memory leaks.
|
|
6
|
+
*/
|
|
7
|
+
dispose: () => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates a computed signal that derives its value from other signals.
|
|
11
|
+
* It only calculates and subscribes when its value is read for the first time.
|
|
12
|
+
*
|
|
13
|
+
* @param fn The function to compute the value.
|
|
14
|
+
* @returns A read-only signal with a dispose method.
|
|
15
|
+
*/
|
|
16
|
+
export declare function computed<T>(fn: () => T): Computed<T>;
|
|
17
|
+
//# sourceMappingURL=computed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB;;;OAGG;IACH,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAmCpD"}
|
package/dist/computed.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { signal } from './signal';
|
|
2
|
+
import { effect } from './effect';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a computed signal that derives its value from other signals.
|
|
5
|
+
* It only calculates and subscribes when its value is read for the first time.
|
|
6
|
+
*
|
|
7
|
+
* @param fn The function to compute the value.
|
|
8
|
+
* @returns A read-only signal with a dispose method.
|
|
9
|
+
*/
|
|
10
|
+
export function computed(fn) {
|
|
11
|
+
let initialized = false;
|
|
12
|
+
let internalSignal;
|
|
13
|
+
let unsubscribe = null;
|
|
14
|
+
let isDisposed = false;
|
|
15
|
+
return {
|
|
16
|
+
get value() {
|
|
17
|
+
if (isDisposed) {
|
|
18
|
+
if (!initialized)
|
|
19
|
+
return fn();
|
|
20
|
+
return internalSignal.value;
|
|
21
|
+
}
|
|
22
|
+
if (!initialized) {
|
|
23
|
+
unsubscribe = effect(() => {
|
|
24
|
+
const newValue = fn();
|
|
25
|
+
if (!initialized) {
|
|
26
|
+
internalSignal = signal(newValue);
|
|
27
|
+
initialized = true;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
internalSignal.value = newValue;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return internalSignal.value;
|
|
35
|
+
},
|
|
36
|
+
dispose() {
|
|
37
|
+
isDisposed = true;
|
|
38
|
+
if (unsubscribe) {
|
|
39
|
+
unsubscribe();
|
|
40
|
+
unsubscribe = null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
package/dist/effect.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type SubscriberSet = Set<() => void>;
|
|
2
|
+
interface ActiveEffect {
|
|
3
|
+
run: () => void;
|
|
4
|
+
deps: Set<SubscriberSet>;
|
|
5
|
+
}
|
|
6
|
+
export declare let activeEffect: ActiveEffect | null;
|
|
7
|
+
/**
|
|
8
|
+
* Creates an effect that runs immediately and tracks dependencies.
|
|
9
|
+
* @param fn The function to execute.
|
|
10
|
+
* @returns An unsubscribe function.
|
|
11
|
+
*/
|
|
12
|
+
export declare function effect(fn: () => void): () => void;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=effect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC;AAE5C,UAAU,YAAY;IACpB,GAAG,EAAE,MAAM,IAAI,CAAC;IAChB,IAAI,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;CAC1B;AAED,eAAO,IAAI,YAAY,EAAE,YAAY,GAAG,IAAW,CAAC;AAEpD;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAyBjD"}
|
package/dist/effect.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export let activeEffect = null;
|
|
2
|
+
/**
|
|
3
|
+
* Creates an effect that runs immediately and tracks dependencies.
|
|
4
|
+
* @param fn The function to execute.
|
|
5
|
+
* @returns An unsubscribe function.
|
|
6
|
+
*/
|
|
7
|
+
export function effect(fn) {
|
|
8
|
+
const effectObj = {
|
|
9
|
+
run() {
|
|
10
|
+
cleanup(effectObj);
|
|
11
|
+
const previousEffect = activeEffect;
|
|
12
|
+
activeEffect = effectObj;
|
|
13
|
+
try {
|
|
14
|
+
fn();
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
activeEffect = previousEffect;
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
deps: new Set()
|
|
21
|
+
};
|
|
22
|
+
function cleanup(eff) {
|
|
23
|
+
for (const depSet of eff.deps) {
|
|
24
|
+
depSet.delete(eff.run);
|
|
25
|
+
}
|
|
26
|
+
eff.deps.clear();
|
|
27
|
+
}
|
|
28
|
+
effectObj.run();
|
|
29
|
+
return () => cleanup(effectObj);
|
|
30
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
package/dist/signal.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Signal<T> {
|
|
2
|
+
value: T;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Creates a reactive signal.
|
|
6
|
+
* @param initialValue The starting value.
|
|
7
|
+
* @returns An object with a reactive .value property.
|
|
8
|
+
*/
|
|
9
|
+
export declare function signal<T>(initialValue: T): Signal<T>;
|
|
10
|
+
//# sourceMappingURL=signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,MAAM,CAAC,CAAC;IACvB,KAAK,EAAE,CAAC,CAAC;CACV;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAmBpD"}
|
package/dist/signal.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { activeEffect } from './effect';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a reactive signal.
|
|
4
|
+
* @param initialValue The starting value.
|
|
5
|
+
* @returns An object with a reactive .value property.
|
|
6
|
+
*/
|
|
7
|
+
export function signal(initialValue) {
|
|
8
|
+
const subscribers = new Set();
|
|
9
|
+
let _value = initialValue;
|
|
10
|
+
return {
|
|
11
|
+
get value() {
|
|
12
|
+
if (activeEffect) {
|
|
13
|
+
subscribers.add(activeEffect.run);
|
|
14
|
+
activeEffect.deps.add(subscribers);
|
|
15
|
+
}
|
|
16
|
+
return _value;
|
|
17
|
+
},
|
|
18
|
+
set value(newValue) {
|
|
19
|
+
if (_value !== newValue) {
|
|
20
|
+
_value = newValue;
|
|
21
|
+
[...subscribers].forEach(sub => sub());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hoosk/minisignals",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Minimal agnostic signals library",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"test": "vitest run"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "Hoosk",
|
|
18
|
+
"license": "MIT"
|
|
19
|
+
}
|