Host FlexSearch

This commit is contained in:
Will Faught
2025-03-02 00:23:46 -08:00
parent 2e77b35940
commit 078157e62c
164 changed files with 23495 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
import Index from "../index.js";
export default function (data) {
data = data.data;
/** @type Index */
const index = self._index,
args = data.args,
task = data.task;
switch (task) {
case "init":
const options = data.options || {},
factory = data.factory,
encode = options.encode;
options.cache = /* normalize: */ /* collapse: */ /* normalize: */
/* collapse: */ /* normalize: */ /* collapse: */ /* normalize: */ /* collapse: */ /* collapse: */!1;
if (encode && 0 === encode.indexOf("function")) {
options.encode = Function("return " + encode)();
}
if (factory) {
// export the FlexSearch global payload to "self"
Function("return " + factory)()(self);
/** @type Index */
self._index = new self.FlexSearch.Index(options);
// destroy the exported payload
delete self.FlexSearch;
} else {
self._index = new Index(options);
}
break;
default:
const id = data.id,
message = index[task].apply(index, args);
postMessage("search" === task ? { id: id, msg: message } : { id: id });
}
}

View File

@@ -0,0 +1,136 @@
//import { promise as Promise } from "../polyfill.js";
import { create_object, is_function, is_object, is_string } from "../common.js";
import handler from "./handler.js";
let pid = 0;
/**
* @param {Object=} options
* @constructor
*/
function WorkerIndex(options) {
if (!(this instanceof WorkerIndex)) {
return new WorkerIndex(options);
}
let opt;
if (options) {
if (is_function(opt = options.encode)) {
options.encode = opt.toString();
}
} else {
options = {};
}
// the factory is the outer wrapper from the build
// we use "self" as a trap for node.js
let factory = (self || window)._factory;
if (factory) {
factory = factory.toString();
}
const is_node_js = "undefined" == typeof window && self.exports,
_self = this;
this.worker = create(factory, is_node_js, options.worker);
this.resolver = create_object();
if (!this.worker) {
return;
}
if (is_node_js) {
this.worker.on("message", function (msg) {
_self.resolver[msg.id](msg.msg);
delete _self.resolver[msg.id];
});
} else {
this.worker.onmessage = function (msg) {
msg = msg.data;
_self.resolver[msg.id](msg.msg);
delete _self.resolver[msg.id];
};
}
this.worker.postMessage({
task: "init",
factory: factory,
options: options
});
}
export default WorkerIndex;
register("add");
register("append");
register("search");
register("update");
register("remove");
function register(key) {
WorkerIndex.prototype[key] = WorkerIndex.prototype[key + "Async"] = function () {
const self = this,
args = [].slice.call(arguments),
arg = args[args.length - 1];
let callback;
if (is_function(arg)) {
callback = arg;
args.splice(args.length - 1, 1);
}
const promise = new Promise(function (resolve) {
setTimeout(function () {
self.resolver[++pid] = resolve;
self.worker.postMessage({
task: key,
id: pid,
args: args
});
});
});
if (callback) {
promise.then(callback);
return this;
} else {
return promise;
}
};
}
function create(factory, is_node_js, worker_path) {
let worker;
try {
worker = is_node_js ? eval('new (require("worker_threads")["Worker"])(__dirname + "/node/node.js")') : factory ? new Worker(URL.createObjectURL(new Blob(["onmessage=" + handler.toString()], { type: "text/javascript" }))) : new Worker(is_string(worker_path) ? worker_path : "worker/worker.js", { type: "module" });
} catch (e) {}
return worker;
}

View File

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

View File

@@ -0,0 +1,2 @@
import handler from "./handler.js";
onmessage = handler;