-
-
Save ugudango/fde5d2d6a07bc069773f4ce26b9445bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict";class e{_endpoints=new Map;getEndpoint(e){return this._endpoints.get(e)??(()=>{console.error(`Endpoint ${e} non-existent or malformed.`)})}setEndpoint(e,t){this._endpoints.set(e,t)}_defaultEndpointAction=()=>{};get defaultEndpointAction(){return this._defaultEndpointAction}set defaultEndpointAction(e){"function"==typeof e&&(this._defaultEndpointAction=e)}}class t{endpointRegistry=new e;context;config={contextTokens:{fetch:"$get",call:"$call",remoteCall:"$callRemote"}};_self;_responseCallbacks={};constructor(e,n=(()=>null)){var s,o,r,i,a;null===n()&&(s=this.endpointRegistry,n=e=>{const t=e[1];return"endpoint"in t?s.getEndpoint(t.endpoint)(t):s.defaultEndpointAction(t)}),this._self=e,this.postMessage=(o=t._getNextId(),r=this._responseCallbacks,i=this._self,e=>{const t=o.next().value;return new Promise(((n,s)=>{Object.defineProperty(r,t,{writable:!1,configurable:!0,value:(e,t)=>e?s(new Error(e.message)):n(t)}),i.postMessage([t,e])}))}),this.makeRequest=(a=this.postMessage,(e,t)=>{const n=t;return n.endpoint=e,a(n)}),this.handleIncomingMessage=function(e){return(t,n,s)=>{let o;try{o={response:t(s)}}catch(e){o={error:e}}var r;o.error?e.postMessage([n,o.error.message]):!(r=o.response)||"object"!=typeof r&&"function"!=typeof r||"function"!=typeof r.then?e.postMessage([n,null,o.response]):o.response.then((t=>{e.postMessage([n,null,t])})).catch((t=>{e.postMessage([s,t])}))}}(this._self),this.context=function(e,t){const n={get(s,o){const r=[...s];switch(s.length=0,o){case t.fetch:return e("context",{data:{field:r.join(".")}});case t.call:return async(...t)=>e("context",{data:{field:r.join("."),arguments:t}});case t.remoteCall:return async(...t)=>e("context-remote",{data:{field:r.join("."),argumentIdentifiers:t}});default:return(s=[...r]).push(o),new Proxy(s,n)}}};return new Proxy([],n)}(this.makeRequest,this.config.contextTokens),this._self.onmessage=function(e,t,n,s,o,r){return i=>{const a=i.data;if(Array.isArray(a)&&!(a.length<2))if(null!==a[0].match(new RegExp(`${e}-[0-9]+`,"g"))){const[e,t,n]=a,s=o[e];if(!s)return;delete o[e],s(t,n)}else if(null!==a[0].match(new RegExp(`${t}-[0-9]+`,"g"))){const[e]=a;"function"!=typeof s?r.postMessage([e,new Error("Please construct this worker with a valid requestHandler function!")]):n(s,e,a)}}}("c","h",this.handleIncomingMessage,n,this._responseCallbacks,this._self)}makeRequest;postMessage;handleIncomingMessage;static*_getNextId(){const e=Number.MAX_SAFE_INTEGER;for(let t=0;t<e;t=(t+1)%e)yield`c-${t}`;return"c-0"}log(e){this.makeRequest("log",{data:e})}}Object.defineProperty(self,"tile",{value:new t(self),writable:!1}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment