apprun
Version:
JavaScript library that has Elm inspired architecture, event pub-sub and components
2 lines • 9.95 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.apprun=t():e.apprun=t()}(this,(()=>(()=>{"use strict";var e={672:(e,t,n)=>{n.d(t,{C:()=>o});const o="AppRun-3.38.0"},859:(e,t,n)=>{n.d(t,{A:()=>c});var o=n(672);const s=o.C;let r;const l="undefined"!=typeof window?window:void 0!==n.g?n.g:"undefined"!=typeof self?self:{};l.app&&l._AppRunVersions?r=l.app:(r=new class{constructor(){this._events={}}on(e,t,n={}){this._events[e]=this._events[e]||[],this._events[e].push({fn:t,options:n})}off(e,t){const n=this._events[e]||[];this._events[e]=n.filter((e=>e.fn!==t))}find(e){return this._events[e]}run(e,...t){const n=this.getSubscribers(e,this._events);return console.assert(n&&n.length>0,"No subscriber for event: "+e),n.forEach((n=>{const{fn:o,options:s}=n;if(!o||"function"!=typeof o)return console.error(`AppRun event handler for '${e}' is not a function:`,o),!1;if(s.delay)this.delay(e,o,t,s);else try{Object.keys(s).length>0?o.apply(this,[...t,s]):o.apply(this,t)}catch(t){console.error(`Error in event handler for '${e}':`,t)}return!n.options.once})),n.length}once(e,t,n={}){this.on(e,t,{...n,once:!0})}delay(e,t,n,o){o._t&&clearTimeout(o._t),o._t=setTimeout((()=>{clearTimeout(o._t);try{Object.keys(o).length>0?t.apply(this,[...n,o]):t.apply(this,n)}catch(t){console.error(`Error in delayed event handler for '${e}':`,t)}}),o.delay)}runAsync(e,...t){const n=this.getSubscribers(e,this._events);console.assert(n&&n.length>0,"No subscriber for event: "+e);const o=n.map((n=>{const{fn:o,options:s}=n;if(!o||"function"!=typeof o)return console.error(`AppRun async event handler for '${e}' is not a function:`,o),Promise.resolve(null);try{return Object.keys(s).length>0?o.apply(this,[...t,s]):o.apply(this,t)}catch(t){return console.error(`Error in async event handler for '${e}':`,t),Promise.reject(t)}}));return Promise.all(o)}query(e,...t){return console.warn("app.query() is deprecated. Use app.runAsync() instead."),this.runAsync(e,...t)}getSubscribers(e,t){const n=t[e]||[];return t[e]=n.filter((e=>!e.options.once)),Object.keys(t).filter((t=>t.endsWith("*")&&e.startsWith(t.replace("*","")))).sort(((e,t)=>t.length-e.length)).forEach((o=>n.push(...t[o].map((t=>({...t,options:{...t.options,event:e}})))))),n}},l.app=r,l._AppRunVersions=s);const c=r}},t={};function n(o){var s=t[o];if(void 0!==s)return s.exports;var r=t[o]={exports:{}};return e[o](r,r.exports,n),r.exports}n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var o=n(859);function s(e){return e.map((e=>l(e))).join("")}function r(e){for(var t in e)null==e[t]?delete e[t]:"object"==typeof e[t]&&r(e[t])}function l(e){if(!e)return"";if(e._$litType$)return e.toString();if(r(e),Array.isArray(e))return s(e);if("string"==typeof e)return e.startsWith("_html:")?e.substring(6):e;if(e.tag){const t=e.props?function(e){return Object.keys(e).map((t=>{return` ${"className"===t?"class":t}="${n=e[t],"object"==typeof n?Object.keys(n).map((e=>`${e}:${n[e]}`)).join(";"):n.toString()}"`;var n})).join("")}(e.props):"",n=e.children?s(e.children):"";return`<${e.tag}${t}>${n}</${e.tag}>`}return"object"==typeof e?JSON.stringify(e):void 0}const c=l;let i;function a(e){i=window.open("",e),i.document.write(`<html>\n <title>AppRun Analyzer | ${document.location.href}</title>\n <style>\n body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI" }\n </style>\n <body><pre>`)}function p(e){i.document.write(e+"\n")}function u(){i.document.write("</pre>\n </body>\n </html>"),i.document.close()}app.debug=!0;const f=e=>{p(`import ${e.constructor.name} from '../src/${e.constructor.name}'`),p(`describe('${e.constructor.name}', ()=>{`),e._actions.forEach((t=>{"."!==t.name&&(p(` it ('should handle event: ${t.name}', (done)=>{`),p(` const component = new ${e.constructor.name}().mount();`),p(` component.run('${t.name}');`),p(" setTimeout(() => {"),p(" //expect(?).toHaveBeenCalled();"),p(" //expect(component.state).toBe(?);"),p(" done();"),p(" })"))})),p("});")};let d=!1,h=[];app.on("debug",(e=>{d&&e.vdom&&(h.push(e),console.log(`* ${h.length} state(s) recorded.`))}));function m(e){const t={get(e,t,n){const o=Reflect.get(e,t,n);return"object"==typeof o&&null!==o?Array.isArray(o)?o.map((e=>m(e))):m(o):`{${t}}`}};return Array.isArray(e)?e.map((e=>m(e))):new Proxy(e,t)}function g(e){let t;if("object"==typeof e.state){const n=m(e.state);t=e.view(n)}else t=e.view(e.state);return t}const y=new Map;function v(e){const t=window.open("","_apprun_debug","toolbar=0");t.document.write(`<html>\n <title>AppRun Analyzer | ${document.location.href}</title>\n <style>\n body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI" }\n li { margin-left: 80px; }\n </style>\n <body>\n <div id="main">${e}</div>\n <\/script>\n </body>\n </html>`),t.document.close()}o.A.debug=!0,o.A.on("debug-create-component",(e=>{const t=e.element;t?y.get(t)?y.get(t).push(e):y.set(t,[e]):console.warn("Component created without an element:",e)})),window["_apprun-help"]=["",()=>{Object.keys(window).forEach((e=>{e.startsWith("_apprun-")&&("_apprun-help"===e?console.log("AppRun Commands:"):console.log(`* ${e.substring(8)}: ${window[e][0]}`))}))}];const b=e=>{const t=y,n=[];if(t instanceof Map)for(let[e,o]of t){const t="string"==typeof e?document.getElementById(e)||document.querySelector(e):e;n.push({element:t,comps:o})}if(e){const e=(e=>{const t=({components:e})=>o.A.h("ul",null,e.map((e=>{const t=g(e),n=e._actions.map((e=>e.name)),s={state:e.state,view:t,actions:n,update:e.update};return o.A.h("li",null,o.A.h("div",null,e.constructor.name),o.A.h("div",null,o.A.h("pre",null,(r=JSON.stringify(s,null,2))?r.toString().replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">"):r)),o.A.h("br",null));var r})));return o.A.h("ul",null,e.map((({element:e,comps:n})=>o.A.h("li",null,o.A.h("div",null,(e=>o.A.h("div",null,e.tagName.toLowerCase(),e.id?"#"+e.id:""," ",e.className&&e.className.split(" ").map((e=>"."+e)).join()))(e)),o.A.h(t,{components:n})))))})(n);v(c(e))}else n.forEach((({element:e,comps:t})=>console.log(e,t)))};let w=Number(window?.localStorage?.getItem("__apprun_debugging__"))||0;if(o.A.on("debug",(e=>{1&w&&e.event&&console.log(e),2&w&&e.vdom&&console.log(e)})),window["_apprun-components"]=["components [print|clear]",e=>{"print"===e?b(!0):"clear"===e?(y.clear(),console.log("Component cache cleared")):b(!1)}],window["_apprun-events"]=["events [print]",e=>{(e=>{const t=o.A._events,n={},s=y,r=e=>e._actions.forEach((t=>{n[t.name]=n[t.name]||[],n[t.name].push(e)}));if(s instanceof Map)for(let[e,t]of s)t.forEach(r);const l=[];if(Object.keys(n).forEach((e=>{l.push({event:e,components:n[e],global:!!t[e]})})),l.sort(((e,t)=>e.event>t.event?1:-1)).map((e=>e.event)),e){const e=(e=>{const t=({components:e})=>o.A.h("ul",null,e.map((e=>o.A.h("li",null,o.A.h("div",null,e.constructor.name))))),n=({events:e,global:n})=>o.A.h("ul",null,e&&e.filter((e=>e.global===n&&"."!==e.event)).map((({event:e,components:n})=>o.A.h("li",null,o.A.h("div",null,e),o.A.h(t,{components:n})))));return o.A.h("div",null,o.A.h("div",null,"GLOBAL EVENTS"),o.A.h(n,{events:e,global:!0}),o.A.h("div",null,"LOCAL EVENTS"),o.A.h(n,{events:e,global:!1}))})(l);v(c(e))}else console.log("=== GLOBAL EVENTS ==="),l.filter((e=>e.global&&"."!==e.event)).forEach((({event:e,components:t})=>console.log({event:e},t))),console.log("=== LOCAL EVENTS ==="),l.filter((e=>!e.global&&"."!==e.event)).forEach((({event:e,components:t})=>console.log({event:e},t)))})("print"===e)}],window["_apprun-log"]=["log [event|view] on|off",(e,t)=>{"on"===e?w=3:"off"===e?w=0:"event"===e?"on"===t?w|=1:"off"===t&&(w&=-2):"view"===e&&("on"===t?w|=2:"off"===t&&(w&=-3)),console.log(`* log ${e} ${t||""}`),window?.localStorage?.setItem("__apprun_debugging__",`${w}`)}],window["_apprun-create-event-tests"]=["create-event-tests",()=>(()=>{const e={components:{}};app.run("get-components",e);const{components:t}=e;if(a(""),t instanceof Map)for(let[e,n]of t)n.forEach(f);else Object.keys(t).forEach((e=>{t[e].forEach(f)}));u()})()],window["_apprun-create-state-tests"]=["create-state-tests <start|stop>",e=>{var t;"start"===(t=e)?(h=[],d=!0,console.log("* State logging started.")):"stop"===t?(0!==h.length?(a(""),h.forEach(((e,t)=>{p(` it ('view snapshot: #${t+1}', ()=>{`),p(` const component = new ${e.component.constructor.name}()`),p(` const state = ${JSON.stringify(e.state,void 0,2)};`),p(" const vdom = component['view'](state);"),p(" expect(JSON.stringify(vdom)).toMatchSnapshot();"),p(" })")})),u()):console.log("* No state recorded."),d=!1,h=[],console.log("* State logging stopped.")):console.log("create-state-tests <start|stop>")}],window._apprun=e=>{const[t,...n]=e[0].split(" ").filter((e=>!!e)),o=window[`_apprun-${t}`];o?o[1](...n):window["_apprun-help"][1]()},console.info('AppRun DevTools 3.36: type "_apprun `help`" to list all available commands.'),window.__REDUX_DEVTOOLS_EXTENSION__){let e=!1;const t=window.__REDUX_DEVTOOLS_EXTENSION__.connect();if(t){const n=location.hash||"#";t.send(n,"");const s=[{component:null,state:""}];console.info("Connected to the Redux DevTools"),t.subscribe((t=>{if("START"===t.type)e=!0;else if("STOP"===t.type)e=!1;else if("DISPATCH"===t.type){const e=t.payload.index;if(0===e)o.A.run(n);else{const{component:t,state:n}=s[e];t?.setState(n)}}}));const r=(e,n,o)=>{null!=o&&(s.push({component:e,state:o}),t.send(n,o))};o.A.on("debug",(t=>{if(e&&t.event){const e=t.newState,n={type:t.event,payload:t.p},o=t.component;e instanceof Promise?e.then((e=>r(o,n,e))):r(o,n,e)}}))}}return{}})()));
//# sourceMappingURL=apprun-dev-tools.js.map