From 94235f3e744452dd7adf2e3ac5737e90d895dafe Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Wed, 2 Jun 2021 19:44:28 +0200 Subject: [PATCH] fix node.js worker threads --- dist/node/node.js | 3 ++- package.json | 2 +- src/index.js | 4 +--- src/worker/handler.js | 5 ----- src/worker/index.js | 16 +++------------- 5 files changed, 7 insertions(+), 23 deletions(-) diff --git a/dist/node/node.js b/dist/node/node.js index 3d91340..4e1a20b 100644 --- a/dist/node/node.js +++ b/dist/node/node.js @@ -8,6 +8,7 @@ parentPort.on("message", function(data){ /** @type Index */ const args = data["args"]; const task = data["task"]; + const id = data["id"]; switch(task){ @@ -29,6 +30,6 @@ parentPort.on("message", function(data){ default: const message = index[task].apply(index, args); - parentPort.postMessage(task === "search" ? message : null); + parentPort.postMessage(task === "search" ? { "id": id, "msg": message } : { "id": id }); } }); diff --git a/package.json b/package.json index fe93645..ba1a600 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "copy": "npm run clean && cpx \"src/**\" tmp/", "clean": "if exist tmp rmdir tmp /s /q && mkdir tmp", "build": "npm run copy && npm run build:bundle", - "build:bundle": "node task/build RELEASE=bundle", + "build:bundle": "node task/build RELEASE=bundle && cpx \"src/worker/node.js\" dist/node/", "build:debug": "node task/build RELEASE=debug COMPILATION_LEVEL=SIMPLE FORMATTING=PRETTY_PRINT", "build:compact": "node task/build RELEASE=compact", "build:light": "node task/build RELEASE=light", diff --git a/src/index.js b/src/index.js index 50778bc..0e7b919 100644 --- a/src/index.js +++ b/src/index.js @@ -20,15 +20,13 @@ import { IndexInterface } from "./type.js"; import { encode as default_encoder } from "./lang/latin/default.js"; import { create_object, create_object_array, concat, sort_by_length_down, is_array, is_string, is_object, parse_option } from "./common.js"; import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js"; -import { registerCharset, global_lang, global_charset } from "./global.js"; +import { global_lang, global_charset } from "./global.js"; import apply_async from "./async.js"; import { intersect } from "./intersect.js"; import Cache, { searchCache } from "./cache.js"; import apply_preset from "./preset.js"; import { exportIndex, importIndex } from "./serialize.js"; -//registerCharset("latin:default", default_encoder); - /** * @constructor * @implements IndexInterface diff --git a/src/worker/handler.js b/src/worker/handler.js index 9588f57..b63c847 100644 --- a/src/worker/handler.js +++ b/src/worker/handler.js @@ -47,10 +47,5 @@ export default function(data) { const id = data["id"]; const message = index[task].apply(index, args); postMessage(task === "search" ? { "id": id, "msg": message } : { "id": id }); - - // if(task === "search"){ - // - // postMessage(message); - // } } }; diff --git a/src/worker/index.js b/src/worker/index.js index b86766e..62fa36b 100644 --- a/src/worker/index.js +++ b/src/worker/index.js @@ -2,7 +2,7 @@ import { create_object, is_function, is_object, is_string } from "../common.js"; import handler from "./handler.js"; -let counter = 0; +let pid = 0; /** * @param {Object=} options @@ -68,7 +68,6 @@ function WorkerIndex(options){ this.worker.postMessage({ "task": "init", - //"id": ++counter, "factory": factory, "options": options }); @@ -102,22 +101,13 @@ function register(key){ setTimeout(function(){ - self.resolver[++counter] = resolve; + self.resolver[++pid] = resolve; self.worker.postMessage({ "task": key, - "id": counter, + "id": pid, "args": args }); - - // if(key === "search"){ - // - // self.resolve = resolve; - // } - // else{ - // - // resolve(); - // } }); });