mancha 0.5.1 → 0.5.2
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/mancha.js +1 -1
- package/dist/reactive.d.ts +10 -4
- package/dist/reactive.js +32 -20
- package/package.json +2 -2
package/dist/mancha.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";var t={885:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.attributeNameToCamelCase=void 0,e.attributeNameToCamelCase=function(t){return t.replace(/-./g,(t=>t[1].toUpperCase()))}},246:function(t,e,i){var r,o,s,n,a=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0});const l=i(283);class c extends l.IRenderer{constructor(){super(...arguments),this.dirpath=(0,l.dirname)(self.location.href)}parseHTML(t,e={root:!1}){if(e.root)return(new DOMParser).parseFromString(t,"text/html");{const e=document.createRange();return e.selectNodeContents(document.body),e.createContextualFragment(t)}}serializeHTML(t){return(new XMLSerializer).serializeToString(t).replace(/\s?xmlns="[^"]+"/gm,"")}preprocessLocal(t,e){return this.preprocessRemote(t,e)}}const h=new c;self.Mancha=h;const d=null===(r=self.document)||void 0===r?void 0:r.currentScript;if(null===(s=null===(o=self.document)||void 0===o?void 0:o.currentScript)||void 0===s?void 0:s.hasAttribute("init")){h.update(Object.assign({},null==d?void 0:d.dataset));const t=null==d?void 0:d.hasAttribute("debug"),e=null==d?void 0:d.getAttribute("cache");((null===(n=null==d?void 0:d.getAttribute("target"))||void 0===n?void 0:n.split(","))||["body"]).map((i=>a(void 0,void 0,void 0,(function*(){const r=self.document.querySelector(i);yield h.mount(r,{cache:e,debug:t})}))))}e.default=h},283:function(t,e,i){var r=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.IRenderer=e.safeEval=e.isRelativePath=e.dirname=e.traverse=void 0;const o=i(63),s=i(150),n=i(230);function*a(t,e=new Set){const i=new Set,r=Array.from(t.childNodes).filter((t=>!e.has(t)));for(yield t;r.length;){const t=r.pop();i.has(t)||(i.add(t),yield t),t.childNodes&&Array.from(t.childNodes).filter((t=>!e.has(t))).forEach((t=>r.push(t)))}}function l(t){return t.includes("/")?t.split("/").slice(0,-1).join("/"):""}function c(t,e,i={}){const r=`with (this) { return (async () => (${t}))(); }`;return new Function(...Object.keys(i),r).call(e,...Object.values(i))}e.traverse=a,e.dirname=l,e.isRelativePath=function(t){return!(t.includes("://")||t.startsWith("/")||t.startsWith("#")||t.startsWith("data:"))},e.safeEval=c;class h extends o.ReactiveProxyStore{constructor(){super(...arguments),this.dirpath="",this.skipNodes=new Set}fetchRemote(t,e){return r(this,void 0,void 0,(function*(){var i;return fetch(t,{cache:null!==(i=null==e?void 0:e.cache)&&void 0!==i?i:"default"}).then((t=>t.text()))}))}fetchLocal(t,e){return r(this,void 0,void 0,(function*(){return this.fetchRemote(t,e)}))}preprocessString(t,e){return r(this,void 0,void 0,(function*(){this.log(e,"Preprocessing string content with params:\n",e);const i=this.parseHTML(t,e);return yield this.preprocessNode(i,e),i}))}preprocessLocal(t,e){return r(this,void 0,void 0,(function*(){var i;const r=yield this.fetchLocal(t,e);return this.preprocessString(r,Object.assign(Object.assign({},e),{dirpath:l(t),root:null!==(i=null==e?void 0:e.root)&&void 0!==i?i:!t.endsWith(".tpl.html")}))}))}preprocessRemote(t,e){return r(this,void 0,void 0,(function*(){var i;const r=(null==e?void 0:e.cache)||"default",o=yield fetch(t,{cache:r}).then((t=>t.text()));return this.preprocessString(o,Object.assign(Object.assign({},e),{dirpath:l(t),root:null!==(i=null==e?void 0:e.root)&&void 0!==i?i:!t.endsWith(".tpl.html")}))}))}clone(){return new this.constructor(Object.fromEntries(this.store.entries()))}log(t,...e){(null==t?void 0:t.debug)&&console.debug(...e)}eval(t){return r(this,arguments,void 0,(function*(t,e={},i){const r=(0,o.proxify)(this),s=yield c(t,r,Object.assign({},e));return this.log(i,`eval \`${t}\` => `,s),s}))}preprocessNode(t,e){return r(this,void 0,void 0,(function*(){e=Object.assign({dirpath:this.dirpath,maxdepth:10},e);const i=new s.Iterator(a(t,this.skipNodes)).map((t=>r(this,void 0,void 0,(function*(){this.log(e,"Preprocessing node:\n",t),yield n.resolveIncludes.call(this,t,e),yield n.rebaseRelativePaths.call(this,t,e)}))));yield Promise.all(i.generator())}))}renderNode(t,e){return r(this,void 0,void 0,(function*(){for(const i of a(t,this.skipNodes))this.log(e,"Rendering node:\n",i),yield n.resolveDataAttribute.call(this,i,e),yield n.resolveForAttribute.call(this,i,e),yield n.resolveHtmlAttribute.call(this,i,e),yield n.resolveShowAttribute.call(this,i,e),yield n.resolveWatchAttribute.call(this,i,e),yield n.resolveBindAttribute.call(this,i,e),yield n.resolvePropAttributes.call(this,i,e),yield n.resolveAttrAttributes.call(this,i,e),yield n.resolveEventAttributes.call(this,i,e),yield n.resolveTextNodeExpressions.call(this,i,e)}))}mount(t,e){return r(this,void 0,void 0,(function*(){yield this.preprocessNode(t,e),yield this.renderNode(t,e)}))}}e.IRenderer=h},150:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Iterator=void 0;class i{constructor(t){this.iterable=t}filter(t){return new i(i.filterGenerator(t,this.iterable))}map(t){return new i(i.mapGenerator(t,this.iterable))}array(){return Array.from(this.iterable)}*generator(){for(const t of this.iterable)yield t}static*filterGenerator(t,e){for(const i of e)t(i)&&(yield i)}static*mapGenerator(t,e){for(const i of e)yield t(i)}}e.Iterator=i},230:function(t,e,i){var r=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.resolveShowAttribute=e.resolveBindAttribute=e.resolveForAttribute=e.resolveEventAttributes=e.resolveAttrAttributes=e.resolvePropAttributes=e.resolveHtmlAttribute=e.resolveWatchAttribute=e.resolveDataAttribute=e.resolveTextNodeExpressions=e.rebaseRelativePaths=e.resolveIncludes=void 0;const o=i(885),s=i(283),n=new Set([":bind",":bind-events",":data",":for",":show","@watch","$html"]),a={$text:"$text-content"};e.resolveIncludes=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o,s;const n=t;if("include"!==(null===(i=n.tagName)||void 0===i?void 0:i.toLocaleLowerCase()))return;this.log(e,"<include> tag found in:\n",t),this.log(e,"<include> params:",e);const a=null===(r=n.getAttribute)||void 0===r?void 0:r.call(n,"src");if(!a)throw new Error(`"src" attribute missing from ${t}.`);const l=e=>{t.replaceWith(...Array.from(e.childNodes))},c=Object.assign(Object.assign({},e),{root:!1,maxdepth:e.maxdepth-1});if(0===c.maxdepth)throw new Error("Maximum recursion depth reached.");if(a.includes("://")||a.startsWith("//"))this.log(e,"Including remote file from absolute path:",a),yield this.preprocessRemote(a,c).then(l);else if((null===(o=e.dirpath)||void 0===o?void 0:o.includes("://"))||(null===(s=e.dirpath)||void 0===s?void 0:s.startsWith("//"))){const t=e.dirpath&&"."!==e.dirpath?`${e.dirpath}/${a}`:a;this.log(e,"Including remote file from relative path:",t),yield this.preprocessRemote(t,c).then(l)}else if("/"===a.charAt(0))this.log(e,"Including local file from absolute path:",a),yield this.preprocessLocal(a,c).then(l);else{const t=e.dirpath&&"."!==e.dirpath?`${e.dirpath}/${a}`:a;this.log(e,"Including local file from relative path:",t),yield this.preprocessLocal(t,c).then(l)}}))},e.rebaseRelativePaths=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o,n,a,l,c;const h=t,d=null===(i=h.tagName)||void 0===i?void 0:i.toLowerCase();if(!e.dirpath)return;const u=null===(o=(r=t).getAttribute)||void 0===o?void 0:o.call(r,"src"),v=null===(a=(n=t).getAttribute)||void 0===a?void 0:a.call(n,"href"),f=null===(c=(l=t).getAttribute)||void 0===c?void 0:c.call(l,"data"),p=u||v||f;p&&(p&&(0,s.isRelativePath)(p)&&this.log(e,"Rebasing relative path as:",e.dirpath,"/",p),"img"===d&&u&&(0,s.isRelativePath)(u)?h.src=`${e.dirpath}/${u}`:"a"===d&&v&&(0,s.isRelativePath)(v)||"link"===d&&v&&(0,s.isRelativePath)(v)?h.href=`${e.dirpath}/${v}`:"script"===d&&u&&(0,s.isRelativePath)(u)||"source"===d&&u&&(0,s.isRelativePath)(u)||"audio"===d&&u&&(0,s.isRelativePath)(u)||"video"===d&&u&&(0,s.isRelativePath)(u)||"track"===d&&u&&(0,s.isRelativePath)(u)||"iframe"===d&&u&&(0,s.isRelativePath)(u)?h.src=`${e.dirpath}/${u}`:"object"===d&&f&&(0,s.isRelativePath)(f)?h.data=`${e.dirpath}/${f}`:"input"===d&&u&&(0,s.isRelativePath)(u)?h.src=`${e.dirpath}/${u}`:("area"===d&&v&&(0,s.isRelativePath)(v)||"base"===d&&v&&(0,s.isRelativePath)(v))&&(h.href=`${e.dirpath}/${v}`))}))},e.resolveTextNodeExpressions=function(t,e){return r(this,void 0,void 0,(function*(){if(3!==t.nodeType)return;const i=t.nodeValue||"",o=new RegExp(/{{ ([^}]+) }}/gm),s=Array.from(i.matchAll(o)).map((t=>t[1])),n=()=>r(this,void 0,void 0,(function*(){let r=i;for(const i of s){const o=yield this.eval(i,{$elem:t},e);r=r.replace(`{{ ${i} }}`,String(o))}t.nodeValue=r})),[a,l]=yield this.trace(n);this.log(e,i,"=>",a),this.watch(l,n)}))},e.resolveDataAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t,o=null===(i=r.getAttribute)||void 0===i?void 0:i.call(r,":data");if(o){this.log(e,":data attribute found in:\n",t),r.removeAttribute(":data");const i=yield this.eval(o,{$elem:t},e);this.log(e,":data",o,"=>",i),yield this.update(i)}}))},e.resolveWatchAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t,o=null===(i=r.getAttribute)||void 0===i?void 0:i.call(r,"@watch");if(o){this.log(e,"@watch attribute found in:\n",t),r.removeAttribute("@watch");const i=()=>this.eval(o,{$elem:t},e),[s,n]=yield this.trace(i);this.log(e,"@watch",o,"=>",s),this.watch(n,i)}}))},e.resolveHtmlAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,s=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,"$html");if(s){this.log(e,"$html attribute found in:\n",t),o.removeAttribute("$html");const i=this.clone(),n=()=>r(this,void 0,void 0,(function*(){const r=yield this.eval(s,{$elem:t},e),n=yield i.preprocessString(r,e);yield i.renderNode(n,e),o.replaceChildren(n)})),[a,l]=yield this.trace(n);this.log(e,"$html",s,"=>",a),this.watch(l,n)}}))},e.resolvePropAttributes=function(t,e){return r(this,void 0,void 0,(function*(){if(this.skipNodes.has(t))return;const i=t;for(const s of Array.from(i.attributes||[]))if(s.name.startsWith("$")&&!n.has(s.name)){this.log(e,s.name,"attribute found in:\n",t),i.removeAttribute(s.name);const n=(a[s.name]||s.name).slice(1),l=()=>this.eval(s.value,{$elem:t},e),[c,h]=yield this.trace(l);this.log(e,s.name,s.value,"=>",c,`[${h}]`);const d=(0,o.attributeNameToCamelCase)(n);this.watch(h,(()=>r(this,void 0,void 0,(function*(){return t[d]=yield l()})))),t[d]=c}}))},e.resolveAttrAttributes=function(t,e){return r(this,void 0,void 0,(function*(){if(this.skipNodes.has(t))return;const i=t;for(const o of Array.from(i.attributes||[]))if(o.name.startsWith(":")&&!n.has(o.name)){this.log(e,o.name,"attribute found in:\n",t),i.removeAttribute(o.name);const s=(a[o.name]||o.name).slice(1),n=()=>this.eval(o.value,{$elem:t},e),[l,c]=yield this.trace(n);this.log(e,o.name,o.value,"=>",l,`[${c}]`),this.watch(c,(()=>r(this,void 0,void 0,(function*(){return i.setAttribute(s,yield n())})))),i.setAttribute(s,l)}}))},e.resolveEventAttributes=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t;for(const o of Array.from(r.attributes||[]))o.name.startsWith("@")&&!n.has(o.name)&&(this.log(e,o.name,"attribute found in:\n",t),r.removeAttribute(o.name),null===(i=t.addEventListener)||void 0===i||i.call(t,o.name.substring(1),(i=>this.eval(o.value,{$elem:t,$event:i},e))))}))},e.resolveForAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,n=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,":for");if(n){this.log(e,":for attribute found in:\n",t),o.removeAttribute(":for");for(const e of(0,s.traverse)(t,this.skipNodes))this.skipNodes.add(e);const i=t.parentNode,a=t.ownerDocument.createElement("template");i.insertBefore(a,t),a.append(t),this.log(e,":for template:\n",a);const l=n.split(" in ",2);if(2!==l.length)throw new Error(`Invalid :for format: \`${n}\`. Expected "{key} in {expression}".`);let c=[],h=[];const[d,u]=l;try{[c,h]=yield this.trace((()=>this.eval(u,{$elem:t},e))),this.log(e,u,"=>",c,`[${h}]`)}catch(t){return void console.error(t)}const v=[],f=o=>r(this,void 0,void 0,(function*(){if(this.log(e,":for list items:",o),Array.isArray(o))return this.lock=this.lock.then((()=>new Promise((s=>r(this,void 0,void 0,(function*(){v.splice(0,v.length).forEach((t=>{i.removeChild(t),this.skipNodes.delete(t)}));for(const r of o.slice(0).reverse()){const o=this.clone();yield o.set(d,r);const s=t.cloneNode(!0);i.insertBefore(s,a.nextSibling),v.push(s),this.skipNodes.add(s),yield o.mount(s,e),this.log(e,"Rendered list child:\n",s)}s()})))))),this.lock;console.error(`Expression did not yield a list: \`${u}\` => \`${o}\``)}));return this.watch(h,(()=>r(this,void 0,void 0,(function*(){return f(yield this.eval(u,{$elem:t},e))})))),f(c)}}))},e.resolveBindAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o;if(this.skipNodes.has(t))return;const s=t,n=null===(i=s.getAttribute)||void 0===i?void 0:i.call(s,":bind");if(n){this.log(e,":bind attribute found in:\n",t);const i=["change","input"],a=(null===(o=null===(r=s.getAttribute)||void 0===r?void 0:r.call(s,":bind-events"))||void 0===o?void 0:o.split(","))||i;s.removeAttribute(":bind"),s.removeAttribute(":bind-events");const l="checkbox"===s.getAttribute("type")?"checked":"value";this.has(n)||(yield this.set(n,s[l])),s[l]=this.get(n);for(const e of a)t.addEventListener(e,(()=>this.set(n,s[l])));this.watch([n],(()=>s[l]=this.get(n)))}}))},e.resolveShowAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,s=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,":show");if(s){this.log(e,":show attribute found in:\n",t),o.removeAttribute(":show");const i=()=>this.eval(s,{$elem:t},e),[n,a]=yield this.trace(i);this.log(e,":show",s,"=>",n,`[${a}]`);const l="none"===o.style.display?"":o.style.display;n||(o.style.display="none"),this.watch(a,(()=>r(this,void 0,void 0,(function*(){o.style.display=(yield i())?l:"none"}))))}}))}},63:function(t,e){var i=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};function r(t,e,i=!0){if(null==t||function(t){return t instanceof o||t.__is_proxy__}(t))return t;if(i)for(const i in t)t.hasOwnProperty(i)&&"object"==typeof t[i]&&null!=t[i]&&(t[i]=r(t[i],e));return new Proxy(t,{deleteProperty:(t,i)=>i in t&&(delete t[i],e(),!0),set:(t,o,s,n)=>{i&&"object"==typeof s&&(s=r(s,e));const a=Reflect.set(t,o,s,n);return e(),a},get:(t,e,i)=>"__is_proxy__"===e||Reflect.get(t,e,i)})}Object.defineProperty(e,"__esModule",{value:!0}),e.proxify=e.ReactiveProxyStore=e.InertProxy=e.ReactiveProxy=e.proxifyObject=void 0,e.proxifyObject=r;class o{constructor(t=null,...e){this.value=null,this.listeners=[],this.set(t),e.forEach((t=>this.watch(t)))}static from(t,...e){return t instanceof o?(e.forEach(t.watch),t):new o(t,...e)}get(){return this.value}set(t){return i(this,void 0,void 0,(function*(){if(this.value!==t){const e=this.value;null!=t&&"object"==typeof t&&(t=r(t,(()=>this.trigger()))),this.value=t,yield this.trigger(e)}}))}watch(t){this.listeners.push(t)}unwatch(t){this.listeners=this.listeners.filter((e=>e!==t))}trigger(){return i(this,arguments,void 0,(function*(t=null){for(const e of this.listeners)yield e(this.value,t)}))}}e.ReactiveProxy=o;class s extends o{static from(t,...e){return t instanceof o?t:new s(t,...e)}watch(t){}trigger(t){return Promise.resolve()}}function n(t){const e=Array.from(t.entries()).map((([t])=>t)),i=Object.fromEntries(e.map((t=>[t,void 0])));return new Proxy(Object.assign({},t,i),{get:(e,i,r)=>"string"==typeof i&&t.has(i)?t.get(i):Reflect.get(t,i,r),set:(e,i,r,o)=>("string"!=typeof i||i in t?Reflect.set(t,i,r,o):t.set(i,r),!0)})}e.InertProxy=s,e.ReactiveProxyStore=class{wrapFnValue(t){return t&&"function"==typeof t?(...e)=>t.call(n(this),...e):t}constructor(t){this.store=new Map,this.tracing=!1,this.traced=new Set,this.lock=Promise.resolve();for(const[e,i]of Object.entries(t||{}))this.store.set(e,o.from(this.wrapFnValue(i)))}get $(){return n(this)}entries(){return this.store.entries()}get(t){var e;return this.tracing&&this.traced.add(t),null===(e=this.store.get(t))||void 0===e?void 0:e.get()}set(t,e){return i(this,void 0,void 0,(function*(){this.store.has(t)?yield this.store.get(t).set(this.wrapFnValue(e)):this.store.set(t,o.from(this.wrapFnValue(e)))}))}del(t){return this.store.delete(t)}has(t){return this.store.has(t)}update(t){return i(this,void 0,void 0,(function*(){for(const[e,i]of Object.entries(t))yield this.set(e,i)}))}watch(t,e){(t=Array.isArray(t)?t:[t]).forEach((i=>this.store.get(i).watch((()=>e(...t.map((t=>this.store.get(t).get())))))))}trigger(t){return i(this,void 0,void 0,(function*(){for(const e of Array.isArray(t)?t:[t])yield this.store.get(e).trigger()}))}trace(t){return i(this,void 0,void 0,(function*(){yield this.lock;const e=new Promise(((e,r)=>i(this,void 0,void 0,(function*(){this.traced.clear(),this.tracing=!0;try{const i=yield t(),r=Array.from(this.traced);e([i,r])}catch(t){r(t)}finally{this.tracing=!1,this.traced.clear()}}))));return this.lock=e.then((()=>{})),e}))}computed(t,e){return i(this,void 0,void 0,(function*(){const[r,o]=yield this.trace((()=>e.call(n(this))));this.watch(o,(()=>i(this,void 0,void 0,(function*(){return this.set(t,yield e.call(n(this)))})))),this.set(t,r)}))}},e.proxify=n}},e={};!function i(r){var o=e[r];if(void 0!==o)return o.exports;var s=e[r]={exports:{}};return t[r].call(s.exports,s,s.exports,i),s.exports}(246)})();
|
|
1
|
+
(()=>{"use strict";var t={885:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.attributeNameToCamelCase=void 0,e.attributeNameToCamelCase=function(t){return t.replace(/-./g,(t=>t[1].toUpperCase()))}},246:function(t,e,i){var r,o,s,n,a=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0});const l=i(283);class c extends l.IRenderer{constructor(){super(...arguments),this.dirpath=(0,l.dirname)(self.location.href)}parseHTML(t,e={root:!1}){if(e.root)return(new DOMParser).parseFromString(t,"text/html");{const e=document.createRange();return e.selectNodeContents(document.body),e.createContextualFragment(t)}}serializeHTML(t){return(new XMLSerializer).serializeToString(t).replace(/\s?xmlns="[^"]+"/gm,"")}preprocessLocal(t,e){return this.preprocessRemote(t,e)}}const h=new c;self.Mancha=h;const u=null===(r=self.document)||void 0===r?void 0:r.currentScript;if(null===(s=null===(o=self.document)||void 0===o?void 0:o.currentScript)||void 0===s?void 0:s.hasAttribute("init")){h.update(Object.assign({},null==u?void 0:u.dataset));const t=null==u?void 0:u.hasAttribute("debug"),e=null==u?void 0:u.getAttribute("cache");((null===(n=null==u?void 0:u.getAttribute("target"))||void 0===n?void 0:n.split(","))||["body"]).map((i=>a(void 0,void 0,void 0,(function*(){const r=self.document.querySelector(i);yield h.mount(r,{cache:e,debug:t})}))))}e.default=h},283:function(t,e,i){var r=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.IRenderer=e.safeEval=e.isRelativePath=e.dirname=e.traverse=void 0;const o=i(63),s=i(150),n=i(230);function*a(t,e=new Set){const i=new Set,r=Array.from(t.childNodes).filter((t=>!e.has(t)));for(yield t;r.length;){const t=r.pop();i.has(t)||(i.add(t),yield t),t.childNodes&&Array.from(t.childNodes).filter((t=>!e.has(t))).forEach((t=>r.push(t)))}}function l(t){return t.includes("/")?t.split("/").slice(0,-1).join("/"):""}function c(t,e,i={}){const r=`with (this) { return (async () => (${t}))(); }`;return new Function(...Object.keys(i),r).call(e,...Object.values(i))}e.traverse=a,e.dirname=l,e.isRelativePath=function(t){return!(t.includes("://")||t.startsWith("/")||t.startsWith("#")||t.startsWith("data:"))},e.safeEval=c;class h extends o.ReactiveProxyStore{constructor(){super(...arguments),this.dirpath="",this.skipNodes=new Set}fetchRemote(t,e){return r(this,void 0,void 0,(function*(){var i;return fetch(t,{cache:null!==(i=null==e?void 0:e.cache)&&void 0!==i?i:"default"}).then((t=>t.text()))}))}fetchLocal(t,e){return r(this,void 0,void 0,(function*(){return this.fetchRemote(t,e)}))}preprocessString(t,e){return r(this,void 0,void 0,(function*(){this.log(e,"Preprocessing string content with params:\n",e);const i=this.parseHTML(t,e);return yield this.preprocessNode(i,e),i}))}preprocessLocal(t,e){return r(this,void 0,void 0,(function*(){var i;const r=yield this.fetchLocal(t,e);return this.preprocessString(r,Object.assign(Object.assign({},e),{dirpath:l(t),root:null!==(i=null==e?void 0:e.root)&&void 0!==i?i:!t.endsWith(".tpl.html")}))}))}preprocessRemote(t,e){return r(this,void 0,void 0,(function*(){var i;const r=(null==e?void 0:e.cache)||"default",o=yield fetch(t,{cache:r}).then((t=>t.text()));return this.preprocessString(o,Object.assign(Object.assign({},e),{dirpath:l(t),root:null!==(i=null==e?void 0:e.root)&&void 0!==i?i:!t.endsWith(".tpl.html")}))}))}clone(){return new this.constructor(Object.fromEntries(this.store.entries()))}log(t,...e){(null==t?void 0:t.debug)&&console.debug(...e)}eval(t){return r(this,arguments,void 0,(function*(t,e={},i){const r=(0,o.proxify)(this),s=yield c(t,r,Object.assign({},e));return this.log(i,`eval \`${t}\` => `,s),s}))}preprocessNode(t,e){return r(this,void 0,void 0,(function*(){e=Object.assign({dirpath:this.dirpath,maxdepth:10},e);const i=new s.Iterator(a(t,this.skipNodes)).map((t=>r(this,void 0,void 0,(function*(){this.log(e,"Preprocessing node:\n",t),yield n.resolveIncludes.call(this,t,e),yield n.rebaseRelativePaths.call(this,t,e)}))));yield Promise.all(i.generator())}))}renderNode(t,e){return r(this,void 0,void 0,(function*(){for(const i of a(t,this.skipNodes))this.log(e,"Rendering node:\n",i),yield n.resolveDataAttribute.call(this,i,e),yield n.resolveForAttribute.call(this,i,e),yield n.resolveHtmlAttribute.call(this,i,e),yield n.resolveShowAttribute.call(this,i,e),yield n.resolveWatchAttribute.call(this,i,e),yield n.resolveBindAttribute.call(this,i,e),yield n.resolvePropAttributes.call(this,i,e),yield n.resolveAttrAttributes.call(this,i,e),yield n.resolveEventAttributes.call(this,i,e),yield n.resolveTextNodeExpressions.call(this,i,e)}))}mount(t,e){return r(this,void 0,void 0,(function*(){yield this.preprocessNode(t,e),yield this.renderNode(t,e)}))}}e.IRenderer=h},150:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Iterator=void 0;class i{constructor(t){this.iterable=t}filter(t){return new i(i.filterGenerator(t,this.iterable))}map(t){return new i(i.mapGenerator(t,this.iterable))}array(){return Array.from(this.iterable)}*generator(){for(const t of this.iterable)yield t}static*filterGenerator(t,e){for(const i of e)t(i)&&(yield i)}static*mapGenerator(t,e){for(const i of e)yield t(i)}}e.Iterator=i},230:function(t,e,i){var r=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.resolveShowAttribute=e.resolveBindAttribute=e.resolveForAttribute=e.resolveEventAttributes=e.resolveAttrAttributes=e.resolvePropAttributes=e.resolveHtmlAttribute=e.resolveWatchAttribute=e.resolveDataAttribute=e.resolveTextNodeExpressions=e.rebaseRelativePaths=e.resolveIncludes=void 0;const o=i(885),s=i(283),n=new Set([":bind",":bind-events",":data",":for",":show","@watch","$html"]),a={$text:"$text-content"};e.resolveIncludes=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o,s;const n=t;if("include"!==(null===(i=n.tagName)||void 0===i?void 0:i.toLocaleLowerCase()))return;this.log(e,"<include> tag found in:\n",t),this.log(e,"<include> params:",e);const a=null===(r=n.getAttribute)||void 0===r?void 0:r.call(n,"src");if(!a)throw new Error(`"src" attribute missing from ${t}.`);const l=e=>{t.replaceWith(...Array.from(e.childNodes))},c=Object.assign(Object.assign({},e),{root:!1,maxdepth:e.maxdepth-1});if(0===c.maxdepth)throw new Error("Maximum recursion depth reached.");if(a.includes("://")||a.startsWith("//"))this.log(e,"Including remote file from absolute path:",a),yield this.preprocessRemote(a,c).then(l);else if((null===(o=e.dirpath)||void 0===o?void 0:o.includes("://"))||(null===(s=e.dirpath)||void 0===s?void 0:s.startsWith("//"))){const t=e.dirpath&&"."!==e.dirpath?`${e.dirpath}/${a}`:a;this.log(e,"Including remote file from relative path:",t),yield this.preprocessRemote(t,c).then(l)}else if("/"===a.charAt(0))this.log(e,"Including local file from absolute path:",a),yield this.preprocessLocal(a,c).then(l);else{const t=e.dirpath&&"."!==e.dirpath?`${e.dirpath}/${a}`:a;this.log(e,"Including local file from relative path:",t),yield this.preprocessLocal(t,c).then(l)}}))},e.rebaseRelativePaths=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o,n,a,l,c;const h=t,u=null===(i=h.tagName)||void 0===i?void 0:i.toLowerCase();if(!e.dirpath)return;const d=null===(o=(r=t).getAttribute)||void 0===o?void 0:o.call(r,"src"),v=null===(a=(n=t).getAttribute)||void 0===a?void 0:a.call(n,"href"),f=null===(c=(l=t).getAttribute)||void 0===c?void 0:c.call(l,"data"),p=d||v||f;p&&(p&&(0,s.isRelativePath)(p)&&this.log(e,"Rebasing relative path as:",e.dirpath,"/",p),"img"===u&&d&&(0,s.isRelativePath)(d)?h.src=`${e.dirpath}/${d}`:"a"===u&&v&&(0,s.isRelativePath)(v)||"link"===u&&v&&(0,s.isRelativePath)(v)?h.href=`${e.dirpath}/${v}`:"script"===u&&d&&(0,s.isRelativePath)(d)||"source"===u&&d&&(0,s.isRelativePath)(d)||"audio"===u&&d&&(0,s.isRelativePath)(d)||"video"===u&&d&&(0,s.isRelativePath)(d)||"track"===u&&d&&(0,s.isRelativePath)(d)||"iframe"===u&&d&&(0,s.isRelativePath)(d)?h.src=`${e.dirpath}/${d}`:"object"===u&&f&&(0,s.isRelativePath)(f)?h.data=`${e.dirpath}/${f}`:"input"===u&&d&&(0,s.isRelativePath)(d)?h.src=`${e.dirpath}/${d}`:("area"===u&&v&&(0,s.isRelativePath)(v)||"base"===u&&v&&(0,s.isRelativePath)(v))&&(h.href=`${e.dirpath}/${v}`))}))},e.resolveTextNodeExpressions=function(t,e){return r(this,void 0,void 0,(function*(){if(3!==t.nodeType)return;const i=t.nodeValue||"",o=new RegExp(/{{ ([^}]+) }}/gm),s=Array.from(i.matchAll(o)).map((t=>t[1])),n=()=>r(this,void 0,void 0,(function*(){let r=i;for(const i of s){const o=yield this.eval(i,{$elem:t},e);r=r.replace(`{{ ${i} }}`,String(o))}t.nodeValue=r})),[a,l]=yield this.trace(n);this.log(e,i,"=>",a),this.watch(l,n)}))},e.resolveDataAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t,o=null===(i=r.getAttribute)||void 0===i?void 0:i.call(r,":data");if(o){this.log(e,":data attribute found in:\n",t),r.removeAttribute(":data");const i=yield this.eval(o,{$elem:t},e);this.log(e,":data",o,"=>",i),yield this.update(i)}}))},e.resolveWatchAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t,o=null===(i=r.getAttribute)||void 0===i?void 0:i.call(r,"@watch");if(o){this.log(e,"@watch attribute found in:\n",t),r.removeAttribute("@watch");const i=()=>this.eval(o,{$elem:t},e),[s,n]=yield this.trace(i);this.log(e,"@watch",o,"=>",s),this.watch(n,i)}}))},e.resolveHtmlAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,s=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,"$html");if(s){this.log(e,"$html attribute found in:\n",t),o.removeAttribute("$html");const i=this.clone(),n=()=>r(this,void 0,void 0,(function*(){const r=yield this.eval(s,{$elem:t},e),n=yield i.preprocessString(r,e);yield i.renderNode(n,e),o.replaceChildren(n)})),[a,l]=yield this.trace(n);this.log(e,"$html",s,"=>",a),this.watch(l,n)}}))},e.resolvePropAttributes=function(t,e){return r(this,void 0,void 0,(function*(){if(this.skipNodes.has(t))return;const i=t;for(const s of Array.from(i.attributes||[]))if(s.name.startsWith("$")&&!n.has(s.name)){this.log(e,s.name,"attribute found in:\n",t),i.removeAttribute(s.name);const n=(a[s.name]||s.name).slice(1),l=()=>this.eval(s.value,{$elem:t},e),[c,h]=yield this.trace(l);this.log(e,s.name,s.value,"=>",c,`[${h}]`);const u=(0,o.attributeNameToCamelCase)(n);this.watch(h,(()=>r(this,void 0,void 0,(function*(){return t[u]=yield l()})))),t[u]=c}}))},e.resolveAttrAttributes=function(t,e){return r(this,void 0,void 0,(function*(){if(this.skipNodes.has(t))return;const i=t;for(const o of Array.from(i.attributes||[]))if(o.name.startsWith(":")&&!n.has(o.name)){this.log(e,o.name,"attribute found in:\n",t),i.removeAttribute(o.name);const s=(a[o.name]||o.name).slice(1),n=()=>this.eval(o.value,{$elem:t},e),[l,c]=yield this.trace(n);this.log(e,o.name,o.value,"=>",l,`[${c}]`),this.watch(c,(()=>r(this,void 0,void 0,(function*(){return i.setAttribute(s,yield n())})))),i.setAttribute(s,l)}}))},e.resolveEventAttributes=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const r=t;for(const o of Array.from(r.attributes||[]))o.name.startsWith("@")&&!n.has(o.name)&&(this.log(e,o.name,"attribute found in:\n",t),r.removeAttribute(o.name),null===(i=t.addEventListener)||void 0===i||i.call(t,o.name.substring(1),(i=>this.eval(o.value,{$elem:t,$event:i},e))))}))},e.resolveForAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,n=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,":for");if(n){this.log(e,":for attribute found in:\n",t),o.removeAttribute(":for");for(const e of(0,s.traverse)(t,this.skipNodes))this.skipNodes.add(e);const i=t.parentNode,a=t.ownerDocument.createElement("template");i.insertBefore(a,t),a.append(t),this.log(e,":for template:\n",a);const l=n.split(" in ",2);if(2!==l.length)throw new Error(`Invalid :for format: \`${n}\`. Expected "{key} in {expression}".`);let c=[],h=[];const[u,d]=l;try{[c,h]=yield this.trace((()=>this.eval(d,{$elem:t},e))),this.log(e,d,"=>",c,`[${h}]`)}catch(t){return void console.error(t)}const v=[],f=o=>r(this,void 0,void 0,(function*(){if(this.log(e,":for list items:",o),Array.isArray(o))return this.lock=this.lock.then((()=>new Promise((s=>r(this,void 0,void 0,(function*(){v.splice(0,v.length).forEach((t=>{i.removeChild(t),this.skipNodes.delete(t)}));for(const r of o.slice(0).reverse()){const o=this.clone();yield o.set(u,r);const s=t.cloneNode(!0);i.insertBefore(s,a.nextSibling),v.push(s),this.skipNodes.add(s),yield o.mount(s,e),this.log(e,"Rendered list child:\n",s)}s()})))))),this.lock;console.error(`Expression did not yield a list: \`${d}\` => \`${o}\``)}));return this.watch(h,(()=>r(this,void 0,void 0,(function*(){return f(yield this.eval(d,{$elem:t},e))})))),f(c)}}))},e.resolveBindAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i,r,o;if(this.skipNodes.has(t))return;const s=t,n=null===(i=s.getAttribute)||void 0===i?void 0:i.call(s,":bind");if(n){this.log(e,":bind attribute found in:\n",t);const i=["change","input"],a=(null===(o=null===(r=s.getAttribute)||void 0===r?void 0:r.call(s,":bind-events"))||void 0===o?void 0:o.split(","))||i;s.removeAttribute(":bind"),s.removeAttribute(":bind-events");const l="checkbox"===s.getAttribute("type")?"checked":"value";this.has(n)||(yield this.set(n,s[l])),s[l]=this.get(n);for(const e of a)t.addEventListener(e,(()=>this.set(n,s[l])));this.watch([n],(()=>s[l]=this.get(n)))}}))},e.resolveShowAttribute=function(t,e){return r(this,void 0,void 0,(function*(){var i;if(this.skipNodes.has(t))return;const o=t,s=null===(i=o.getAttribute)||void 0===i?void 0:i.call(o,":show");if(s){this.log(e,":show attribute found in:\n",t),o.removeAttribute(":show");const i=()=>this.eval(s,{$elem:t},e),[n,a]=yield this.trace(i);this.log(e,":show",s,"=>",n,`[${a}]`);const l="none"===o.style.display?"":o.style.display;n||(o.style.display="none"),this.watch(a,(()=>r(this,void 0,void 0,(function*(){o.style.display=(yield i())?l:"none"}))))}}))}},63:function(t,e){var i=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,s){function n(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}l((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.proxify=e.ReactiveProxyStore=e.InertProxy=e.ReactiveProxy=e.proxifyObject=e.REACTIVE_DEBOUNCE_MILLIS=void 0;class r{constructor(){this.timeout=null}debounce(t,e){return new Promise(((i,r)=>{this.timeout&&clearTimeout(this.timeout),this.timeout=setTimeout((()=>{try{i(e())}catch(t){r(t)}}),t)}))}}function o(t,e,i=!0){if(null==t||function(t){return t instanceof s||t.__is_proxy__}(t))return t;if(i)for(const i in t)t.hasOwnProperty(i)&&"object"==typeof t[i]&&null!=t[i]&&(t[i]=o(t[i],e));return new Proxy(t,{deleteProperty:(t,i)=>i in t&&(delete t[i],e(),!0),set:(t,r,s,n)=>{i&&"object"==typeof s&&(s=o(s,e));const a=Reflect.set(t,r,s,n);return e(),a},get:(t,e,i)=>"__is_proxy__"===e||Reflect.get(t,e,i)})}e.REACTIVE_DEBOUNCE_MILLIS=25,e.proxifyObject=o;class s extends r{constructor(t=null,...e){super(),this.value=null,this.listeners=[],this.set(t),e.forEach((t=>this.watch(t)))}static from(t,...e){return t instanceof s?(e.forEach(t.watch),t):new s(t,...e)}get(){return this.value}set(t){return i(this,void 0,void 0,(function*(){if(this.value!==t){const e=this.value;null!=t&&"object"==typeof t&&(t=o(t,(()=>this.trigger()))),this.value=t,yield this.trigger(e)}}))}watch(t){this.listeners.push(t)}unwatch(t){this.listeners=this.listeners.filter((e=>e!==t))}trigger(){return i(this,arguments,void 0,(function*(t=null){yield this.debounce(e.REACTIVE_DEBOUNCE_MILLIS,(()=>Promise.all(this.listeners.map((e=>e(this.value,t))))))}))}}e.ReactiveProxy=s;class n extends s{static from(t,...e){return t instanceof s?t:new n(t,...e)}watch(t){}trigger(t){return Promise.resolve()}}function a(t){const e=Array.from(t.entries()).map((([t])=>t)),i=Object.fromEntries(e.map((t=>[t,void 0])));return new Proxy(Object.assign({},t,i),{get:(e,i,r)=>"string"==typeof i&&t.has(i)?t.get(i):Reflect.get(t,i,r),set:(e,i,r,o)=>("string"!=typeof i||i in t?Reflect.set(t,i,r,o):t.set(i,r),!0)})}e.InertProxy=n,e.ReactiveProxyStore=class extends r{wrapFnValue(t){return t&&"function"==typeof t?(...e)=>t.call(a(this),...e):t}constructor(t){super(),this.store=new Map,this.tracing=!1,this.traced=new Set,this.lock=Promise.resolve();for(const[e,i]of Object.entries(t||{}))this.store.set(e,s.from(this.wrapFnValue(i)))}get $(){return a(this)}entries(){return this.store.entries()}get(t){var e;return this.tracing&&this.traced.add(t),null===(e=this.store.get(t))||void 0===e?void 0:e.get()}set(t,e){return i(this,void 0,void 0,(function*(){this.store.has(t)?yield this.store.get(t).set(this.wrapFnValue(e)):this.store.set(t,s.from(this.wrapFnValue(e)))}))}del(t){return this.store.delete(t)}has(t){return this.store.has(t)}update(t){return i(this,void 0,void 0,(function*(){yield Promise.all(Object.entries(t).map((([t,e])=>this.set(t,e))))}))}watch(t,e){(t=Array.isArray(t)?t:[t]).forEach((i=>this.store.get(i).watch((()=>e(...t.map((t=>this.store.get(t).get())))))))}trigger(t){return i(this,void 0,void 0,(function*(){t=Array.isArray(t)?t:[t],yield Promise.all(t.map((t=>this.store.get(t).trigger())))}))}trace(t){return i(this,void 0,void 0,(function*(){yield this.lock;const e=new Promise(((e,r)=>i(this,void 0,void 0,(function*(){this.traced.clear(),this.tracing=!0;try{const i=yield t(),r=Array.from(this.traced);e([i,r])}catch(t){r(t)}finally{this.tracing=!1,this.traced.clear()}}))));return this.lock=e.then((()=>{})),e}))}computed(t,e){return i(this,void 0,void 0,(function*(){const[r,o]=yield this.trace((()=>e.call(a(this))));this.watch(o,(()=>i(this,void 0,void 0,(function*(){return this.set(t,yield e.call(a(this)))})))),this.set(t,r)}))}},e.proxify=a}},e={};!function i(r){var o=e[r];if(void 0!==o)return o.exports;var s=e[r]={exports:{}};return t[r].call(s.exports,s,s.exports,i),s.exports}(246)})();
|
package/dist/reactive.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
type Listener<T> = (curr: T | null, prev: T | null) => any | Promise<any>;
|
|
2
|
+
declare abstract class IDebouncer {
|
|
3
|
+
timeout: ReturnType<typeof setTimeout> | null;
|
|
4
|
+
debounce<T>(millis: number, callback: () => T): Promise<T>;
|
|
5
|
+
}
|
|
6
|
+
/** Default debouncer time in millis. */
|
|
7
|
+
export declare const REACTIVE_DEBOUNCE_MILLIS = 25;
|
|
2
8
|
export declare function proxifyObject<T extends object>(object: T, callback: () => void, deep?: boolean): T;
|
|
3
|
-
export declare class ReactiveProxy<T> {
|
|
9
|
+
export declare class ReactiveProxy<T> extends IDebouncer {
|
|
4
10
|
private value;
|
|
5
11
|
private listeners;
|
|
6
12
|
protected constructor(value?: T | null, ...listeners: Listener<T>[]);
|
|
@@ -13,10 +19,10 @@ export declare class ReactiveProxy<T> {
|
|
|
13
19
|
}
|
|
14
20
|
export declare class InertProxy<T> extends ReactiveProxy<T> {
|
|
15
21
|
static from<T>(value: T | InertProxy<T>, ...listeners: Listener<T>[]): InertProxy<T>;
|
|
16
|
-
watch(
|
|
17
|
-
trigger(
|
|
22
|
+
watch(_: Listener<T>): void;
|
|
23
|
+
trigger(_?: T | null): Promise<void>;
|
|
18
24
|
}
|
|
19
|
-
export declare class ReactiveProxyStore {
|
|
25
|
+
export declare class ReactiveProxyStore extends IDebouncer {
|
|
20
26
|
protected readonly store: Map<string, ReactiveProxy<any>>;
|
|
21
27
|
protected tracing: boolean;
|
|
22
28
|
protected readonly traced: Set<string>;
|
package/dist/reactive.js
CHANGED
|
@@ -9,10 +9,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.proxify = exports.ReactiveProxyStore = exports.InertProxy = exports.ReactiveProxy = exports.proxifyObject = void 0;
|
|
12
|
+
exports.proxify = exports.ReactiveProxyStore = exports.InertProxy = exports.ReactiveProxy = exports.proxifyObject = exports.REACTIVE_DEBOUNCE_MILLIS = void 0;
|
|
13
|
+
class IDebouncer {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.timeout = null;
|
|
16
|
+
}
|
|
17
|
+
debounce(millis, callback) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (this.timeout)
|
|
20
|
+
clearTimeout(this.timeout);
|
|
21
|
+
this.timeout = setTimeout(() => {
|
|
22
|
+
try {
|
|
23
|
+
resolve(callback());
|
|
24
|
+
}
|
|
25
|
+
catch (exc) {
|
|
26
|
+
reject(exc);
|
|
27
|
+
}
|
|
28
|
+
}, millis);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
13
32
|
function isProxified(object) {
|
|
14
33
|
return object instanceof ReactiveProxy || object["__is_proxy__"];
|
|
15
34
|
}
|
|
35
|
+
/** Default debouncer time in millis. */
|
|
36
|
+
exports.REACTIVE_DEBOUNCE_MILLIS = 25;
|
|
16
37
|
function proxifyObject(object, callback, deep = true) {
|
|
17
38
|
// If this object is already a proxy, return it as-is.
|
|
18
39
|
if (object == null || isProxified(object))
|
|
@@ -52,11 +73,11 @@ function proxifyObject(object, callback, deep = true) {
|
|
|
52
73
|
});
|
|
53
74
|
}
|
|
54
75
|
exports.proxifyObject = proxifyObject;
|
|
55
|
-
class ReactiveProxy {
|
|
76
|
+
class ReactiveProxy extends IDebouncer {
|
|
56
77
|
constructor(value = null, ...listeners) {
|
|
78
|
+
super();
|
|
57
79
|
this.value = null;
|
|
58
80
|
this.listeners = [];
|
|
59
|
-
// this.value = value;
|
|
60
81
|
this.set(value);
|
|
61
82
|
listeners.forEach((x) => this.watch(x));
|
|
62
83
|
}
|
|
@@ -93,10 +114,7 @@ class ReactiveProxy {
|
|
|
93
114
|
}
|
|
94
115
|
trigger() {
|
|
95
116
|
return __awaiter(this, arguments, void 0, function* (prev = null) {
|
|
96
|
-
|
|
97
|
-
for (const listener of this.listeners) {
|
|
98
|
-
yield listener(this.value, prev);
|
|
99
|
-
}
|
|
117
|
+
yield this.debounce(exports.REACTIVE_DEBOUNCE_MILLIS, () => Promise.all(this.listeners.map((x) => x(this.value, prev))));
|
|
100
118
|
});
|
|
101
119
|
}
|
|
102
120
|
}
|
|
@@ -110,13 +128,13 @@ class InertProxy extends ReactiveProxy {
|
|
|
110
128
|
return new InertProxy(value, ...listeners);
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
|
-
watch(
|
|
114
|
-
trigger(
|
|
131
|
+
watch(_) { }
|
|
132
|
+
trigger(_) {
|
|
115
133
|
return Promise.resolve();
|
|
116
134
|
}
|
|
117
135
|
}
|
|
118
136
|
exports.InertProxy = InertProxy;
|
|
119
|
-
class ReactiveProxyStore {
|
|
137
|
+
class ReactiveProxyStore extends IDebouncer {
|
|
120
138
|
wrapFnValue(value) {
|
|
121
139
|
if (!value || typeof value !== "function")
|
|
122
140
|
return value;
|
|
@@ -124,6 +142,7 @@ class ReactiveProxyStore {
|
|
|
124
142
|
return (...args) => value.call(proxify(this), ...args);
|
|
125
143
|
}
|
|
126
144
|
constructor(data) {
|
|
145
|
+
super();
|
|
127
146
|
this.store = new Map();
|
|
128
147
|
this.tracing = false;
|
|
129
148
|
this.traced = new Set();
|
|
@@ -166,10 +185,7 @@ class ReactiveProxyStore {
|
|
|
166
185
|
*/
|
|
167
186
|
update(data) {
|
|
168
187
|
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
|
|
170
|
-
for (const [key, value] of Object.entries(data)) {
|
|
171
|
-
yield this.set(key, value);
|
|
172
|
-
}
|
|
188
|
+
yield Promise.all(Object.entries(data).map(([key, value]) => this.set(key, value)));
|
|
173
189
|
});
|
|
174
190
|
}
|
|
175
191
|
watch(keys, listener) {
|
|
@@ -181,12 +197,8 @@ class ReactiveProxyStore {
|
|
|
181
197
|
}
|
|
182
198
|
trigger(keys) {
|
|
183
199
|
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
yield this.store.get(key).trigger();
|
|
187
|
-
}
|
|
188
|
-
// keys = Array.isArray(keys) ? keys : [keys];
|
|
189
|
-
// return Promise.all(keys.map((key) => this.store.get(key)!!.trigger())).then();
|
|
200
|
+
keys = Array.isArray(keys) ? keys : [keys];
|
|
201
|
+
yield Promise.all(keys.map((key) => this.store.get(key).trigger()));
|
|
190
202
|
});
|
|
191
203
|
}
|
|
192
204
|
trace(callback) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mancha",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Javscript HTML rendering engine",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@types/gulp": "^4.0.8",
|
|
46
46
|
"@types/jsdom": "^21.1.6",
|
|
47
47
|
"@types/mocha": "^10.0.3",
|
|
48
|
-
"@types/node": "^20.
|
|
48
|
+
"@types/node": "^20.12.11",
|
|
49
49
|
"@types/path-browserify": "^1.0.1",
|
|
50
50
|
"@types/through2": "^2.0.36",
|
|
51
51
|
"@types/yargs": "^17.0.29",
|