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