ff-worker-dist/index.js
2024-08-07 10:38:59 +08:00

4 lines
1.5 KiB
JavaScript

"use strict";importScripts("./lib/moment@2_29_4.min.js","./lib/moment.locale.zh-cn.js"),moment.locale("zh-cn");const Util=(e,s)=>{const t=Util.msg_id++;return new Proxy(()=>{},{get({root:o},a){const r=o?`${o}/${a}`:a;return function(...i){return new Promise((n,l)=>{Util.mQueue.set(t,[n,l]),self.postMessage({id:t,task_id:e,session:s,category:"util",method:r,args:i})})}},apply(o,a,r){return new Proxy(Object.create({root:r[0]||null}),{get:this.get})}})};Util.msg_id=0,Util.mQueue=new Map,Util.mMethodWorkspace=new Map,self.addEventListener("message",function({data:{id:e,data:s,error:t,category:o}}){o==="util"&&(t!==null?Util.mQueue.get(e)[1](t):Util.mQueue.get(e)[0](s),Util.mQueue.get(e),Util.mQueue.delete(e))},!1),self.addEventListener("message",({data:{id,method,args,category,session,sourceURL}})=>{if(category!=="eval")return;session&&!Util.mMethodWorkspace.has(session)&&Util.mMethodWorkspace.set(session,{});const evalDataSourceURL=sourceURL?`
//# sourceURL=ff:///./${sourceURL}`:"",evalData=eval(`(async function({${Object.keys(args).join(", ")}}, Util){${method}
})${evalDataSourceURL}`).call(session?Util.mMethodWorkspace.get(session):{},args,Util(id,session));return Promise.resolve(evalData).then(e=>{self.postMessage({id,session,category,data:e,error:null})}).catch(e=>{self.postMessage({id,session,category,data:null,error:e instanceof DOMException&&e.name==="DataCloneError"?"DataCloneError 返回结果只能为简单数据格式":e})})}),self.addEventListener("message",({data:{category:e,session:s}})=>{e==="clear"&&Util.mMethodWorkspace.delete(s)},!1);