site/node_modules/flexsearch/dist/node/node.js

36 lines
853 B
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
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 });
}
});