jointjs
Version:
JavaScript diagramming library
2 lines (1 loc) • 2.05 kB
JavaScript
var workly=function(t){"use strict";function e(t,r){return r=r||[],new Proxy(function(){},{get(a,s,n){if("then"===s){if(0===r.length)return{then:()=>n};const e=t.remote({type:"GET",path:r});return e.then.bind(e)}return e(t,r.concat(s))},set:(e,a,s)=>t.remote({type:"SET",path:r.concat(a),value:s}),apply:(e,a,s)=>t.remote({type:"APPLY",path:r,args:s}),construct:(e,r)=>t.remote({type:"CONSTRUCT",args:r})})}class r{constructor(t,s){this.w=t,this.uid=s||`${Date.now()}-${a()}`,this.c=0,this.cbs={},t.addEventListener("message",a=>{if(this.w.oURL)try{URL.revokeObjectURL(this.w.oURL)}catch(t){}finally{delete this.w.oURL}let s=a.data&&a.data.id,n=s&&this.cbs[s];n&&(delete this.cbs[s],a.data.error?n[1](new Error(a.data.error)):n[0](a.data.targetId?e(new r(t,a.data.targetId)):a.data.value))})}remote(t){const e=t.args||[],r=`${this.uid}-${++this.c}`;return new Promise((a,s)=>{this.cbs[r]=[a,s],this.w.postMessage(Object.assign({},t,{id:r,args:e,target:this.uid}))})}}function a(){return Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)}function s(t){const e=t,r={};self.addEventListener("message",async t=>{let s=t.data||{};s.path=s.path||[];let n=s.target&&r[s.target]||e;const o=t=>t.reduce((t,e)=>t?t[e]:t,n),c=s&&s.id;if(c&&s.type){const t={id:c},e=o(s.path),n=o(s.path.slice(0,-1));switch(s.type){case"GET":t.value=e;break;case"SET":let o=s.path.length&&s.path[s.path.length-1];o&&(n[o]=s.value),t.value=!!o;break;case"APPLY":try{t.value=await e.apply(n,s.args||[])}catch(e){t.error=e.toString()}break;case"CONSTRUCT":try{t.value=new e(...s.args),t.targetId=(t=>{const e=`${Date.now()}-${a()}`;return r[e]=t,e})(t.value)}catch(e){t.error=e.toString()}}self.postMessage(t)}})}return t.proxy=function(t){let n,o;if("function"==typeof t){const e=Function.prototype.toString;n=o=URL.createObjectURL(new Blob([`${e.call(a)}\n(${e.call(s)})(${e.call(t)})`]))}else"string"==typeof t&&(n=t,0===t.indexOf("blob:")&&(o=n));if(n){let t=new Worker(n);return o&&(t.oURL=o),e(new r(t))}throw"Workly only supports functions, classes, urls"},t.expose=s,t}({});