@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 CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![Coverage Status](https://coveralls.io/repos/github/WebReflection/signals/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/signals?branch=main)
6
6
 
7
- A minimalistic [Preact-like signals](https://preactjs.com/guide/v10/signals/) (*minus options*) implementation.
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 w/out options
17
- effect, // Preact-like API w/out options
18
- signal, // Preact-like API w/out options
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 12 LOC, based on the core library mentioned in the previous points.
82
- * [branded](https://github.com/WebReflection/signals/blob/main/src/branded.js) is 21 LOC extra needed only for libraries building on top.
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
+ ![benchmark](/WebReflection/usignal/raw/main/test/benchmark.png)
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,d=t=>{l=t},i=()=>l,n=null,f=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,g=[],p=!1,M=t=>{let e=!p;e&&(p=!0);try{return t()}finally{if(e){p=!1;for(let[r,o]of g.splice(0))u.has(r)||o()}}},h=t=>{let e=c.get(t);if(e.length)for(let o of e.splice(0))x(o)},x=t=>{u.add(t),h(t),c.delete(t)},k=t=>{let e=()=>{o||u.has(e)||(o=!0,n||(p?g.push([e,r]):r()))},r=()=>{for(;o;)if(o=!1,h(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?.(),x(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 f(e),b()},peek(){return b()}}};var y=t=>{let e=new Set;return{get value(){return f(e),t},set value(r){if(t=r,i()){let o=e;e=new Set;for(let s of o)s()}},peek(){return t}}};var B=t=>{let e=i();d(!1);try{return t()}finally{d(e)}};var{defineProperty:T}=Object,{dispose:W}=Symbol,J=t=>function(...r){let o,s=k(()=>{o??=t.apply(this,r)??this});return T(o,W,{value:s})};var m=new WeakSet,R=t=>{let e=w(t);return m.add(e),e},V=t=>m.has(t),X=t=>{let e=y(t);return m.add(e),e};export{M as batch,R as computed,J as disposable,k as effect,V as isSignal,X as signal,B as untracked};
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};
@@ -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,d=[],l=!1,W=t=>{let e=!l;e&&(l=!0);try{return t()}finally{if(e){l=!1;for(let[r,o]of d.splice(0))a.has(r)||o()}}},m=t=>{let e=c.get(t);if(e.length)for(let o of e.splice(0))g(o)},g=t=>{a.add(t),m(t),c.delete(t)},x=t=>{let e=()=>{o||a.has(e)||(o=!0,n||(l?d.push([e,r]):r()))},r=()=>{for(;o;)if(o=!1,m(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 O=t=>{let e=new Set,r=!0,o,s=()=>{if(r)return;r=!0;let k=e;e=new Set;for(let y of k)y()},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{defineProperty:w}=Object,{dispose:v}=Symbol,J=t=>function(...r){let o,s=x(()=>{o??=t.apply(this,r)??this});return w(o,v,{value:s})};export{W as batch,O as computed,J as disposable,x as effect,q as signal,B as untracked};
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webreflection/signals",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A minimalistic Preact-like signals implementation",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
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
- export const computed = callback => {
10
- const computed = $computed(callback);
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
- export const signal = current => {
18
- const signal = $signal(current);
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 { defineProperty } = Object;
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 ??= callback.apply(this, args) ?? this;
6
+ ref ??= fn.apply(this, args) ?? this;
10
7
  });
11
- return defineProperty(ref, dispose, { value });
8
+ ref[Symbol.dispose] = value;
9
+ return ref;
12
10
  };
@@ -1,11 +1,15 @@
1
- export * from "./effect.js";
2
1
  export * from "./disposable.js";
3
- export function computed(callback: any): {
4
- readonly value: any;
5
- peek: () => any;
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
- export function isSignal(value: any): boolean;
8
- export function signal(current: any): {
9
- value: any;
10
- peek: () => any;
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
  };
@@ -1,2 +1,2 @@
1
1
  export * from "./index.js";
2
- export function disposable(callback: any): (...args: any[]) => undefined;
2
+ export function disposable(fn: any): (...args: any[]) => undefined;