UNPKG

apprun

Version:

JavaScript library that has Elm inspired architecture, event pub-sub and components

2 lines 8.1 kB
!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,(function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var s=t[o]={i:o,l:!1,exports:{}};return e[o].call(s.exports,s,s.exports,n),s.l=!0,s.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)n.d(o,s,function(t){return e[t]}.bind(null,s));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=8)}({1:function(e,t,n){"use strict";(function(e){n.d(t,"a",(function(){return o}));class o{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._events[e]||[];return console.assert(n&&n.length>0,"No subscriber for event: "+e),this._events[e]=n.filter(e=>!e.options.once),n.forEach(n=>{const{fn:o,options:s}=n;return s.delay?this.delay(e,o,t,s):o.apply(this,t),!n.options.once}),n.length}once(e,t,n={}){this.on(e,t,Object.assign(Object.assign({},n),{once:!0}))}delay(e,t,n,o){o._t&&clearTimeout(o._t),o._t=setTimeout(()=>{clearTimeout(o._t),t.apply(this,n)},o.delay)}}let s;const r="object"==typeof self&&self.self===self&&self||"object"==typeof e&&e.global===e&&e;r.app&&r._AppRunVersions?s=r.app:(s=new o,r.app=s,r._AppRunVersions="AppRun-2"),t.b=s}).call(this,n(3))},3:function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},8:function(e,t,n){"use strict";n.r(t);var o=n(1);function s(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("")}function r(e){return e.map(e=>l(e)).join("")}function l(e){if(!e)return"";if(function e(t){for(var n in t)null==t[n]?delete t[n]:"object"==typeof t[n]&&e(t[n])}(e),Array.isArray(e))return r(e);if("string"==typeof e)return e.startsWith("_html:")?e.substring(6):e;if(e.tag){const t=e.props?s(e.props):"",n=e.children?r(e.children):"";return`<${e.tag}${t}>${n}</${e.tag}>`}return"object"==typeof e?JSON.stringify(e):void 0}var 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;let f=!1,m=[];app.on("debug",e=>{f&&e.vdom&&(m.push(e),console.log(`* ${m.length} state(s) recorded.`))});const d=e=>{"start"===e?(m=[],f=!0,console.log("* State logging started.")):"stop"===e?(0!==m.length?(a(""),m.forEach((e,t)=>{p(` it ('view snapshot: #${t+1}', ()=>{`),p(` const component = ${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."),f=!1,m=[],console.log("* State logging stopped.")):console.log("create-state-tests <start|stop>")};function b(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.b.debug=!0,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 v=()=>{const e={components:{}};o.b.run("get-components",e);const{components:t}=e;return t},y=e=>{const t=o.b._events,n={},s=v();Object.keys(s).forEach(e=>{s[e].forEach(e=>{e._actions.forEach(t=>{n[t.name]=n[t.name]||[],n[t.name].push(e)})})});const r=[];if(Object.keys(n).forEach(e=>{r.push({event:e,components:n[e],global:!!t[e]})}),r.sort((e,t)=>e.event>t.event?1:-1).map(e=>e.event),e){const e=(e=>{const t=({components:e})=>o.b.createElement("ul",null,e.map(e=>o.b.createElement("li",null,o.b.createElement("div",null,e.constructor.name)))),n=({events:e,global:n})=>o.b.createElement("ul",null,e&&e.filter(e=>e.global===n).map(({event:e,components:n})=>o.b.createElement("li",null,o.b.createElement("div",null,e),o.b.createElement(t,{components:n}))));return o.b.createElement("div",null,o.b.createElement("div",null,"GLOBAL EVENTS"),o.b.createElement(n,{events:e,global:!0}),o.b.createElement("div",null,"LOCAL EVENTS"),o.b.createElement(n,{events:e,global:!1}))})(r);b(c(e))}else console.log("=== GLOBAL EVENTS ==="),r.filter(e=>e.global).forEach(({event:e,components:t})=>console.log({event:e},t)),console.log("=== LOCAL EVENTS ==="),r.filter(e=>!e.global).forEach(({event:e,components:t})=>console.log({event:e},t))},g=e=>{const t=v(),n=[];if(Object.keys(t).forEach(e=>{const o="string"==typeof e?document.getElementById(e):e,s=t[e].map(e=>({component:e,events:e._actions}));n.push({element:o,comps:s})}),e){const e=(e=>{const t=({events:e})=>o.b.createElement("ul",null,e&&e.map(e=>o.b.createElement("li",null,e.name))),n=({components:e})=>o.b.createElement("ul",null,e.map(e=>o.b.createElement("li",null,o.b.createElement("div",null,e.constructor.name),o.b.createElement(t,{events:e._actions}))));return o.b.createElement("ul",null,Object.keys(e).map(t=>o.b.createElement("li",null,o.b.createElement("div",null,"#",t),o.b.createElement(n,{components:e[t]}))))})(t);b(c(e))}else n.forEach(({element:e,comps:t})=>console.log(e,t))};let h=0;o.b.on("debug",e=>{1&h&&e.event&&console.log(e),2&h&&e.vdom&&console.log(e)}),window["_apprun-components"]=["components [print]",e=>{g("print"===e)}],window["_apprun-events"]=["events [print]",e=>{y("print"===e)}],window["_apprun-log"]=["log [event|view] on|off",(e,t)=>{"on"===e?h=3:"off"===e?h=0:"event"===e?"on"===t?h|=1:"off"===t&&(h&=-2):"view"===e&&("on"===t?h|=2:"off"===t&&(h&=-3)),console.log(`* log ${e} ${t||""}`)}],window["_apprun-create-event-tests"]=["create-event-tests",()=>(()=>{const e={components:{}};app.run("get-components",e);const{components:t}=e;a(""),Object.keys(t).forEach(e=>{t[e].forEach(e=>{p(`const component = ${e.constructor.name};`),p(`describe('${e.constructor.name}', ()=>{`),e._actions.forEach(e=>{p(` it ('should handle event: ${e.name}', ()=>{`),p(` component.run('${e.name}');`),p(" expect(component.state).toBeTruthy();"),p(" })")}),p("});")})}),u()})()],window["_apprun-create-state-tests"]=["create-state-tests <start|stop>",e=>d(e)],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 0.4: type "_apprun `help`" to list all available commands.');if(window.__REDUX_DEVTOOLS_EXTENSION__){let e=!1;const t=window.__REDUX_DEVTOOLS_EXTENSION__.connect();t&&(console.info("Connected to the Redux DevTools"),t.subscribe(t=>{"START"===t.type?e=!0:"STOP"===t.type?e=!1:t.type}),o.b.on("debug",n=>{if(e&&n.event){const e=n.newState,o={type:n.event,payload:n.e};e instanceof Promise?e.then(e=>t.send(o,e)):t.send(o,e)}}))}}})})); //# sourceMappingURL=apprun-dev-tools.js.map