@webreflection/signals 0.1.1 → 0.1.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/README.md +16 -6
- package/dist/branded.js +1 -1
- package/dist/disposable.js +1 -1
- package/package.json +1 -1
- package/src/branded.js +9 -5
- package/src/disposable.js +4 -6
- package/types/branded.d.ts +12 -8
- package/types/disposable.d.ts +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://coveralls.io/github/WebReflection/signals?branch=main)
|
|
6
6
|
|
|
7
|
-
A minimalistic [Preact-like signals](https://
|
|
7
|
+
A minimalistic [Preact-like signals](https://github.com/preactjs/signals/blob/main/packages/core/README.md) implementation.
|
|
8
8
|
|
|
9
9
|
Once minified and compressed, this module is actually [0.5KB](https://cdn.jsdelivr.net/npm/@webreflection/signals/dist/signals.js).
|
|
10
10
|
|
|
@@ -13,9 +13,9 @@ Once minified and compressed, this module is actually [0.5KB](https://cdn.jsdeli
|
|
|
13
13
|
// basic core features
|
|
14
14
|
import {
|
|
15
15
|
batch, // Preact-like API
|
|
16
|
-
computed, // Preact-like API
|
|
17
|
-
effect, // Preact-like API
|
|
18
|
-
signal, // Preact-like API
|
|
16
|
+
computed, // Preact-like API
|
|
17
|
+
effect, // Preact-like API
|
|
18
|
+
signal, // Preact-like API
|
|
19
19
|
untracked, // Preact-like API
|
|
20
20
|
} from '@webreflection/signals';
|
|
21
21
|
```
|
|
@@ -72,13 +72,23 @@ import {
|
|
|
72
72
|
|
|
73
73
|
You know, nowadays it's hard to find libraries that are still 100% under control, minimalistic, not bloated, yet correct, and this one would like to be one of those 😇
|
|
74
74
|
|
|
75
|
+
|
|
75
76
|
#### The Beauty
|
|
76
77
|
|
|
77
78
|
* [signal](https://github.com/WebReflection/signals/blob/main/src/signal.js) is 26 LOC.
|
|
78
79
|
* [computed](https://github.com/WebReflection/signals/blob/main/src/computed.js) is 33 LOC.
|
|
79
80
|
* the shared [stack](https://github.com/WebReflection/signals/blob/main/src/stack.js) is 20 LOC.
|
|
80
81
|
* [effect](https://github.com/WebReflection/signals/blob/main/src/effect.js) is where business happens, 75 LOC.
|
|
81
|
-
* [disposable](https://github.com/WebReflection/signals/blob/main/src/disposable.js) is
|
|
82
|
-
* [branded](https://github.com/WebReflection/signals/blob/main/src/branded.js) is
|
|
82
|
+
* [disposable](https://github.com/WebReflection/signals/blob/main/src/disposable.js) is 10 LOC, based on the core library mentioned in the previous points.
|
|
83
|
+
* [branded](https://github.com/WebReflection/signals/blob/main/src/branded.js) is 25 LOC extra needed only for libraries building on top.
|
|
83
84
|
|
|
84
85
|
I mean ... that's coding, isn't it ... today I really needed something that would remind me why I love what I do ❤️
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
#### Benchmark
|
|
89
|
+
|
|
90
|
+

|
|
91
|
+
|
|
92
|
+
There is a *huge* difference between *NodeJS* and *Bun* but that's likely because *JSC* handles *Set* or *Map* in a better way, meaning all *WebKit* based browsers and mobile devices will have similar *Preact* performance, while *Chromium* based browsers will have half Preact size, but 1.5X slowdown.
|
|
93
|
+
|
|
94
|
+
However, in common scenarios with no more than 10 to 100 signals per *effect*, the performance are consistently better or really close to Preact.
|
package/dist/branded.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var l=!0,
|
|
1
|
+
var l=!0,m=t=>{l=t},f=()=>l,n=null,i=t=>{l&&n&&t.add(n)},a=(t,e)=>{let r=n;n=t;try{return e()}finally{n=r}};var u=new WeakSet,c=new WeakMap,x=[],p=!1,$=t=>{let e=!p;e&&(p=!0);try{return t()}finally{if(e){p=!1;for(let[r,o]of x.splice(0))u.has(r)||o()}}},g=t=>{let e=c.get(t);if(e.length)for(let o of e.splice(0))h(o)},h=t=>{u.add(t),g(t),c.delete(t)},k=t=>{let e=()=>{o||u.has(e)||(o=!0,n||(p?x.push([e,r]):r()))},r=()=>{for(;o;)if(o=!1,g(e),s?.(),s=a(e,t),u.has(e))return},o=!0,s;return n&&c.get(n).push(e),c.set(e,[]),r(),()=>{c.has(e)&&(s?.(),h(e))}};var w=t=>{let e=new Set,r=!0,o,s=()=>{if(r)return;r=!0;let S=e;e=new Set;for(let v of S)v()},b=()=>{for(;r;)r=!1,o=a(s,t);return o};return{get value(){return i(e),b()},peek(){return b()}}};var y=t=>{let e=new Set;return{get value(){return i(e),t},set value(r){if(t=r,f()){let o=e;e=new Set;for(let s of o)s()}},peek(){return t}}};var B=t=>{let e=f();m(!1);try{return t()}finally{m(e)}};var J=t=>function(...r){let o,s=k(()=>{o??=t.apply(this,r)??this});return o[Symbol.dispose]=s,o};var d=new WeakSet,P=t=>{let e=w(t);return d.add(e),e},Q=t=>d.has(t),R=t=>{let e=y(t);return d.add(e),e};export{$ as batch,P as computed,J as disposable,k as effect,Q as isSignal,R as signal,B as untracked};
|
package/dist/disposable.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var p=!0,b=t=>{p=t},f=()=>p,n=null,i=t=>{p&&n&&t.add(n)},u=(t,e)=>{let r=n;n=t;try{return e()}finally{n=r}};var a=new WeakSet,c=new WeakMap,
|
|
1
|
+
var p=!0,b=t=>{p=t},f=()=>p,n=null,i=t=>{p&&n&&t.add(n)},u=(t,e)=>{let r=n;n=t;try{return e()}finally{n=r}};var a=new WeakSet,c=new WeakMap,m=[],l=!1,S=t=>{let e=!l;e&&(l=!0);try{return t()}finally{if(e){l=!1;for(let[r,o]of m.splice(0))a.has(r)||o()}}},d=t=>{let e=c.get(t);if(e.length)for(let o of e.splice(0))g(o)},g=t=>{a.add(t),d(t),c.delete(t)},x=t=>{let e=()=>{o||a.has(e)||(o=!0,n||(l?m.push([e,r]):r()))},r=()=>{for(;o;)if(o=!1,d(e),s?.(),s=u(e,t),a.has(e))return},o=!0,s;return n&&c.get(n).push(e),c.set(e,[]),r(),()=>{c.has(e)&&(s?.(),g(e))}};var M=t=>{let e=new Set,r=!0,o,s=()=>{if(r)return;r=!0;let k=e;e=new Set;for(let w of k)w()},h=()=>{for(;r;)r=!1,o=u(s,t);return o};return{get value(){return i(e),h()},peek(){return h()}}};var q=t=>{let e=new Set;return{get value(){return i(e),t},set value(r){if(t=r,f()){let o=e;e=new Set;for(let s of o)s()}},peek(){return t}}};var B=t=>{let e=f();b(!1);try{return t()}finally{b(e)}};var J=t=>function(...r){let o,s=x(()=>{o??=t.apply(this,r)??this});return o[Symbol.dispose]=s,o};export{S as batch,M as computed,J as disposable,x as effect,q as signal,B as untracked};
|
package/package.json
CHANGED
package/src/branded.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
export * from './effect.js';
|
|
2
1
|
export * from './disposable.js';
|
|
2
|
+
export * from './effect.js';
|
|
3
|
+
export * from './untracked.js';
|
|
3
4
|
|
|
4
5
|
import { computed as $computed } from './computed.js';
|
|
5
6
|
import { signal as $signal } from './signal.js';
|
|
6
7
|
|
|
7
8
|
const branded = new WeakSet;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/** @type {<T>(fn: () => T) => { readonly value: T, peek: () => T }} */
|
|
11
|
+
export const computed = fn => {
|
|
12
|
+
const computed = $computed(fn);
|
|
11
13
|
branded.add(computed);
|
|
12
14
|
return computed;
|
|
13
15
|
};
|
|
14
16
|
|
|
17
|
+
/** @type {(value: unknown) => boolean} */
|
|
15
18
|
export const isSignal = value => branded.has(value);
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
/** @type {<T>(init: T) => { value: T, peek: () => T }} */
|
|
21
|
+
export const signal = init => {
|
|
22
|
+
const signal = $signal(init);
|
|
19
23
|
branded.add(signal);
|
|
20
24
|
return signal;
|
|
21
25
|
};
|
package/src/disposable.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
export * from './index.js';
|
|
2
2
|
import { effect } from './effect.js';
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const { dispose } = Symbol;
|
|
6
|
-
|
|
7
|
-
export const disposable = callback => function disposable(...args) {
|
|
4
|
+
export const disposable = fn => function disposable(...args) {
|
|
8
5
|
let ref, value = effect(() => {
|
|
9
|
-
ref ??=
|
|
6
|
+
ref ??= fn.apply(this, args) ?? this;
|
|
10
7
|
});
|
|
11
|
-
|
|
8
|
+
ref[Symbol.dispose] = value;
|
|
9
|
+
return ref;
|
|
12
10
|
};
|
package/types/branded.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
export * from "./effect.js";
|
|
2
1
|
export * from "./disposable.js";
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
export * from "./effect.js";
|
|
3
|
+
export * from "./untracked.js";
|
|
4
|
+
/** @type {<T>(fn: () => T) => { readonly value: T, peek: () => T }} */
|
|
5
|
+
export const computed: <T>(fn: () => T) => {
|
|
6
|
+
readonly value: T;
|
|
7
|
+
peek: () => T;
|
|
6
8
|
};
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/** @type {(value: unknown) => boolean} */
|
|
10
|
+
export const isSignal: (value: unknown) => boolean;
|
|
11
|
+
/** @type {<T>(init: T) => { value: T, peek: () => T }} */
|
|
12
|
+
export const signal: <T>(init: T) => {
|
|
13
|
+
value: T;
|
|
14
|
+
peek: () => T;
|
|
11
15
|
};
|
package/types/disposable.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from "./index.js";
|
|
2
|
-
export function disposable(
|
|
2
|
+
export function disposable(fn: any): (...args: any[]) => undefined;
|