site/node_modules/flexsearch/dist/node/node.js
2024-10-14 08:09:33 +02:00

36 lines
853 B
JavaScript

const { parentPort } = require("worker_threads");
const { Index } = require("../flexsearch.bundle.min.js");
let index;
parentPort.on("message", function(data){
/** @type Index */
const args = data["args"];
const task = data["task"];
const id = data["id"];
switch(task){
case "init":
const options = data["options"] || {};
const encode = options["encode"];
options["cache"] = false;
if(encode && (encode.indexOf("function") === 0)){
options["encode"] = new Function("return " + encode)();
}
index = new Index(options);
break;
default:
const message = index[task].apply(index, args);
parentPort.postMessage(task === "search" ? { "id": id, "msg": message } : { "id": id });
}
});