Skip to content

Commit

Permalink
fix adding documents (async)
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-thomas committed Jun 5, 2021
1 parent 0386c3f commit 344f850
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 374 deletions.
54 changes: 27 additions & 27 deletions dist/flexsearch.bundle.js

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions dist/flexsearch.compact.js

Large diffs are not rendered by default.

491 changes: 242 additions & 249 deletions dist/flexsearch.debug.js

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions dist/flexsearch.es5.js

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions dist/flexsearch.light.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions dist/node/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { parentPort } = require("worker_threads");
const { Index } = require("../flexsearch.min.js");
const { Index } = require("../flexsearch.bundle.js");

let index;

Expand All @@ -26,14 +26,9 @@ parentPort.on("message", function(data){
index = new Index(options);
break;

case "search":

const message = index.search.apply(index, args);
parentPort.postMessage(message);
break;

default:

index[task].apply(index, args);
const message = index[task].apply(index, args);
parentPort.postMessage(task === "search" ? message : null);
}
});
3 changes: 2 additions & 1 deletion src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function register(prototype, key){
setTimeout(function(){

self.async = true;
resolve(self[key].apply(self, args));
const res = self[key].apply(self, args);
self.async = false;
resolve(res);
});
});

Expand Down
20 changes: 10 additions & 10 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function add_index(obj, tree, marker, pos, index, id, key, _append){

for(let i = 0; i < obj.length; i++){

index.add(id, obj[i], true);
index.add(id, obj[i], true, true);
}

return;
Expand All @@ -272,7 +272,7 @@ function add_index(obj, tree, marker, pos, index, id, key, _append){
obj = obj.join(" ");
}

index.add(id, obj, _append);
index.add(id, obj, _append, true);
}
else if(obj){

Expand Down Expand Up @@ -408,32 +408,32 @@ Document.prototype.remove = function(id){

if(is_object(id)){

id = id[this.key];
id = parse_simple(id, this.key);
}

if(this.register[id]){

const fastupdate = this.fastupdate && !this.worker;

for(let i = 0; i < this.field.length; i++){

this.index[this.field[i]].remove(id, true);
this.index[this.field[i]].remove(id, fastupdate);

// workers does not share the register

if(this.fastupdate && !this.worker){
if(fastupdate){

// when fastupdate was enabled all ids will
// be already cleanup after the first loop
// when fastupdate was enabled all ids are removed

break;
}
}

if(SUPPORT_TAGS && this.tag){

// when fastupdate was enabled the id will
// be already cleanup by the index
// when fastupdate was enabled all ids are already removed

if(!this.fastupdate || this.worker){
if(!fastupdate){

for(let key in this.tagindex){

Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ Index.prototype.append = function(id, content){
* @param {!number|string} id
* @param {!string} content
* @param {boolean=} _append
* @param {boolean=} _skip_update
*/

Index.prototype.add = function(id, content, _append){
Index.prototype.add = function(id, content, _append, _skip_update){

if(this.register[id] && !_append){
if(!_skip_update && !_append && this.register[id]){

return this.update(id, content);
}
Expand Down
16 changes: 8 additions & 8 deletions src/worker/handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Index from "../index.js";

export default function handler(event) {
export default function(event) {

/** @type Index */
const index = self["_index"];
Expand Down Expand Up @@ -41,14 +41,14 @@ export default function handler(event) {

break;

case "search":

const message = index.search.apply(index, args);
postMessage(message);
break;

default:

index[task].apply(index, args);
const message = index[task].apply(index, args);
postMessage(task === "search" ? message : null);

// if(task === "search"){
//
// postMessage(message);
// }
}
};
38 changes: 19 additions & 19 deletions src/worker/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { promise as Promise } from "../polyfill.js";
import { is_function, is_object } from "../common.js";
import { is_function, is_object, is_string } from "../common.js";
import handler from "./handler.js";

let counter = 0;
Expand All @@ -19,21 +19,20 @@ function WorkerIndex(id, options){
options = /** @type {Object} */ (id);
//id = 0;
}
else{

if(options){
if(options){

if(is_function(opt = options["encode"])){
if(is_function(opt = options["encode"])){

options["encode"] = opt.toString();
}
options["encode"] = opt.toString();
}
else{
}
else{

options = {};
}
options = {};
}


// the factory is the outer wrapper from the build
// we use "self" as a trap for node.js

Expand All @@ -46,7 +45,7 @@ function WorkerIndex(id, options){

const is_node_js = self["exports"];

this.worker = create(factory, is_node_js);
this.worker = create(factory, is_node_js, options["worker"]);

if(!this.worker){

Expand Down Expand Up @@ -99,14 +98,14 @@ function register(key){

self.worker.postMessage({ "task": key, /*id: this.id,*/ "args": args });

if(key === "search"){
//if(key === "search"){

self.resolver = resolve;
}
else{

resolve();
}
// }
// else{
//
// resolve();
// }
});
});

Expand All @@ -122,7 +121,7 @@ function register(key){
};
}

function create(factory, is_node_js){
function create(factory, is_node_js, worker_path){

let worker

Expand All @@ -131,7 +130,7 @@ function create(factory, is_node_js){
worker = is_node_js ?

eval('new (require("worker_threads")["Worker"])("../dist/node/node.js")')
:
:(
factory ?

new Worker(URL.createObjectURL(
Expand All @@ -143,7 +142,8 @@ function create(factory, is_node_js){
], { "type": "text/javascript" })
))
:
new Worker("worker.js", { type: "module" });
new Worker(is_string(worker_path) ? worker_path : "worker/worker.js", { type: "module" })
);
}
catch(e){}

Expand Down
9 changes: 2 additions & 7 deletions src/worker/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ parentPort.on("message", function(data){
index = new Index(options);
break;

case "search":

const message = index.search.apply(index, args);
parentPort.postMessage(message);
break;

default:

index[task].apply(index, args);
const message = index[task].apply(index, args);
parentPort.postMessage(task === "search" ? message : null);
}
});

0 comments on commit 344f850

Please sign in to comment.