arcs.min.js 18.4 KB
/*! For license information please see arcs.min.js.LICENSE.txt */
var t={d:(n,e)=>{for(var o in e)t.o(e,o)&&!t.o(n,o)&&Object.defineProperty(n,o,{enumerable:!0,get:e[o]})},o:(t,n)=>Object.prototype.hasOwnProperty.call(t,n)},n={};t.d(n,{Z:()=>S});class e{constructor(t,n){this.slots=[],this.signals={},void 0!==t&&this.slot(t),void 0!==n&&this.signal(n)}slotList(){return this.slots}signalList(){return Object.keys(this.signals)}emit(t){let n,e,o,i=Array.prototype.slice.call(arguments,1);for(n in this.signals[t])e=this.signals[t][n].func,o=this.signals[t][n].obj,e.apply(o,i)}slot(t,n){let e=this;t instanceof Array?t.forEach((t=>{e.slots.includes(t)||e.slot(t)})):(this.slots.includes(t)||this.slots.push(t),void 0!==n&&(this[t]=n))}signal(t){let n=this;t instanceof Array?t.forEach((t=>{n.signals[t]=[]})):this.signals[t]=[]}static create(t,n,e){return void 0===t.prototype?(console.error("Cannot create such a component"),0):(t.prototype.slots=[],t.prototype.signals={},t.slotList=function(){return t.prototype.slots},t.prototype.slotList=function(){return this.slots},t.prototype.signalList=function(){return Object.keys(this.signals)},t.signalList=function(){return t.prototype.signalList()},t.prototype.emit=function(t){var n,e,o,i=Array.prototype.slice.call(arguments,1);for(n in this.signals[t])e=this.signals[t][n].func,o=this.signals[t][n].obj,e.apply(o,i)},t.prototype.slot=function(n,e){this.hasOwnProperty("slots")||(this.slots=t.prototype.slots.map((t=>t))),this.slots.includes(n)||this.slots.push(n),this[n]=e},t.prototype.signal=function(n){this.hasOwnProperty("signals")||(this.signals={},Object.assign(this.signals,t.prototype.signals)),this.signals[n]=[]},t.slot=function(n,e){var o;if(n instanceof Array)for(o=0;o<n.length;o++)t.prototype.slots.includes(n[o])||t.prototype.slots.push(n[o]);else t.prototype.slots.includes(n)||t.prototype.slots.push(n),void 0!==e&&(t.prototype[n]=e)},t.signal=function(n){var e;if(n instanceof Array)for(e=0;e<n.length;e++)t.prototype.signals[n[e]]=[];else t.prototype.signals[n]=[]},void 0!==n&&t.slot(n),void 0!==e&&t.signal(e),t)}static check(t){return void 0!==t.prototype&&void 0!==t.prototype.signals&&void 0!==t.prototype.slots}static connect(t,n,o,i){var s,r;if(void 0===t.signals)throw e.SourceIsNotComponent;if(void 0===t.signals[n]){let t=Object.assign({},e.UndefinedSignal);throw t.message=t.message+': "'+n+'"',t}if(void 0===o[i]){let t=Object.assign({},e.UndefinedSlot);throw t.message=t.message+': "'+i+'"',t}if(!t.hasOwnProperty("signals"))for(r in s=t.signals,t.signals={},s)t.signals[r]=[];t.signals[n].push({obj:o,func:o[i]})}static disconnect(t,n,e,o){var i;for(i=0;i<t.signals[n].length;i++)t.signals[n][i].obj===e&&t.signals[n][i].func===e[o]&&(t.signals[n].splice(i,1),i--)}static invoke(t,n,o){if(void 0===t[n])throw e.UndefinedSlot;t[n].apply(t,o)}static config(t,n){"function"==typeof t.config&&t.config(n)}}e.SourceIsNotComponent={message:"Source is not a component"},e.UndefinedSignal={message:"Signal is not defined"},e.UndefinedSlot={message:"Slot is not defined"};const o=e,i=function(t,n){this.eval=function(){return t[n]}},s=function(t,n,e){this.eval=function(){return n.eval(t)||e.eval(t)}},r=function(t,n,e){this.eval=function(t){return n.eval(t)&&e.eval(t)}};let a={build:function(t,n){var e,o,c,l;if("string"==typeof t)return n[t]=n[t]??!1,new i(n,t);for(e=a.build(t[0],n),l=1;l<t.length;l++)t[l].hasOwnProperty("and")?(c=a.build(t[l].and,n),o=new r(n,e,c)):t[l].hasOwnProperty("or")?(c=a.build(t[l].or,n),o=new s(n,e,c)):console.warn("[ARCS] Illegal tree"),e=o;return e}};const c=a,l=function(){function t(t,n,e,o,i,s){this.message=t,this.expected=n,this.found=e,this.offset=o,this.line=i,this.column=s,this.name="SyntaxError"}return function(t,n){function e(){this.constructor=t}e.prototype=n.prototype,t.prototype=new e}(t,Error),{SyntaxError:t,parse:function(n){var e,o,i,s,r,a,c=arguments.length>1?arguments[1]:{},l={},u={start:q},f=q,h=l,p=function(t,n){return[t].concat(n)},d=function(t){return{and:t}},g=function(t){return{or:t}},v=function(t){return t},y="(",m={type:"literal",value:"(",description:'"("'},C=")",w={type:"literal",value:")",description:'")"'},S=function(t){return[t]},b={type:"other",description:"id"},A=function(t){return t.trim()},O=/^[_a-zA-Z]/,x={type:"class",value:"[_a-zA-Z]",description:"[_a-zA-Z]"},P=/^[_a-zA-Z0-9]/,T={type:"class",value:"[_a-zA-Z0-9]",description:"[_a-zA-Z0-9]"},k="&",R={type:"literal",value:"&",description:'"&"'},j="|",N={type:"literal",value:"|",description:'"|"'},F=/^[ \r\n\t]/,E={type:"class",value:"[ \\r\\n\\t]",description:"[ \\r\\n\\t]"},_=0,L=0,I={line:1,column:1,seenCR:!1},U=0,Z=[],z=0;if("startRule"in c){if(!(c.startRule in u))throw new Error("Can't start parsing from rule \""+c.startRule+'".');f=u[c.startRule]}function D(t){_<U||(_>U&&(U=_,Z=[]),Z.push(t))}function q(){return J()}function J(){var t,n,e,o;if(t=_,(n=W())!==l){if(e=[],(o=V())!==l)for(;o!==l;)e.push(o),o=V();else e=h;e!==l?t=n=p(n,e):(_=t,t=h)}else _=t,t=h;return t===l&&(t=W()),t}function V(){var t,e,o;return t=_,e=function(){var t,e,o,i;return t=_,(e=M())!==l?(38===n.charCodeAt(_)?(o=k,_++):(o=l,0===z&&D(R)),o!==l&&(i=M())!==l?t=e=[e,o,i]:(_=t,t=h)):(_=t,t=h),t}(),e!==l&&(o=W())!==l?t=e=d(o):(_=t,t=h),t===l&&(t=_,e=function(){var t,e,o,i;return t=_,(e=M())!==l?(124===n.charCodeAt(_)?(o=j,_++):(o=l,0===z&&D(N)),o!==l&&(i=M())!==l?t=e=[e,o,i]:(_=t,t=h)):(_=t,t=h),t}(),e!==l&&(o=W())!==l?t=e=g(o):(_=t,t=h)),t}function W(){var t,e,o,i;return t=_,e=function(){var t,e,o;return z++,t=_,e=_,o=function(){var t,e,o,i,s;if(t=_,(e=M())!==l)if(O.test(n.charAt(_))?(o=n.charAt(_),_++):(o=l,0===z&&D(x)),o!==l){for(i=[],P.test(n.charAt(_))?(s=n.charAt(_),_++):(s=l,0===z&&D(T));s!==l;)i.push(s),P.test(n.charAt(_))?(s=n.charAt(_),_++):(s=l,0===z&&D(T));i!==l&&(s=M())!==l?t=e=[e,o,i,s]:(_=t,t=h)}else _=t,t=h;else _=t,t=h;return t}(),o!==l&&(o=n.substring(e,_)),(e=o)!==l&&(e=A(e)),z--,(t=e)===l&&(e=l,0===z&&D(b)),t}(),e!==l&&(e=v(e)),(t=e)===l&&(t=_,40===n.charCodeAt(_)?(e=y,_++):(e=l,0===z&&D(m)),e!==l&&(o=J())!==l?(41===n.charCodeAt(_)?(i=C,_++):(i=l,0===z&&D(w)),i!==l?t=e=S(o):(_=t,t=h)):(_=t,t=h)),t}function M(){var t,e;for(t=[],F.test(n.charAt(_))?(e=n.charAt(_),_++):(e=l,0===z&&D(E));e!==l;)t.push(e),F.test(n.charAt(_))?(e=n.charAt(_),_++):(e=l,0===z&&D(E));return t}if((e=f())!==l&&_===n.length)return e;throw e!==l&&_<n.length&&D({type:"end",description:"end of input"}),o=null,i=Z,r=function(t){return L!==t&&(L>t&&(L=0,I={line:1,column:1,seenCR:!1}),function(t,e,o){var i,s;for(i=e;i<o;i++)"\n"===(s=n.charAt(i))?(t.seenCR||t.line++,t.column=1,t.seenCR=!1):"\r"===s||"\u2028"===s||"\u2029"===s?(t.line++,t.column=1,t.seenCR=!0):(t.column++,t.seenCR=!1)}(I,L,t),L=t),I}(s=U),a=s<n.length?n.charAt(s):null,null!==i&&function(t){var n=1;for(t.sort((function(t,n){return t.description<n.description?-1:t.description>n.description?1:0}));n<t.length;)t[n-1]===t[n]?t.splice(n,1):n++}(i),new t(null!==o?o:function(t,n){var e,o=new Array(t.length);for(e=0;e<t.length;e++)o[e]=t[e].description;return"Expected "+(t.length>1?o.slice(0,-1).join(", ")+" or "+o[t.length-1]:o[0])+" but "+(n?'"'+function(t){function n(t){return t.charCodeAt(0).toString(16).toUpperCase()}return t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\x08/g,"\\b").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\f/g,"\\f").replace(/\r/g,"\\r").replace(/[\x00-\x07\x0B\x0E\x0F]/g,(function(t){return"\\x0"+n(t)})).replace(/[\x10-\x1F\x80-\xFF]/g,(function(t){return"\\x"+n(t)})).replace(/[\u0180-\u0FFF]/g,(function(t){return"\\u0"+n(t)})).replace(/[\u1080-\uFFFF]/g,(function(t){return"\\u"+n(t)}))}(n)+'"':"end of input")+" found."}(i,a),i,a,s,r.line,r.column)}}}(),u=o.create((function(t){var n="",e="",o={},i="",s=this,r={},a={},u={},f=function(t){var n;s.slots.indexOf(t)<0&&s.slot(t,(n=t,function(){s.setToken(n)}))},h=function(t){var n;if(Object.keys(a).forEach((t=>a[t]=!1)),o.hasOwnProperty(t))for(n in a={},o[t])o[t].hasOwnProperty(n)&&(u[n]=c.build(r[t][n],a));i=t,s.emit("requestSheet",i),i===e&&s.emit("requestTermination")};this.setInitialState=function(t){i=n=t},this.setFinalState=function(t){e=t},this.addTransition=function(t,n,e){var i,s=/([A-Za-z_]\w*)/g;try{var a;if("string"==typeof(a=l.parse(n)))f(a);else for(;null!==(i=s.exec(n));)f(i[0]);r[t]=r[t]||{},r[t][n]=a,void 0===o[t]&&(o[t]={}),o[t][n]=e}catch(t){}},this.setToken=function(t){a.hasOwnProperty(t)&&(a[t]=!0);for(const[t,n]of Object.entries(u))n.eval()&&(u={},h(o[i][t]))},this.setTransitions=function(t){var n,e;for(n in t)if(t.hasOwnProperty(n))for(e in t[n])t[n].hasOwnProperty(e)&&this.addTransition(n,e,t[n][e])},this.start=function(){h(n)},void 0!==t&&(n=t.initial,e=t.final,this.setTransitions(t.transitions),i="")}),["setToken"],["requestSheet","requestTermination"]);let f=function(t){let n={},e={},o={},i=[],s=this,r=!1;o.StateMachine=u,void 0!==t&&(i=t.libraries,Object.assign(n,t.components),Object.assign(e,t.constants));var a=async function(t){return S()?new Promise((function(n,e){var o=require(t);void 0!==o?n(o):e("[ARCS] File not found")})):(await fetch(t)).json()};this.loadLibrary=function(t,n){var e=t,o=t;return f.currentContext=s,"string"!=typeof t&&(o=t.name,e=t.url),i.indexOf(o)<0&&i.push(o),import(e).then((function(t){for(let n in t.default)t.default.hasOwnProperty(n)&&f.currentContext.setFactory(n,t.default[n]);void 0!==n&&n()})).catch((function(t){console.error("[ARCS] Trouble loading '",e,"' with reason -",{msg:t})}))},this.getComponentList=function(){var t,e=Object.keys(n);for(t=0;t<e.length;t++)n.hasOwnProperty(e[t])||e.splice(t--,1);return e},this.getConstant=function(t){return e[t]},this.getComponentType=function(t){if(void 0!==n[t])return n[t].type},this.getComponentValue=function(t){if(void 0!==n[t])return n[t].value},this.getComponent=function(t){if(void 0!==n[t])return n[t].instance},this.getComponentName=function(t){var e,o;for(o=n.getComponentList(),e=0;e<o.length;e++)if(n[o[e]].instance===t)return o[e]},this.addFactories=function(t){for(p in r=!0,t)t.hasOwnProperty(p)&&(o[p]=t[p])},this.setFactory=function(t,n){o[t]=n},this.toJSON=function(){var t,e={};for(t in n)n.hasOwnProperty(t)&&(e[t]={type:n[t].type,value:n[t].value});return e},this.setComponentValue=function(t,e){n[t].value=e},this.addComponent=function(t,e,i){var s;n[t]={},n[t].type=e,n[t].value=i;var r=o[e];void 0!==r&&(s=new r(i)),n[t].instance=s},this.removeComponent=function(t){delete n[t]},this.getFactory=function(t){return o[t]},this.getFactoryList=function(){return Object.keys(o)},this.instanciate=async function(){try{r||await function(){var t;f.currentContext=s;var n=[];for(t=0;t<i.length;t++)n.push(s.loadLibrary(i[t]));return Promise.all(n)}(),await async function(){var t,s=[];for(t in n)if(n.hasOwnProperty(t)){if(void 0===o[n[t].type])return console.error("[ARCS] Factory "+n[t].type+" not found."),void console.error("[ARCS] Context dump follows: ",i,n,e);var r=o[n[t].type];try{void 0!==n[t].value||void 0!==n[t].url||void 0!==n[t].ref?(void 0!==n[t].value&&(n[t].instance=new r(n[t].value)),void 0!==n[t].url&&(console.log("loading data file",n[t].url),s.push(a(n[t].url).then(function(t,e){return function(o){return console.log("instanciating from data file"),n[t].instance=new e(o),Promise.resolve()}}(t,r)))),void 0!==n[t].ref&&void 0!==e[n[t].ref]&&(n[t].instance=new r(e[n[t].ref]))):n[t].instance=new r}catch(n){console.error("[ARCS] Component of type ",t," not instanciated.",n)}}return Promise.all(s)}()}catch(t){console.error("[ARCS] Trouble instanciating context",t)}};var c=function(t,n){return Object.create(n,t)};this.chain=function(t,i,s){return[c(t,n),c(i,e),c(s,o)]},this.setParent=function(t){if(void 0!==t){var i=t.chain(n,e,o);n=i[0],e=i[1],o=i[2]}}};f.currentContext=null;const h=f;let d=function(t,n,e){this.getDestination=function(){return t},this.getSlot=function(){return n},this.getValue=function(){return e},this.invoke=function(){let o=t[n];void 0!==o?o.apply(t,e):console.error("Undefined slot %s of component %s",n,t)}};d.cast=function(t,n){if(void 0!==t.call){let n=t.call.split(".");t.destination=n[0],t.slot=n[1]}let e=n.getComponent(t.destination);if(void 0===e&&console.error("[ARCS] Destination ",t.destination," is undefined"),void 0!==t.value)return new d(e,t.slot,t.value);if(void 0!==t.ref)return new d(e,t.slot,n.getConstant(t.ref));if(void 0!==t.storage){let n=null;return"undefined"!=typeof localStorage&&null===localStorage.getItem(`ARCS.${storage.key}`)&&void 0!==t.storage.default&&(n=t.storage.default),new d(e,t.slot,n)}return new d(e,t.slot)},d.PRE_CONNECTION=0,d.POST_CONNECTION=1,d.CLEAN_UP=2;const g=d;let v=function(t,n,e,i){this.connect=function(){try{o.connect(t,n,e,i)}catch(o){console.log(o,t,n,e,i)}},this.disconnect=function(){o.disconnect(t,n,e,i)},this.getSource=function(){return t},this.getDestination=function(){return e},this.getSlot=function(){return i},this.getSignal=function(){return n}};v.cast=function(t,n){if(void 0!==t.from){let n=t.from.split(".");t.source=n[0],t.signal=n[1]}if(void 0!==t.to){let n=t.to.split(".");t.destination=n[0],t.slot=n[1]}return new v(n.getComponent(t.source),t.signal,n.getComponent(t.destination),t.slot)};const y=v,m=function(t){var n,e,o,i,s,r=new h,a=[],c=[],l=[],u=[],f=0,p=0,d=0,v=0;n=function(){var t;for(t=0;t<a.length;t++)a[t].invoke()},e=function(){var t;for(t=0;t<c.length;t++)c[t].invoke()},o=function(){var t;for(t=0;t<l.length;t++)l[t].invoke()},i=function(){var t;for(t=0;t<u.length;t++)u[t].connect()},s=function(){var t;for(t=0;t<u.length;t++)u[t].disconnect()},this.setContext=function(t){r=t},this.activate=function(){r.instanciate().then((function(){n(),i(),e()}))},this.deactivate=function(){s(),o()},this.addPreConnection=function(t){var n=g.cast(t,r);return n.id=f++,a.push(n),n.id},this.addPostConnection=function(t){var n=g.cast(t,r);return n.id=p++,c.push(n),n.id},this.addCleanup=function(t){var n=g.cast(t,r);return n.id=d++,l.push(n),n.id},this.addConnection=function(t){var n=y.cast(t,r);return n.id=v++,u.push(n),n.id};let m=function(t,n){let e=n.length;for(;e--&&n[e].id!==t;);e>=0?n.splice(e,1):console.warn("Could not remove data with id",t)};this.removePreConnection=function(t){m(t,a)},this.removePostConnection=function(t){m(t,c)},this.removeCleanup=function(t){m(t,l)};var C=function(t,n,e){for(var o=e.length;o--&&e[o].id!==t;);o>=0&&(e[o].value=n)};this.changePreConnection=function(t,n){C(t,n,a)},this.changePostConnection=function(t,n){C(t,n,c)},this.changeCleanup=function(t,n){C(t,n,l)},this.removeConnection=function(t){m(t,u)};let w=function(t,n,e){let o,i=e.length,s=e.length;for(;i--&&e[i].id!==t;);for(;s--&&e[s].id!==n;);i>=0&&s>=0&&(o=e[i],e[i]=e[s],e[s]=o,e[i].id=t,e[s].id=n)};this.swapConnections=function(t,n){w(t,n,u)},this.swapCleanups=function(t,n){w(t,n,l)},this.swapPreConnections=function(t,n){w(t,n,a)},this.swapPostConnections=function(t,n){w(t,n,c)},this.import=function(n){n.hasOwnProperty("context")&&(r=new h(n.context)).setParent(t),r.instanciate().then((function(){!function(t){let n=0,e=g.cast,o=y.cast;if(void 0!==t.preconnections)for(n=0;n<t.preconnections.length;n++)a.push(e(t.preconnections[n],r));if(void 0!==t.postconnections)for(n=0;n<t.postconnections.length;n++)c.push(e(t.postconnections[n],r));if(void 0!==t.cleanups)for(n=0;n<t.cleanups.length;n++)l.push(e(t.cleanups[n],r));if(void 0!==t.connections)for(n=0;n<t.connections.length;n++)u.push(o(t.connections[n],r))}(n)}))};let S=function(t){return{destination:r.getComponentName(t.getDestination()),slot:t.getSlot(),value:t.getValue()}};this.toJSON=function(){let t,n=[],e=[],o=[],i=[];for(t=0;t<u.length;t++)o.push((s=u[t],{source:r.getComponentName(s.getSource()),signal:s.getSignal(),destination:r.getComponentName(s.getDestination()),slot:s.getSlot()}));var s;for(t=0;t<a.length;t++)n.push(S(a[t]));for(t=0;t<c.length;t++)e.push(S(c[t]));for(t=0;t<l.length;t++)i.push(S(l[t]));return{preconnections:n,postconnections:e,connections:o,cleanups:i}},r.setParent(t)};let C=function(){var t=new h,n={},e={},i=[],s=this,r="";this.export=function(){return{context:t,controller:t.getComponentName(e),sheets:n}},this.getContext=function(){return t},this.getSheetList=function(){return Object.keys(n)},this.getSheet=function(t){return n[t]},this.addSheet=function(e,o){n[e]=o,o.setContext(t)},this.removeSheet=function(t){delete n[t]},this.launch=function(){var i,r,a;if(console.log("[ARCS] Starting application..."),r=t.getComponent(e),void 0!==(e=r)){for(a=Object.keys(n),i=0;i<a.length;i++)(r=new m(t)).import(n[a[i]],t),n[a[i]]=r;o.connect(e,"requestSheet",s,"setSheet"),o.connect(e,"requestTermination",s,"finish"),e.start()}else console.error("[ARCS] undefined controller")},this.setController=function(n){e=t.getComponent(n)},this.setSheet=function(t){n.hasOwnProperty(t)?(r&&n[r].deactivate(),n[r=t].activate()):console.warn("[ARCS] Tried to activate hollow sheet named: "+t)},this.finish=function(){r&&n[r].deactivate()},this.import=function(o){t=new h(o.context),n=o.sheets,void 0===(e=o.controller)&&console.error("[ARCS] Undefined controller. Cannot start application.")},this.setFactory=function(t,n){factories[t]=n},this.setDependency=function(t){i[t]={}},this.start=async function(){console.log("[ARCS] Instanciating components..."),await t.instanciate(),this.launch()}};C.setDependency=function(t,n){t.setDependency(n)},o.create(C),C.slot("setSheet"),C.slot("finish");const w=C,S={Component:o,isInNode:function(){return"undefined"!=typeof process&&-1!==process.release.name.search(/node|io.js/)},StateMachine:u,Context:h,Invocation:g,Connection:y,Sheet:m,RemoteConsole:class{#t=[];#n;#e;_log=globalThis.console.log;_info=globalThis.console.info;_warn=globalThis.console.warn;_error=globalThis.console.error;constructor(t){let n=this;console.error=this.error,console.warn=this.warn,console.info=this.info,console.log=this.log,console.tainted=!0,globalThis.window&&(globalThis.window.onerror=this.runtimeException),globalThis.WebSocket&&globalThis.navigator&&(this.#n=new globalThis.WebSocket(t),this.#e=globalThis.navigator.userAgent.match(/firefox|fxios/i)?2:3,this.#n.onopen=function(){n.#o()})}#i(){return(new Error).stack.split("\n")[this.#e]}#s(t){this.#n&&(this.#n.readyState===WebSocket.OPEN?this.#n.send(t):this.#t.push(t))}#o(){this.#n&&this.#n.readyState===WebSocket.OPEN&&(this.#t.forEach((t=>this.#n.send(t))),this.#t=[])}#r(t){let n=this;return function(){let e={type:t,args:Array.from(arguments),context:n.#i()};n[`_${t}`].apply(globalThis.console,e.args);let o="";try{o=JSON.stringify(e)}catch(t){e.args=["Argument(s) not serializable"],o=JSON.stringify(e)}n.#s(o)}}#a(t,n,e,o,i){return!!this.#n&&(this.#n.send(JSON.stringify({type:"exception",args:[{message:t,url:n,line:e,column:o}]})),!1)}log=this.#r("log");info=this.#r("info");warn=this.#r("warn");error=this.#r("error")},Application:w,__lib__:()=>{}};var b=n.Z;export{b as default};