UNPKG

hyperscript.org

Version:

a small scripting language for the web

3 lines (2 loc) 3.43 kB
var m=class e{errors=[];collectErrors(t){if(t||(t=new Set),t.has(this))return[];t.add(this);var r=[...this.errors];for(var n of Object.keys(this))for(var a of[this[n]].flat())a instanceof e&&r.push(...a.collectErrors(t));return r}sourceFor(){return this.programSource.substring(this.startToken.start,this.endToken.end)}lineFor(){return this.programSource.split(` `)[this.startToken.line-1]}static parseEventArgs(t){var r=[];if(t.token(0).value==="("&&(t.token(1).value===")"||t.token(2).value===","||t.token(2).value===")")){t.matchOpToken("(");do r.push(t.requireTokenType("IDENTIFIER"));while(t.matchOpToken(","));t.requireOpToken(")")}return r}};var h=class extends m{isFeature=!0;constructor(){super(),this.constructor.keyword&&(this.type=this.constructor.keyword+"Feature")}install(t,r,n,a){}static parseErrorAndFinally(t){var r,n,a;return t.matchToken("catch")&&(r=t.requireTokenType("IDENTIFIER").value,n=t.requireElement("commandList"),t.ensureTerminated(n)),t.matchToken("finally")&&(a=t.requireElement("commandList"),t.ensureTerminated(a)),{errorHandler:n,errorSymbol:r,finallyHandler:a}}};var L=0,j=function(e){e.onmessage=function(t){switch(t.data.type){case"init":e.importScripts(t.data._hyperscript),e.importScripts.apply(e,t.data.extraScripts);var r=e._hyperscript.parse(t.data.src);r.apply(e,e),postMessage({type:"didInit"});break;case"call":try{var n=e._hyperscript.internals.runtime.getHyperscriptFeatures(e)[t.data.function].apply(e,t.data.args);Promise.resolve(n).then(function(i){postMessage({type:"resolve",id:t.data.id,value:i})}).catch(function(i){postMessage({type:"reject",id:t.data.id,error:i.toString()})})}catch(i){postMessage({type:"reject",id:t.data.id,error:i.toString()})}break}}},q="("+j.toString()+")(self)",M=new Blob([q],{type:"text/javascript"}),N=URL.createObjectURL(M),o=class e extends h{static keyword="worker";constructor(t,r,n,a){super(),this.workerName=t,this.name=t,this.nameSpace=r,this.worker=n,this.stubs=a}static parse(t){if(t.matchToken("worker")){var r=t.requireElement("dotOrColonPath"),n=r.evalStatically(),a=n.split("."),i=a.pop(),v=[];if(t.matchOpToken("(")&&!t.matchOpToken(")")){do{var E=t.requireTokenType("STRING").value,S=new URL(E,location.href).href;v.push(S)}while(t.matchOpToken(","));t.requireOpToken(")")}var f=[],F=t.consumed.length,y=t.consumed.length;do{var l=t.parseAnyOf(["defFeature","jsFeature"]);if(l){if(l.type==="defFeature")f.push(l.name),y=t.consumed.length;else if(t.hasMore())continue}else break}while(t.matchToken("end")&&t.hasMore());var d=t.consumed.slice(F,y+1),b=t.source.substring(d[0].start,d[d.length-1].end),s=new Worker(N);s.postMessage({type:"init",_hyperscript:document.currentScript?.src||"/dist/_hyperscript.js",extraScripts:v,src:b});var O=new Promise(function(c,k){s.addEventListener("message",function(p){p.data.type==="didInit"&&c()},{once:!0})}),g={};return f.forEach(function(c){g[c]=function(){var k=arguments;return new Promise(function(p,x){var T=L++;s.addEventListener("message",function I(u){u.data.id===T&&(s.removeEventListener("message",I),u.data.type==="resolve"?p(u.data.value):x(u.data.error))}),O.then(function(){s.postMessage({type:"call",function:c,args:Array.from(k),id:T})})})}}),new e(i,a,s,g)}}install(t,r,n,a){a.assignToNamespace(t,this.nameSpace,this.workerName,this.stubs)}};function w(e){e.addFeature(o.keyword,o.parse.bind(o))}typeof self<"u"&&self._hyperscript&&self._hyperscript.use(w);export{w as default};