@esmj/signals 0.0.3 → 0.0.5
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/index.d.mts +20 -14
- package/dist/index.d.ts +20 -14
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -12,7 +12,7 @@ function untrack(callback) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const INTERNAL_OBSERVABLE = Symbol('internal observable');
|
|
15
|
-
const
|
|
15
|
+
const ORIGINAL_FUNCTION = Symbol('original function');
|
|
16
16
|
|
|
17
17
|
class Watcher extends Observable {
|
|
18
18
|
#pendings = new Set();
|
|
@@ -50,13 +50,13 @@ class Watcher extends Observable {
|
|
|
50
50
|
this.#notify();
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
|
-
signal.next[
|
|
53
|
+
signal.next[ORIGINAL_FUNCTION] = undefined;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// WATCH Computed
|
|
57
57
|
if (
|
|
58
58
|
signal instanceof Computed &&
|
|
59
|
-
signal.next[
|
|
59
|
+
signal.next[ORIGINAL_FUNCTION] === undefined
|
|
60
60
|
) {
|
|
61
61
|
const originalNext = signal.next.bind(signal);
|
|
62
62
|
signal.next = () => {
|
|
@@ -68,7 +68,7 @@ class Watcher extends Observable {
|
|
|
68
68
|
this.#notify();
|
|
69
69
|
});
|
|
70
70
|
};
|
|
71
|
-
signal.next[
|
|
71
|
+
signal.next[ORIGINAL_FUNCTION] = originalNext;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
return this.subscribe(signal);
|
|
@@ -76,14 +76,18 @@ class Watcher extends Observable {
|
|
|
76
76
|
|
|
77
77
|
getPending() {
|
|
78
78
|
const pendings = Array.from(this.#pendings).map((pending) => {
|
|
79
|
-
|
|
79
|
+
if (!pending.get[ORIGINAL_FUNCTION]) {
|
|
80
|
+
const originalGet = pending.get.bind(pending);
|
|
80
81
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
pending.get = () => {
|
|
83
|
+
return untrack(() => {
|
|
84
|
+
this.#pendings.delete(pending);
|
|
85
|
+
return originalGet();
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
pending.get[ORIGINAL_FUNCTION] = originalGet;
|
|
90
|
+
}
|
|
87
91
|
|
|
88
92
|
return pending;
|
|
89
93
|
});
|
|
@@ -92,7 +96,8 @@ class Watcher extends Observable {
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
unwatch(signal) {
|
|
95
|
-
signal.next = signal.next[
|
|
99
|
+
signal.next = signal.next[ORIGINAL_FUNCTION];
|
|
100
|
+
signal.get = signal.get[ORIGINAL_FUNCTION];
|
|
96
101
|
|
|
97
102
|
return this.unsubscribe(signal);
|
|
98
103
|
}
|
|
@@ -103,10 +108,11 @@ let w = null;
|
|
|
103
108
|
function createWatcher(notify) {
|
|
104
109
|
w = new Watcher(notify);
|
|
105
110
|
}
|
|
106
|
-
|
|
111
|
+
let timer = null;
|
|
107
112
|
createWatcher(() => {
|
|
108
113
|
// TODO performance improvement
|
|
109
|
-
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
timer = setTimeout(() => {
|
|
110
116
|
getPending().forEach((pending) => {
|
|
111
117
|
pending.get();
|
|
112
118
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ function untrack(callback) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const INTERNAL_OBSERVABLE = Symbol('internal observable');
|
|
15
|
-
const
|
|
15
|
+
const ORIGINAL_FUNCTION = Symbol('original function');
|
|
16
16
|
|
|
17
17
|
class Watcher extends Observable {
|
|
18
18
|
#pendings = new Set();
|
|
@@ -50,13 +50,13 @@ class Watcher extends Observable {
|
|
|
50
50
|
this.#notify();
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
|
-
signal.next[
|
|
53
|
+
signal.next[ORIGINAL_FUNCTION] = undefined;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// WATCH Computed
|
|
57
57
|
if (
|
|
58
58
|
signal instanceof Computed &&
|
|
59
|
-
signal.next[
|
|
59
|
+
signal.next[ORIGINAL_FUNCTION] === undefined
|
|
60
60
|
) {
|
|
61
61
|
const originalNext = signal.next.bind(signal);
|
|
62
62
|
signal.next = () => {
|
|
@@ -68,7 +68,7 @@ class Watcher extends Observable {
|
|
|
68
68
|
this.#notify();
|
|
69
69
|
});
|
|
70
70
|
};
|
|
71
|
-
signal.next[
|
|
71
|
+
signal.next[ORIGINAL_FUNCTION] = originalNext;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
return this.subscribe(signal);
|
|
@@ -76,14 +76,18 @@ class Watcher extends Observable {
|
|
|
76
76
|
|
|
77
77
|
getPending() {
|
|
78
78
|
const pendings = Array.from(this.#pendings).map((pending) => {
|
|
79
|
-
|
|
79
|
+
if (!pending.get[ORIGINAL_FUNCTION]) {
|
|
80
|
+
const originalGet = pending.get.bind(pending);
|
|
80
81
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
pending.get = () => {
|
|
83
|
+
return untrack(() => {
|
|
84
|
+
this.#pendings.delete(pending);
|
|
85
|
+
return originalGet();
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
pending.get[ORIGINAL_FUNCTION] = originalGet;
|
|
90
|
+
}
|
|
87
91
|
|
|
88
92
|
return pending;
|
|
89
93
|
});
|
|
@@ -92,7 +96,8 @@ class Watcher extends Observable {
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
unwatch(signal) {
|
|
95
|
-
signal.next = signal.next[
|
|
99
|
+
signal.next = signal.next[ORIGINAL_FUNCTION];
|
|
100
|
+
signal.get = signal.get[ORIGINAL_FUNCTION];
|
|
96
101
|
|
|
97
102
|
return this.unsubscribe(signal);
|
|
98
103
|
}
|
|
@@ -103,10 +108,11 @@ let w = null;
|
|
|
103
108
|
function createWatcher(notify) {
|
|
104
109
|
w = new Watcher(notify);
|
|
105
110
|
}
|
|
106
|
-
|
|
111
|
+
let timer = null;
|
|
107
112
|
createWatcher(() => {
|
|
108
113
|
// TODO performance improvement
|
|
109
|
-
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
timer = setTimeout(() => {
|
|
110
116
|
getPending().forEach((pending) => {
|
|
111
117
|
pending.get();
|
|
112
118
|
});
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var observable=require('@esmj/observable');var
|
|
1
|
+
'use strict';var observable=require('@esmj/observable');var i=null;function f(n){let t=i;i=null;let e=n();return i=t,e}var x=Symbol("internal observable"),c=Symbol("original function"),d=class extends observable.Observable{#t=new Set;#e=null;constructor(t){super(),this.#e=t,this.pipe(e=>{let r=e.subscribe.bind(e),h=e.unsubscribe.bind(e);return e.subscribe=s=>{r(s),this.#t.add(s);},e.unsubscribe=s=>{h(s),this.#t.delete(s);},e});}watch(t){if(typeof t.next!="function"&&(t.next=()=>f(()=>{this.#t.add(t),this.#e();}),t.next[c]=void 0),t instanceof u&&t.next[c]===void 0){let e=t.next.bind(t);t.next=()=>f(()=>{this.#t.add(t),e(),this.#e();}),t.next[c]=e;}return this.subscribe(t)}getPending(){return Array.from(this.#t).map(e=>{if(!e.get[c]){let r=e.get.bind(e);e.get=()=>f(()=>(this.#t.delete(e),r())),e.get[c]=r;}return e})}unwatch(t){return t.next=t.next[c],t.get=t.get[c],this.unsubscribe(t)}},o=null;function g(n){o=new d(n);}var a=null;g(()=>{clearTimeout(a),a=setTimeout(()=>{m().forEach(n=>{n.get();});},0);});function m(){return o.getPending()}function y(n){return o.watch(n)}function E(n){return o.unwatch(n)}var u=class extends observable.Observer{#t=!0;#e=null;#n=null;#r=this.#o();#i=null;#s=null;constructor(t,e){super(),this.#i=t,this.#s=e,this.debug=e?.debug,this.get=this.get.bind(this);}#u(){Array.from(this.#r.dependencies.values()).forEach(({unsubscribe:t})=>{t();}),this.#r.dependencies.clear();}#o(){return {dependencies:new Map,observer:this}}#h(){this.#e=i,i=this.#r;}#f(){i=this.#e;}next(){this.#t=!0,this.#n[x].next();}get(){return this.#n||(this.#n=w(this.#c(),this.#s)),this.#t&&this.#c(),this.#n.get()}#c(){this.#t=!1,this.#u(),this.#h();let t;try{t=this.#i();}catch(e){t=e;}if(this.#f(),t instanceof Promise&&(t=t.then(e=>e).catch(e=>{throw e})),this.#n&&this.#n.set(t),t instanceof Error)throw t;return t}};function N(n,t){return new u(n,t)}function O(n,t){let e,r=N(()=>{e?.(),e=n();},{equals:()=>!1,debug:"effect",...t});return r.get(),y(r),()=>{e?.(),E(r);}}function w(n,t={}){let e=t?.equals??Object.is,r=new observable.Observable;function h(){if(typeof i=="object"&&i!==null&&i.dependencies.set(r,r.subscribe(i.observer)),n instanceof Error)throw n;return n}function s(l){return e(n,l)||(n=l,r.next()),n}return {get:h,set:s,[x]:r}}var P=w;exports.computed=N;exports.createSignal=w;exports.createWatcher=g;exports.effect=O;exports.getPending=m;exports.state=P;exports.untrack=f;exports.unwatch=E;exports.watch=y;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {Observable,Observer}from'@esmj/observable';var
|
|
1
|
+
import {Observable,Observer}from'@esmj/observable';var i=null;function f(n){let t=i;i=null;let e=n();return i=t,e}var x=Symbol("internal observable"),c=Symbol("original function"),d=class extends Observable{#t=new Set;#e=null;constructor(t){super(),this.#e=t,this.pipe(e=>{let r=e.subscribe.bind(e),h=e.unsubscribe.bind(e);return e.subscribe=s=>{r(s),this.#t.add(s);},e.unsubscribe=s=>{h(s),this.#t.delete(s);},e});}watch(t){if(typeof t.next!="function"&&(t.next=()=>f(()=>{this.#t.add(t),this.#e();}),t.next[c]=void 0),t instanceof u&&t.next[c]===void 0){let e=t.next.bind(t);t.next=()=>f(()=>{this.#t.add(t),e(),this.#e();}),t.next[c]=e;}return this.subscribe(t)}getPending(){return Array.from(this.#t).map(e=>{if(!e.get[c]){let r=e.get.bind(e);e.get=()=>f(()=>(this.#t.delete(e),r())),e.get[c]=r;}return e})}unwatch(t){return t.next=t.next[c],t.get=t.get[c],this.unsubscribe(t)}},o=null;function g(n){o=new d(n);}var a=null;g(()=>{clearTimeout(a),a=setTimeout(()=>{m().forEach(n=>{n.get();});},0);});function m(){return o.getPending()}function y(n){return o.watch(n)}function E(n){return o.unwatch(n)}var u=class extends Observer{#t=!0;#e=null;#n=null;#r=this.#o();#i=null;#s=null;constructor(t,e){super(),this.#i=t,this.#s=e,this.debug=e?.debug,this.get=this.get.bind(this);}#u(){Array.from(this.#r.dependencies.values()).forEach(({unsubscribe:t})=>{t();}),this.#r.dependencies.clear();}#o(){return {dependencies:new Map,observer:this}}#h(){this.#e=i,i=this.#r;}#f(){i=this.#e;}next(){this.#t=!0,this.#n[x].next();}get(){return this.#n||(this.#n=w(this.#c(),this.#s)),this.#t&&this.#c(),this.#n.get()}#c(){this.#t=!1,this.#u(),this.#h();let t;try{t=this.#i();}catch(e){t=e;}if(this.#f(),t instanceof Promise&&(t=t.then(e=>e).catch(e=>{throw e})),this.#n&&this.#n.set(t),t instanceof Error)throw t;return t}};function N(n,t){return new u(n,t)}function O(n,t){let e,r=N(()=>{e?.(),e=n();},{equals:()=>!1,debug:"effect",...t});return r.get(),y(r),()=>{e?.(),E(r);}}function w(n,t={}){let e=t?.equals??Object.is,r=new Observable;function h(){if(typeof i=="object"&&i!==null&&i.dependencies.set(r,r.subscribe(i.observer)),n instanceof Error)throw n;return n}function s(l){return e(n,l)||(n=l,r.next()),n}return {get:h,set:s,[x]:r}}var P=w;export{N as computed,w as createSignal,g as createWatcher,O as effect,m as getPending,P as state,f as untrack,E as unwatch,y as watch};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/signals",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Tiny reactive signals.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"signals",
|
|
@@ -79,6 +79,6 @@
|
|
|
79
79
|
"tsup": "^8.3.5"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
-
"@esmj/observable": "^0.
|
|
82
|
+
"@esmj/observable": "^0.2.2"
|
|
83
83
|
}
|
|
84
84
|
}
|