mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-10-04 09:01:41 +02:00
1 line
1.0 KiB
JavaScript
1 line
1.0 KiB
JavaScript
const{parentPort}=require("worker_threads"),{Index}=require("flexsearch");let index,options;parentPort.on("message",async function(a){const b=a.task,c=a.id;let d=a.args;switch(b){case"init":options=a.options||{};let e=options.config;e&&(options=Object.assign({},options,require(e)),delete options.worker),index=new Index(options),parentPort.postMessage({id:c});break;default:let f;if("export"===b){if(!options.export||"function"!=typeof options.export)throw new Error("Either no extern configuration provided for the Worker-Index or no method was defined on the config property \"export\".");d[1]?(d[0]=options.export,d[2]=0,d[3]=1):d=null}if("import"===b){if(!options.import||"function"!=typeof options.import)throw new Error("Either no extern configuration provided for the Worker-Index or no method was defined on the config property \"import\".");if(d[0]){const a=await options.import.call(index,d[0]);index.import(d[0],a)}}else f=d&&index[b].apply(index,d),f&&f.then&&(f=await f);parentPort.postMessage("search"===b?{id:c,msg:f}:{id:c});}}); |