@potch/munifw 2.1.0 → 2.1.1
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/munifw.min.js +1 -1
- package/dist/munifw.min.js.gz +0 -0
- package/dist/ssr.min.js.gz +0 -0
- package/package.json +1 -1
- package/sizes.md +1 -1
- package/src/munifw.js +20 -11
- package/test/munifw.test.js +10 -0
package/dist/munifw.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t="value",e=t=>"object"==typeof t,n=(t,e)=>[...t].map(e);export const using=(t,e)=>
|
|
1
|
+
const t="value",e=t=>null!==t&&"object"==typeof t,n=(t,e)=>[...t].map(e);export const using=(t,e)=>{try{e()}finally{t()}};export const collect=(t,e,n=[])=>(using(t(t=>n.push(t)),e),n);export const event=(t=new Set)=>[(...e)=>n(t,t=>t(...e)),e=>(t.add(e),()=>t.delete(e))];const o=[];export const signal=(e,n=(t,e)=>t===e)=>{const[c,r]=event();return{set[t](t){n(e,t)||(e=t,c())},touch(){c()},get[t](){return o.at(-1)&&o.at(-1).add(this),e},peek:()=>e,watch:r}};export const[emitEffect,onEffect]=event();export const effect=(t,...e)=>{let c=0;o.push(new Set(e));const r=()=>{if(!c){c=1;try{t((...t)=>emitEffect(effect(...t)))}finally{c=0}}};let s;try{r(),s=n(o.at(-1),t=>t.watch(r))}finally{o.pop()}return()=>n(s,t=>t())};export const computed=(e,...n)=>{const o=signal();return o.teardown=effect(()=>o[t]=e(o[t]),...n),o};export const setProp=(n,o,c)=>{"ref"==o&&t in c?c[t]=n:e(c)&&t in c?emitEffect(effect(()=>setProp(n,o,c[t]))):"function"==typeof c||e(c)||o in n?n[o]&&e(n[o])?assign(n[o],c):n[o]=null===c?"":c:null!==c&&(0!=c||o.startsWith("data-")||o.startsWith("aria-"))?n.setAttribute(o,c):n.removeAttribute(o)};export const assign=(t,n)=>(n&&Object.entries(n).forEach(([n,o])=>{t.nodeType?setProp(t,n,o):e(t[n])&&e(o)?assign(t[n],o):t[n]=o}),t);export const dom=(t,n,...o)=>{let c;return"function"==typeof t?c=mount(()=>t(n,o)):(c=document.createElement(t),n&&e(n)&&!n.nodeType?assign(c,n):n&&o.unshift(n),c.append(...o.flat(1))),c};export const mount=(t,...e)=>{const n=[];let o;return emitEffect(effect(()=>{for(;n.length;)try{n.pop()()}catch(t){}collect(onEffect,()=>{const e=t()??document.createComment("");o&&o.replaceWith(e),o=e},n)},...e)),o};export const on=(t,...e)=>(t.addEventListener(...e),()=>t.removeEventListener(...e));
|
package/dist/munifw.min.js.gz
CHANGED
|
Binary file
|
package/dist/ssr.min.js.gz
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/sizes.md
CHANGED
package/src/munifw.js
CHANGED
|
@@ -2,10 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
// prop to monitor for changes
|
|
4
4
|
const val = "value";
|
|
5
|
-
const isObj = (o) => typeof o === "object";
|
|
5
|
+
const isObj = (o) => o !== null && typeof o === "object";
|
|
6
6
|
const map = (a, fn) => [...a].map(fn);
|
|
7
7
|
// utility to run a function and ensure cleanup after
|
|
8
|
-
export const using = (cb, fn) =>
|
|
8
|
+
export const using = (cb, fn) => {
|
|
9
|
+
try {
|
|
10
|
+
fn();
|
|
11
|
+
} finally {
|
|
12
|
+
cb();
|
|
13
|
+
}
|
|
14
|
+
};
|
|
9
15
|
// collect event teardowns within the scope of a function
|
|
10
16
|
export const collect = (onEvent, fn, collection = []) => (
|
|
11
17
|
using(
|
|
@@ -69,18 +75,21 @@ export const effect = (fn, ...explicitDependencies) => {
|
|
|
69
75
|
// prevents effects effecting themselves
|
|
70
76
|
if (!inUpdate) {
|
|
71
77
|
inUpdate = true;
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
try {
|
|
79
|
+
fn((...args) => emitEffect(effect(...args)));
|
|
80
|
+
} finally {
|
|
81
|
+
inUpdate = false;
|
|
82
|
+
}
|
|
74
83
|
}
|
|
75
84
|
};
|
|
76
85
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
dependency.watch(update)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
let teardown;
|
|
87
|
+
try {
|
|
88
|
+
update();
|
|
89
|
+
teardown = map(context.at(-1), (dependency) => dependency.watch(update));
|
|
90
|
+
} finally {
|
|
91
|
+
context.pop();
|
|
92
|
+
}
|
|
84
93
|
|
|
85
94
|
return () => map(teardown, (fn) => fn());
|
|
86
95
|
};
|
package/test/munifw.test.js
CHANGED
|
@@ -276,6 +276,16 @@ describe("fw", () => {
|
|
|
276
276
|
});
|
|
277
277
|
expect(fn).toHaveBeenCalled();
|
|
278
278
|
expect(cb).toHaveBeenCalled();
|
|
279
|
+
|
|
280
|
+
const throwCb = vi.fn();
|
|
281
|
+
|
|
282
|
+
expect(() =>
|
|
283
|
+
using(throwCb, () => {
|
|
284
|
+
throw "oops";
|
|
285
|
+
})
|
|
286
|
+
).toThrowError();
|
|
287
|
+
|
|
288
|
+
expect(throwCb).toHaveBeenCalled();
|
|
279
289
|
});
|
|
280
290
|
it("collect", () => {
|
|
281
291
|
const [emit, on] = event();
|