From 420defe4a2ee03029a7e12ace31b9d79896a65be Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Fri, 24 Nov 2023 21:07:48 +0100 Subject: [PATCH] :bug: renamig issues --- dist/dde-with-observables.js | 36 ++++++------- dist/esm-with-observables.js | 8 +-- src/observables-lib.js | 98 ++++++++++++++++++------------------ 3 files changed, 71 insertions(+), 71 deletions(-) diff --git a/dist/dde-with-observables.js b/dist/dde-with-observables.js index bad5529..fab17cf 100644 --- a/dist/dde-with-observables.js +++ b/dist/dde-with-observables.js @@ -1,27 +1,27 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var R={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(R,t):(Object.setPrototypeOf(t,R),t)}function N(t){return R.isPrototypeOf(t)&&t!==R?t:R}function x(t){return typeof t>"u"}function H(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function P(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var F={setDeleteAttr:et,ssr:""};function et(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var v=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function I(...t){return this.appendOriginal(...t),this}function nt(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var L;function y(t,e,...n){let r=N(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,b.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(m=>m(s)),void 0):s}),c=t(e||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:t.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case t==="#text":c=j.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=j.call(this,document.createDocumentFragment(),e);break;case!!L:c=j.call(this,document.createElementNS(L,t),e);break;case!c:c=j.call(this,document.createElement(t),e)}return nt(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function Et(t){let e=Symbol.for("default"),n=Array.from(t.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||e,c)&&o,{}),r=Reflect.has(n,e);return t.append=new Proxy(t.append,{apply(o,c,s){if(!s.length)return t;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,m=n[l];w(u,"remove","slot"),m&&(m.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[e].replaceWith(d),Reflect.deleteProperty(n,e)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),t}}),t}y.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(``);return e||(r.end=document.createComment("")),r};function xt(t){let e=this;return function(...r){L=t;let o=y.call(e,...r);return L=void 0,o}}var{setDeleteAttr:J}=F,D=new WeakMap;function j(t,...e){if(!e.length)return t;D.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return D.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(d,u)=>G.call(c,t,d,u));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return z(o,n,Z.bind(null,t[e]));case"ariaset":return z(o,n,(d,u)=>r("aria-"+d,u));case"classList":return rt.call(c,t,n)}return ot(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(D.has(t))return D.get(t);let r=(t instanceof SVGElement?st:ct).bind(null,t,"Attribute"),o=N(e);return{setRemoveAttr:r,s:o}}function rt(t,e){let n=N(this);return z(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Ot(t){return Array.from(t.children).forEach(e=>e.remove()),t}function w(t,e,n,r){return t instanceof HTMLElement?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function ot(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!x(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function z(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function ct(t,e,n,r){return t[(x(r)?"remove":"set")+e](n,Q(r))}function st(t,e,n,r,o=null){return t[(x(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function At(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=ft(),it=new WeakSet;_.connected=function(t,e){let{custom_element:n}=b.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(P(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};_.disconnected=function(t,e){let{custom_element:n}=b.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||P(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var q=new WeakMap;_.disconnectedAsAbort=function(t){if(q.has(t))return q.get(t);let e=new AbortController;return q.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||it.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:m}of u)m.dispatchEvent(new CustomEvent(c,{detail:[l,m.getAttribute(l)]}))});return P(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ft(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}m(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let a=t.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let a=t.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){t.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of t.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!t.has(g))continue;let A=t.get(g);A.length_c&&(g.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(g),a=!0)}return a}function m(i,f){let a=!1;for(let g of i)f&&u(g).then(m),!(!t.has(g)||!t.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),t.delete(g),a=!0);return a}}var p=Symbol.for("observable");function M(t){try{return Reflect.has(t,p)}catch{return!1}}var T=[],h=new WeakMap;function E(t,e){if(typeof t!="function")return X(t,e);if(M(t))return t;let n=X(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),T.push(r),n(t()),T.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||C(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[p],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{observable:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));B(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};E.symbols={observable:p,onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,p)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var W="__dde_reactive";E.el=function(t,e){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return C(t,s);b.push(c);let u=e(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return B(t,s),tt(t,s,n,e),s(t()),o};var S="__dde_attributes";E.attribute=function(t,e=null){let n=O(e),r;return b.host(o=>{if(r=o,w(r,"has",t)?n(w(r,"get",t)):e!==null&&w(r,"set",t,e),o[S]){o[S][t]=n;return}r[S]={[t]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[S][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/O.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return w(r,"set",t,d)}})};var Y={isObservable:M,processReactiveAttribute(t,e,n,r){if(!M(n))return n;let o=c=>r(e,c);return B(n,o),tt(n,o,t,e),n()}};function tt(t,e,...n){let{current:r}=b;r.prevent||r.host(function(o){o[W]||(o[W]=[],_.disconnected(()=>o[W].forEach(([[c,s]])=>C(c,s,c[p]?.host()===o)))(o)),o[W].push([[t,e],...n])})}function X(t,e){let n=(...r)=>r.length?pt(n,...r):lt(n);return at(n,t,e)}var ut=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),U=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function at(t,e,n){let r=[];H(n)!=="[object Object]"&&(n={});let{onclear:o}=O.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(t,p,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new U},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[p],ut),t}function dt(){return T[T.length-1]}function lt(t){if(!t[p])return;let{value:e,listeners:n}=t[p],r=dt();return r&&n.add(r),h.has(r)&&h.get(r).add(t),e}function pt(t,e,n){if(!t[p])return;let r=t[p];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function B(t,e){if(t[p])return t[p].listeners.add(e)}function C(t,e,n){let r=t[p];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(t.clear(t),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>C(s,c,!0))}return o}$(Y); -globalThis.dde= {O: E, -assign: j, -assignAttribute: G, -chainableAppend: nt, -classListDeclarative: rt, +var A={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function T(e,t=!0){return t?Object.assign(A,e):(Object.setPrototypeOf(e,A),e)}function C(e){return A.isPrototypeOf(e)&&e!==A?e:A}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function N(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:ee,ssr:""};function ee(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var E=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...E]},push(e={}){return E.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function H(...e){return this.appendOriginal(...e),this}function te(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var j;function y(e,t,...n){let r=C(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,b.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(v=>v(s)),void 0):s}),c=e(t||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:e.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case e==="#text":c=P.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=P.call(this,document.createDocumentFragment(),t);break;case!!j:c=P.call(this,document.createElementNS(j,e),t);break;case!c:c=P.call(this,document.createElement(e),t)}return te(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function _e(e){let t=Symbol.for("default"),n=Array.from(e.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||t,c)&&o,{}),r=Reflect.has(n,t);return e.append=new Proxy(e.append,{apply(o,c,s){if(!s.length)return e;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,v=n[l];O(u,"remove","slot"),v&&(v.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[t].replaceWith(d),Reflect.deleteProperty(n,t)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),e}}),e}y.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function Ee(e){let t=this;return function(...r){j=e;let o=y.call(t,...r);return j=void 0,o}}var{setDeleteAttr:I}=$,L=new WeakMap;function P(e,...t){if(!t.length)return e;L.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return L.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(d,u)=>Z.call(c,e,d,u));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(d,u)=>r("aria-"+d,u));case"classList":return ne.call(c,e,n)}return re(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(L.has(e))return L.get(e);let r=(e instanceof SVGElement?ce:oe).bind(null,e,"Attribute"),o=C(t);return{setRemoveAttr:r,s:o}}function ne(e,t){let n=C(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function xe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function O(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function re(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function oe(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ce(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function we(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function _(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ie(),se=new WeakSet;_.connected=function(e,t){let{custom_element:n}=b.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(N(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};_.disconnected=function(e,t){let{custom_element:n}=b.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||N(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;_.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(_.disconnected(()=>t.abort())),t};_.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||se.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:v}of u)v.dispatchEvent(new CustomEvent(c,{detail:[l,v.getAttribute(l)]}))});return N(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ie(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}v(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){e.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!e.has(g))continue;let w=e.get(g);w.length_c&&(g.dispatchEvent(new Event("dde:connected")),w.connected=new WeakSet,w.length_c=0,w.length_d||e.delete(g),a=!0)}return a}function v(i,f){let a=!1;for(let g of i)f&&u(g).then(v),!(!e.has(g)||!e.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),e.delete(g),a=!0);return a}}var p=Symbol.for("observable");function W(e){try{return Reflect.has(e,p)}catch{return!1}}var M=[],h=new WeakMap;function m(e,t){if(typeof e!="function")return Q(e,t);if(W(e))return e;let n=Q(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),M.push(r),n(e()),M.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||S(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[p],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>S(t,n))}};m.symbols={observable:p,onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,p)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return S(e,s);b.push(c);let u=t(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return U(e,s),Y(e,s,n,t),s(e()),o};var R="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return b.host(o=>{if(r=o,O(r,"has",e)?n(O(r,"get",e)):t!==null&&O(r,"set",e,t),o[R]){o[R][e]=n;return}r[R]={[e]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[R][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[R]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return O(r,"set",e,d)}})};var X={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),Y(n,o,e,t),n()}};function Y(e,t,...n){let{current:r}=b;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>S(c,s,c[p]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function Q(e,t){let n=(...r)=>r.length?le(n,...r):de(n);return ue(n,e,t)}var fe=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` +`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function ue(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(e,p,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[p],fe),e}function ae(){return M[M.length-1]}function de(e){if(!e[p])return;let{value:t,listeners:n}=e[p],r=ae();return r&&n.add(r),h.has(r)&&h.get(r).add(e),t}function le(e,t,n){if(!e[p])return;let r=e[p];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[p])return e[p].listeners.add(t)}function S(e,t,n){let r=e[p];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>S(s,c,!0))}return o}T(X); +globalThis.dde= {O: m, +assign: P, +assignAttribute: Z, +chainableAppend: te, +classListDeclarative: ne, createElement: y, -createElementNS: xt, -dispatchEvent: At, +createElementNS: Ee, +dispatchEvent: we, el: y, -elNS: xt, -elementAttribute: w, -empty: Ot, -isObservable: M, -observable: E, +elNS: Ee, +elementAttribute: O, +empty: xe, +isObservable: W, +observable: m, on: _, -registerReactivity: $, +registerReactivity: T, scope: b, -simulateSlots: Et +simulateSlots: _e }; })(); \ No newline at end of file diff --git a/dist/esm-with-observables.js b/dist/esm-with-observables.js index a7268a6..d1da246 100644 --- a/dist/esm-with-observables.js +++ b/dist/esm-with-observables.js @@ -1,4 +1,4 @@ -var R={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(R,t):(Object.setPrototypeOf(t,R),t)}function N(t){return R.isPrototypeOf(t)&&t!==R?t:R}function x(t){return typeof t>"u"}function H(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function P(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var F={setDeleteAttr:et,ssr:""};function et(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var v=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function I(...t){return this.appendOriginal(...t),this}function nt(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var L;function y(t,e,...n){let r=N(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,b.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(m=>m(s)),void 0):s}),c=t(e||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:t.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case t==="#text":c=j.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=j.call(this,document.createDocumentFragment(),e);break;case!!L:c=j.call(this,document.createElementNS(L,t),e);break;case!c:c=j.call(this,document.createElement(t),e)}return nt(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function Et(t){let e=Symbol.for("default"),n=Array.from(t.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||e,c)&&o,{}),r=Reflect.has(n,e);return t.append=new Proxy(t.append,{apply(o,c,s){if(!s.length)return t;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,m=n[l];w(u,"remove","slot"),m&&(m.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[e].replaceWith(d),Reflect.deleteProperty(n,e)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),t}}),t}y.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(``);return e||(r.end=document.createComment("")),r};function xt(t){let e=this;return function(...r){L=t;let o=y.call(e,...r);return L=void 0,o}}var{setDeleteAttr:J}=F,D=new WeakMap;function j(t,...e){if(!e.length)return t;D.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return D.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(d,u)=>G.call(c,t,d,u));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return z(o,n,Z.bind(null,t[e]));case"ariaset":return z(o,n,(d,u)=>r("aria-"+d,u));case"classList":return rt.call(c,t,n)}return ot(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(D.has(t))return D.get(t);let r=(t instanceof SVGElement?st:ct).bind(null,t,"Attribute"),o=N(e);return{setRemoveAttr:r,s:o}}function rt(t,e){let n=N(this);return z(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Ot(t){return Array.from(t.children).forEach(e=>e.remove()),t}function w(t,e,n,r){return t instanceof HTMLElement?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function ot(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!x(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function z(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function ct(t,e,n,r){return t[(x(r)?"remove":"set")+e](n,Q(r))}function st(t,e,n,r,o=null){return t[(x(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function At(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=ft(),it=new WeakSet;_.connected=function(t,e){let{custom_element:n}=b.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(P(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};_.disconnected=function(t,e){let{custom_element:n}=b.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||P(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var q=new WeakMap;_.disconnectedAsAbort=function(t){if(q.has(t))return q.get(t);let e=new AbortController;return q.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||it.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:m}of u)m.dispatchEvent(new CustomEvent(c,{detail:[l,m.getAttribute(l)]}))});return P(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ft(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}m(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let a=t.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let a=t.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){t.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of t.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!t.has(g))continue;let A=t.get(g);A.length_c&&(g.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(g),a=!0)}return a}function m(i,f){let a=!1;for(let g of i)f&&u(g).then(m),!(!t.has(g)||!t.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),t.delete(g),a=!0);return a}}var p=Symbol.for("observable");function M(t){try{return Reflect.has(t,p)}catch{return!1}}var T=[],h=new WeakMap;function E(t,e){if(typeof t!="function")return X(t,e);if(M(t))return t;let n=X(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),T.push(r),n(t()),T.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||C(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[p],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{observable:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));B(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};E.symbols={observable:p,onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,p)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var W="__dde_reactive";E.el=function(t,e){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return C(t,s);b.push(c);let u=e(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return B(t,s),tt(t,s,n,e),s(t()),o};var S="__dde_attributes";E.attribute=function(t,e=null){let n=O(e),r;return b.host(o=>{if(r=o,w(r,"has",t)?n(w(r,"get",t)):e!==null&&w(r,"set",t,e),o[S]){o[S][t]=n;return}r[S]={[t]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[S][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/O.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return w(r,"set",t,d)}})};var Y={isObservable:M,processReactiveAttribute(t,e,n,r){if(!M(n))return n;let o=c=>r(e,c);return B(n,o),tt(n,o,t,e),n()}};function tt(t,e,...n){let{current:r}=b;r.prevent||r.host(function(o){o[W]||(o[W]=[],_.disconnected(()=>o[W].forEach(([[c,s]])=>C(c,s,c[p]?.host()===o)))(o)),o[W].push([[t,e],...n])})}function X(t,e){let n=(...r)=>r.length?pt(n,...r):lt(n);return at(n,t,e)}var ut=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),U=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function at(t,e,n){let r=[];H(n)!=="[object Object]"&&(n={});let{onclear:o}=O.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(t,p,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new U},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[p],ut),t}function dt(){return T[T.length-1]}function lt(t){if(!t[p])return;let{value:e,listeners:n}=t[p],r=dt();return r&&n.add(r),h.has(r)&&h.get(r).add(t),e}function pt(t,e,n){if(!t[p])return;let r=t[p];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function B(t,e){if(t[p])return t[p].listeners.add(e)}function C(t,e,n){let r=t[p];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(t.clear(t),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>C(s,c,!0))}return o}$(Y);export{E as O,j as assign,G as assignAttribute,nt as chainableAppend,rt as classListDeclarative,y as createElement,xt as createElementNS,At as dispatchEvent,y as el,xt as elNS,w as elementAttribute,Ot as empty,M as isObservable,E as observable,_ as on,$ as registerReactivity,b as scope,Et as simulateSlots}; +var A={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function T(e,t=!0){return t?Object.assign(A,e):(Object.setPrototypeOf(e,A),e)}function C(e){return A.isPrototypeOf(e)&&e!==A?e:A}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function N(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:ee,ssr:""};function ee(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var E=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...E]},push(e={}){return E.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function H(...e){return this.appendOriginal(...e),this}function te(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var j;function y(e,t,...n){let r=C(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,b.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(v=>v(s)),void 0):s}),c=e(t||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:e.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case e==="#text":c=P.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=P.call(this,document.createDocumentFragment(),t);break;case!!j:c=P.call(this,document.createElementNS(j,e),t);break;case!c:c=P.call(this,document.createElement(e),t)}return te(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function _e(e){let t=Symbol.for("default"),n=Array.from(e.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||t,c)&&o,{}),r=Reflect.has(n,t);return e.append=new Proxy(e.append,{apply(o,c,s){if(!s.length)return e;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,v=n[l];O(u,"remove","slot"),v&&(v.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[t].replaceWith(d),Reflect.deleteProperty(n,t)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),e}}),e}y.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function Ee(e){let t=this;return function(...r){j=e;let o=y.call(t,...r);return j=void 0,o}}var{setDeleteAttr:I}=$,L=new WeakMap;function P(e,...t){if(!t.length)return e;L.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return L.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(d,u)=>Z.call(c,e,d,u));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(d,u)=>r("aria-"+d,u));case"classList":return ne.call(c,e,n)}return re(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(L.has(e))return L.get(e);let r=(e instanceof SVGElement?ce:oe).bind(null,e,"Attribute"),o=C(t);return{setRemoveAttr:r,s:o}}function ne(e,t){let n=C(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function xe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function O(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function re(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function oe(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ce(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function we(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function _(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ie(),se=new WeakSet;_.connected=function(e,t){let{custom_element:n}=b.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(N(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};_.disconnected=function(e,t){let{custom_element:n}=b.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||N(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;_.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(_.disconnected(()=>t.abort())),t};_.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||se.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:v}of u)v.dispatchEvent(new CustomEvent(c,{detail:[l,v.getAttribute(l)]}))});return N(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ie(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}v(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){e.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!e.has(g))continue;let w=e.get(g);w.length_c&&(g.dispatchEvent(new Event("dde:connected")),w.connected=new WeakSet,w.length_c=0,w.length_d||e.delete(g),a=!0)}return a}function v(i,f){let a=!1;for(let g of i)f&&u(g).then(v),!(!e.has(g)||!e.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),e.delete(g),a=!0);return a}}var p=Symbol.for("observable");function W(e){try{return Reflect.has(e,p)}catch{return!1}}var M=[],h=new WeakMap;function m(e,t){if(typeof e!="function")return Q(e,t);if(W(e))return e;let n=Q(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),M.push(r),n(e()),M.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||S(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[p],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>S(t,n))}};m.symbols={observable:p,onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,p)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return S(e,s);b.push(c);let u=t(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return U(e,s),Y(e,s,n,t),s(e()),o};var R="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return b.host(o=>{if(r=o,O(r,"has",e)?n(O(r,"get",e)):t!==null&&O(r,"set",e,t),o[R]){o[R][e]=n;return}r[R]={[e]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[R][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[R]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return O(r,"set",e,d)}})};var X={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),Y(n,o,e,t),n()}};function Y(e,t,...n){let{current:r}=b;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>S(c,s,c[p]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function Q(e,t){let n=(...r)=>r.length?le(n,...r):de(n);return ue(n,e,t)}var fe=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` +`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function ue(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(e,p,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[p],fe),e}function ae(){return M[M.length-1]}function de(e){if(!e[p])return;let{value:t,listeners:n}=e[p],r=ae();return r&&n.add(r),h.has(r)&&h.get(r).add(e),t}function le(e,t,n){if(!e[p])return;let r=e[p];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[p])return e[p].listeners.add(t)}function S(e,t,n){let r=e[p];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>S(s,c,!0))}return o}T(X);export{m as O,P as assign,Z as assignAttribute,te as chainableAppend,ne as classListDeclarative,y as createElement,Ee as createElementNS,we as dispatchEvent,y as el,Ee as elNS,O as elementAttribute,xe as empty,W as isObservable,m as observable,_ as on,T as registerReactivity,b as scope,_e as simulateSlots}; diff --git a/src/observables-lib.js b/src/observables-lib.js index 09602b2..cfad2e2 100644 --- a/src/observables-lib.js +++ b/src/observables-lib.js @@ -43,20 +43,20 @@ export function observable(value, actions){ return out; } export { observable as O }; -observable.action= function(observable, name, ...a){ - const s= observable[mark], { actions }= s; +observable.action= function(o, name, ...a){ + const s= o[mark], { actions }= s; if(!actions || !Reflect.has(actions, name)) - throw new Error(`'${observable}' has no action with name '${name}'!`); + throw new Error(`'${o}' has no action with name '${name}'!`); actions[name].apply(s, a); if(s.skip) return Reflect.deleteProperty(s, "skip"); s.listeners.forEach(l=> l(s.value)); }; -observable.on= function on(observables, listener, options= {}){ - const { observable: as }= options; +observable.on= function on(o, listener, options= {}){ + const { signal: as }= options; if(as && as.aborted) return; - if(Array.isArray(observables)) return observables.forEach(s=> on(s, listener, options)); - addObservableListener(observables, listener); - if(as) as.addEventListener("abort", ()=> removeObservableListener(observables, listener)); + if(Array.isArray(o)) return o.forEach(s=> on(s, listener, options)); + addObservableListener(o, listener); + if(as) as.addEventListener("abort", ()=> removeObservableListener(o, listener)); //TODO cleanup when observable removed }; observable.symbols= { @@ -64,23 +64,23 @@ observable.symbols= { onclear: Symbol.for("Observable.onclear") }; observable.clear= function(...observables){ - for(const observable of observables){ - Reflect.deleteProperty(observable, "toJSON"); - const s= observable[mark]; + for(const o of observables){ + Reflect.deleteProperty(o, "toJSON"); + const s= o[mark]; s.onclear.forEach(f=> f.call(s)); - clearListDeps(observable, s); - Reflect.deleteProperty(observable, mark); + clearListDeps(o, s); + Reflect.deleteProperty(o, mark); } - function clearListDeps(observable, s){ + function clearListDeps(o, s){ s.listeners.forEach(l=> { s.listeners.delete(l); if(!deps.has(l)) return; const ls= deps.get(l); - ls.delete(observable); + ls.delete(o); if(ls.size>1) return; - observable.clear(...ls); + o.clear(...ls); deps.delete(l); }); } @@ -88,7 +88,7 @@ observable.clear= function(...observables){ const key_reactive= "__dde_reactive"; import { el, elementAttribute } from "./dom.js"; import { scope } from "./dom.js"; -observable.el= function(observable, map){ +observable.el= function(o, map){ const mark_start= el.mark({ type: "reactive" }, false); const mark_end= mark_start.end; const out= document.createDocumentFragment(); @@ -96,7 +96,7 @@ observable.el= function(observable, map){ const { current }= scope; const reRenderReactiveElement= v=> { if(!mark_start.parentNode || !mark_end.parentNode) - return removeObservableListener(observable, reRenderReactiveElement); + return removeObservableListener(o, reRenderReactiveElement); scope.push(current); let els= map(v); scope.pop(); @@ -107,16 +107,16 @@ observable.el= function(observable, map){ el_r.remove(); mark_start.after(...els); }; - addObservableListener(observable, reRenderReactiveElement); - removeObservablesFromElements(observable, reRenderReactiveElement, mark_start, map); - reRenderReactiveElement(observable()); + addObservableListener(o, reRenderReactiveElement); + removeObservablesFromElements(o, reRenderReactiveElement, mark_start, map); + reRenderReactiveElement(o()); return out; }; import { on } from "./events.js"; const key_attributes= "__dde_attributes"; observable.attribute= function(name, initial= null){ //TODO host=element & reuse existing - const out= O(initial); + const out= observable(initial); let element; scope.host(el=> { element= el; @@ -138,7 +138,7 @@ observable.attribute= function(name, initial= null){ on.disconnected(function(){ /*! This removes all observables mapped to attributes (`S.attribute`). * Investigate `__dde_attributes` key of the element.*/ - O.clear(...Object.values(element[key_attributes])); + observable.clear(...Object.values(element[key_attributes])); })(element); }); return new Proxy(out, { @@ -161,7 +161,7 @@ export const observables_config= { return attrs(); } }; -function removeObservablesFromElements(observable, listener, ...notes){ +function removeObservablesFromElements(o, listener, ...notes){ const { current }= scope; if(current.prevent) return; current.host(function(element){ @@ -172,18 +172,18 @@ function removeObservablesFromElements(observable, listener, ...notes){ * Clears all Observables listeners added in the current scope/host (`S.el`, `assign`, …?). * You can investigate the `__dde_reactive` key of the element. * */ - element[key_reactive].forEach(([ [ observable, listener ] ])=> - removeObservableListener(observable, listener, observable[mark]?.host() === element)) + element[key_reactive].forEach(([ [ o, listener ] ])=> + removeObservableListener(o, listener, o[mark]?.host() === element)) )(element); } - element[key_reactive].push([ [ observable, listener ], ...notes ]); + element[key_reactive].push([ [ o, listener ], ...notes ]); }); } function create(value, actions){ - const observable= (...value)=> - value.length ? write(observable, ...value) : read(observable); - return toObservable(observable, value, actions); + const o= (...value)=> + value.length ? write(o, ...value) : read(o); + return toObservable(o, value, actions); } const protoSigal= Object.assign(Object.create(null), { stopPropagation(){ @@ -198,17 +198,17 @@ class ObservableDefined extends Error{ this.stack= rest.find(l=> !l.includes(curr_file)); } } -function toObservable(observable, value, actions){ +function toObservable(o, value, actions){ const onclear= []; if(typeOf(actions)!=="[object Object]") actions= {}; - const { onclear: ocs }= O.symbols; + const { onclear: ocs }= observable.symbols; if(actions[ocs]){ onclear.push(actions[ocs]); Reflect.deleteProperty(actions, ocs); } const { host }= scope; - Reflect.defineProperty(observable, mark, { + Reflect.defineProperty(o, mark, { value: { value, actions, onclear, host, listeners: new Set(), @@ -218,40 +218,40 @@ function toObservable(observable, value, actions){ writable: false, configurable: true }); - observable.toJSON= ()=> observable(); - Object.setPrototypeOf(observable[mark], protoSigal); - return observable; + o.toJSON= ()=> o(); + Object.setPrototypeOf(o[mark], protoSigal); + return o; } function currentContext(){ return stack_watch[stack_watch.length - 1]; } -function read(observable){ - if(!observable[mark]) return; - const { value, listeners }= observable[mark]; +function read(o){ + if(!o[mark]) return; + const { value, listeners }= o[mark]; const context= currentContext(); if(context) listeners.add(context); - if(deps.has(context)) deps.get(context).add(observable); + if(deps.has(context)) deps.get(context).add(o); return value; } -function write(observable, value, force){ - if(!observable[mark]) return; - const s= observable[mark]; +function write(o, value, force){ + if(!o[mark]) return; + const s= o[mark]; if(!force && s.value===value) return; s.value= value; s.listeners.forEach(l=> l(value)); return value; } -function addObservableListener(observable, listener){ - if(!observable[mark]) return; - return observable[mark].listeners.add(listener); +function addObservableListener(o, listener){ + if(!o[mark]) return; + return o[mark].listeners.add(listener); } -function removeObservableListener(observable, listener, clear_when_empty){ - const s= observable[mark]; +function removeObservableListener(o, listener, clear_when_empty){ + const s= o[mark]; if(!s) return; const out= s.listeners.delete(listener); if(clear_when_empty && !s.listeners.size){ - observable.clear(observable); + o.clear(o); if(!deps.has(s)) return out; const c= deps.get(s); if(!deps.has(c)) return out;