From 9c7541d9939d89290af0a2254c4beaaba800fb90 Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Wed, 19 May 2021 12:22:01 +0200 Subject: [PATCH] added support for workers --- dist/flexsearch.min.js | 52 +++++++++--------- doc/0.7.0.md | 32 ++++++++--- src/adapter.js | 81 ++++++++++++++++++++++++++++ src/async.js | 119 +++++++++++------------------------------ src/cache.js | 7 +-- src/config.js | 3 -- src/config/bundle.js | 3 -- src/config/compact.js | 3 -- src/config/light.js | 3 -- src/document.js | 72 ++++++++++++++++++++----- src/index.js | 24 ++------- src/polyfill.js | 19 +++---- src/webpack.js | 2 +- src/worker.js | 41 +++++++------- task/build.js | 6 +-- 15 files changed, 268 insertions(+), 199 deletions(-) create mode 100644 src/adapter.js diff --git a/dist/flexsearch.min.js b/dist/flexsearch.min.js index 6911ef1..a2a6024 100644 --- a/dist/flexsearch.min.js +++ b/dist/flexsearch.min.js @@ -5,27 +5,31 @@ * Licence: Apache-2.0 * https://github.com/nextapps-de/flexsearch */ -(function(){'use strict';var q;var t=/[\W_]+/;function aa(a){if(a=a.toLowerCase())if(a&&this.B&&(a=B(a,this.B)),this.D&&1=b)return h.concat(c.slice(0,b-g));h=h.concat(c);g+=e}return h};function R(a){this.h=!0!==a&&a;this.cache=D();this.g=[]}function ea(a,b,c){"object"===typeof a&&(a=a.query);var d=this.cache.get(a);d||(d=this.search(a,b,c),this.cache.set(a,d));return d}R.prototype.set=function(a,b){if(!this.cache[a]){var c=this.g.length;c===this.h?delete this.cache[this.g[c-1]]:c++;for(--c;0=f&&(g=f-1);this.u=f;this.threshold=g;this.A=b=c&&c.G||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.v=T(h.bidirectional,!0);this.l=e="memory"===a.optimize; -this.i=T(a.fastupdate,!0);this.s=a.minlength||1;this.g=e?C(f-g):D();f=h.resolution||f;g=h.threshold||g;g>=f&&(g=f-1);this.j=f;this.m=g;this.h=e?C(f-g):D();this.C=c&&c.C||a.rtl;this.B=(b=a.matcher||d&&d.B)&&F(b,!1);this.D=(b=a.stemmer||d&&d.D)&&F(b,!0);this.filter=(b=a.filter||d&&d.filter)&&ca(b);this.cache=(b=a.cache)&&new R(b);this.o=(b=a.worker)&&new Worker("worker.js",{type:"module"});b&&(this.o.onmessage=function(k){console.log(k.data)},this.o.postMessage({H:"register",options:a}))} -function T(a,b){return"undefined"!==typeof a?a:b}q=S.prototype;q.append=function(a,b){return this.add(a,b,!0)}; -q.add=function(a,b,c){if(this.register[a]&&!c)return this.update(a,b);if(b&&(a||0===a)){b=this.encode(b);var d=b.length;if(d){for(var e=this.depth,h=this.u-this.threshold,f=D(),g=D(),k=0;k=this.s&&(e||!f[l])){var r=Math.min(this.u/d*k|0,k);if(rm;w--)p=l.substring(m,w),p.length>=this.s&&U(this,f,p,u,a,c)}break}case "reverse":if(2< -n){for(m=n-1;0=this.s&&U(this,f,p,r,a,c);p=""}case "forward":if(1=this.s&&U(this,f,p,r,a,c);break;default:if(U(this,f,l,r,a,c),e&&1=this.s&&!r[l]){if(r[l]=1,w=Math.min((this.j-m)/d*k+u|0,k+(u-1)),wp;U(this,g,v?p:l,w,a,c,v?l:p)}}else m=Math.min(m+1,d-k)}}}}this.i||(this.register[a]=1)}}return this}; -function U(a,b,c,d,e,h,f){var g=f?a.h:a.g;if(!b[c]||f&&!b[c][f])a.l&&(g=g[d]),f?(b[c]||(b[c]=D()),b[c][f]=1,g=g[f]||(g[f]=D())):b[c]=1,g=g[c]||(g[c]=[]),a.l||(g=g[d]||(g[d]=[])),h&&-1!==g.indexOf(e)||(g[g.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=g))} -q.search=function(a,b,c){"object"===typeof a?(c=a,a=c.query):"object"===typeof b&&(c=b);var d=[],e=this.threshold,h=0;if(c){b=c.limit;h=c.offset||0;e=T(c.threshold,e);var f=c.context;var g=c.suggest}if(a){a=this.encode(a);var k=a.length;if(1=this.s&&!c[p])if(this.l||g||this.g[p])l[r++]=p,c[p]=1;else return d;a=l;k=a.length}}if(!k)return d;b||(b=100);c=this.u-e;e=this.j-e;f=this.depth&&1b?d.slice(0,b):d}}return da(d,b,h,g)};function ka(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a} -function ja(a,b,c,d,e,h,f,g){var k=[],l=g?a.h:a.g;a.l||(l=ka(l,f,g,a.v));if(l){var n=0;d=Math.min(l.length,d);for(var r=0,p=0,m;r=e)));r++);if(n){if(h)return k=1===n?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!c&&k}q.contain=function(a){return!!this.register[a]};q.update=function(a,b){return this.remove(a).add(a,b)}; -q.remove=function(a,b){var c=this.register[a];if(c){if(this.i)for(var d=0,e;da||c)g=g.slice(c,c+a);l&&(g=ma.call(this,g));h={tag:h, -result:g}}else h=void 0;h&&(d[d.length]=h,f++)}return f?d:[]}}g||(g=this.g);n=n&&(1=b)return f.concat(l.slice(0,b-h));f=f.concat(l);h+=u}return f} +;function M(a){this.h=!0!==a&&a;this.cache=E();this.g=[]}function ha(a,b,d){"object"===typeof a&&(a=a.query);let c=this.cache.get(a);c||(c=this.search(a,b,d),this.cache.set(a,c));return c}M.prototype.set=function(a,b){if(!this.cache[a]){var d=this.g.length;d===this.h?delete this.cache[this.g[d-1]]:d++;for(--d;0=e&&(f=e-1);this.s=e;this.threshold=f;this.u=b=d&&d.I||a.tokenize||"strict";this.depth="strict"===b&&h.depth; +this.v=P(h.bidirectional,!0);this.l=g="memory"===a.optimize;this.i=P(a.fastupdate,!0);this.o=a.minlength||1;this.g=g?B(e-f):E();e=h.resolution||e;f=h.threshold||f;f>=e&&(f=e-1);this.j=e;this.m=f;this.h=g?B(e-f):E();this.D=d&&d.D||a.rtl;this.B=(b=a.matcher||c&&c.B)&&F(b,!1);this.F=(b=a.stemmer||c&&c.F)&&F(b,!0);if(d=b=a.filter||c&&c.filter){d=b;c=E();for(let k=0,l=d.length;k=this.o&&(u||!q[r])){var e=Math.min(this.s/l*w|0,w);if(eg;k--)f=r.substring(g,k),f.length>=this.o&&Q(this,q,f,h,a,d)}break}case "reverse":if(2< +c){for(g=c-1;0=this.o&&Q(this,q,f,e,a,d);f=""}case "forward":if(1=this.o&&Q(this,q,f,e,a,d);break;default:if(Q(this,q,r,e,a,d),u&&1=this.o&&!e[r]){if(e[r]=1,k=Math.min((this.j-g)/l*w+h|0,w+(h-1)),kf;Q(this,n,y?f:r,k,a,d,y?r:f)}}else g=Math.min(g+1,l-w)}}}}this.i||(this.register[a]=1)}}return this}; +function Q(a,b,d,c,e,f,g){let h=g?a.h:a.g;if(!b[d]||g&&!b[d][g])a.l&&(h=h[c]),g?(b[d]||(b[d]=E()),b[d][g]=1,h=h[g]||(h[g]=E())):b[d]=1,h=h[d]||(h[d]=[]),a.l||(h=h[c]||(h[c]=[])),f&&-1!==h.indexOf(e)||(h[h.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))} +t.search=function(a,b,d){"object"===typeof a?(d=a,a=d.query):"object"===typeof b&&(d=b);let c=[],e;var f=this.threshold;let g,h=0;if(d){b=d.limit;h=d.offset||0;f=P(d.threshold,f);var k=d.context;g=d.suggest}if(a&&(a=this.encode(a),e=a.length,1=this.o&&!d[n])if(this.l||g||this.g[n])l[q++]=n,d[n]=1;else return c;a=l;e=a.length}if(!e)return c;b||(b=100);d=this.s-f;f=this.j-f;k=this.depth&&1b?c.slice(0,b):c}}return fa(c,b,h,g)};function la(a,b,d,c){d?(c=c&&b>d,a=(a=a[c?b:d])&&a[c?d:b]):a=a[b];return a} +function ka(a,b,d,c,e,f,g,h){let k=[],l=h?a.h:a.g;a.l||(l=la(l,g,h,a.v));if(l){let u=0;c=Math.min(l.length,c);for(let m=0,q=0,n;m=e)));m++);if(u){if(f)return k=1===u?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!d&&k}t.contain=function(a){return!!this.register[a]};t.update=function(a,b){return this.remove(a).add(a,b)}; +t.remove=function(a,b){const d=this.register[a];if(d){if(this.i)for(let c=0,e;cg||m)n=n.slice(m, +m+g);h&&(n=oa.call(c,n));p={tag:f,result:n}}else p=void 0;p&&(e[e.length]=p,u++)}return u?e:[]}}m||(m=c.g);q=q&&(1 What is not included yet but comes soon? -- Worker (almost done) -- Offset-Pagination (not implemented yet) +- ~~Worker (almost done)~~ +- Offset-Pagination (almost done) - ~~Export/Import (not implemented yet)~~ - Engines (almost done) - ~~Tags (almost done)~~ @@ -80,6 +80,7 @@ What will be dropped? - Where-Clause - Index Information `index.info()` - Paging Cursor (gets replaced by offset) +- Inline Worker (was replaced by native workers) ## Builtin Profiles @@ -288,7 +289,7 @@ Assuming our document has a data structure like this: } ``` -Old syntax FlexSearch v0.6.3 (___not supported anymore___): +Old syntax FlexSearch v0.6.3 (___not supported anymore!___): ```js const index = new Document({ @@ -983,8 +984,10 @@ One important change is how workers divided their tasks and how contents are dis Let us take an example. Assuming you have 4 workers and you will add 4 contents to the index, then each content is delegated to one worker (a perfect balance but index becomes a partial index). +Old syntax FlexSearch v0.6.3 (___not supported anymore!___): + ```js -const index = new Index({ worker: 4 }); +const index = new FlexSearch({ worker: 4 }); index.add(1, "some") .add(2, "content") .add(3, "to") @@ -1003,9 +1006,9 @@ The issue starts when you query a term. Each of the worker has to resolve the se The new worker model from v0.7.0 is divided into "fields" from the document (1 worker = 1 field index). This way the worker becomes able to solve tasks (subtasks) completely. The downside of this paradigm is they might not have been perfect balanced in storing contents (fields may have different length of contents). On the other hand there is no indication that balancing the storage gives any advantage (they all require the same amount in total). ```js -const index = new Index({ +const index = new Document({ doc: ["tag", "name", "title", "text"], - worker: 4 + worker: true }); index.add({ @@ -1028,7 +1031,22 @@ Worker 4: { 1: "some", 2: "content", 3: "to", 4: "index" } When you perform a field search through all fields then this task is perfectly balanced through all workers, which can solve their subtasks independently. -The main thread has to solve a last intersection calculation as before. On this step it needs to apply "bool" and "paging" logic, also "suggestions". I'm thinking about to move the workload from the main tread to another worker, so all computations will perform in background completely, the index from the main thread just holds the configuration and its document store (when using store). +### WorkerIndex (Adapter) + +Above we have seen that documents will create worker automatically for each field. You can also create a WorkerIndex directly (same like using `Index` instead of `Document`). + +```js +import WorkerIndex from "./adapter.js"; +const index = new WorkerIndex(options); +index.add(1, "some") + .add(2, "content") + .add(3, "to") + .add(4, "index"); +``` + +Such a WorkerIndex works pretty much the same as a created instance of `Index`. + +> A WorkerIndex only support the `async` variant of all methods. That means when you call `index.search()` on a WorkerIndex this will perform also in async the same way as `index.searchAsync()` will do. ## Export / Import diff --git a/src/adapter.js b/src/adapter.js new file mode 100644 index 0000000..8bf8f3c --- /dev/null +++ b/src/adapter.js @@ -0,0 +1,81 @@ +import { promise as Promise } from "./polyfill.js"; + +let counter = 0; + +/** + * @param {number|string|Object} id + * @param {Object=} options + * @constructor + */ + +function WorkerAdapter(id, options){ + + if(typeof id === "object"){ + + options = id; + id = 0; + } + + if(typeof options["encode"] === "function"){ + + options["encode"] = options["encode"].toString(); + } + + const self = this; + + this.id = id || counter++; + this.resolver = null; + this.worker = new Worker("worker.js", { type: "module" }); + this.worker.onmessage = function(e){ self.resolver(e["data"]/*["results"]*/) }; + this.worker.postMessage({ task: "create", /*id: this.id,*/ options: options }); +} + +export default WorkerAdapter; + +register("add"); +register("append"); +register("search"); +register("update"); +register("remove"); + +function register(key){ + + WorkerAdapter.prototype[key] = + WorkerAdapter.prototype[key + "Async"] = function(){ + + const self = this; + const args = [].slice.call(arguments); + const arg = args[args.length - 1]; + let callback; + + if(typeof arg === "function"){ + + callback = arg; + args.splice(args.length - 1, 1); + } + + const promise = new Promise(function(resolve){ + + self.worker.postMessage({ task: key, /*id: this.id,*/ args: args }); + + if(key === "search"){ + + self.resolver = resolve; + } + else{ + + resolve(); + } + }); + + if(callback){ + + promise.then(callback); + return this; + } + else{ + + return promise; + } + }; +} diff --git a/src/async.js b/src/async.js index 1ed8af1..76d219a 100644 --- a/src/async.js +++ b/src/async.js @@ -2,106 +2,49 @@ import Index from "./index.js"; import Document from "./document.js"; import { promise as Promise } from "./polyfill.js"; -/** - * @param {Function=} callback - */ +export default function(prototype){ -export function addAsync(id, content, callback){ - - return caller.call( - /** @type {Document|Index} */ (this), - /** @type {Document|Index} */ (this).add, - arguments - ); + register(prototype, "add"); + register(prototype, "append"); + register(prototype, "search"); + register(prototype, "update"); + register(prototype, "remove"); } -/** - * @param {Function=} callback - */ +function register(prototype, key){ -export function appendAsync(id, content, callback){ + prototype[key + "Async"] = function(){ - return caller.call( - /** @type {Document|Index} */ (this), - /** @type {Document|Index} */ (this).append, - arguments - ); -} + const self = this; + const args = /*[].slice.call*/(arguments); + const arg = args[args.length - 1]; + let callback; -/** - * @param {!string} query - * @param {number|Object|Function=} options - * @param {Function=} callback - */ + if(typeof arg === "function"){ -export function searchAsync(query, options, callback){ - - return caller.call( - /** @type {Document|Index} */ (this), - /** @type {Document|Index} */ (this).search, - arguments - ); -} - -/** - * @param {Function=} callback - */ - -export function updateAsync(id, content, callback){ - - return caller.call( - /** @type {Document|Index} */ (this), - /** @type {Document|Index} */ (this).update, - arguments - ); -} - -/** - * @param {Function=} callback - */ - -export function removeAsync(id, callback){ - - return caller.call( - /** @type {Document|Index} */ (this), - /** @type {Document|Index} */ (this).remove, - arguments - ); -} - -function caller(method, args){ - - let callback; - - for(let i = 0; i < args.length; i++){ - - if(typeof args[i] === "function"){ - - callback = args[i]; - delete args[i]; - break; + callback = arg; + delete args[args.length - 1]; } - } - const self = /** @type {Document|Index} */ (this); + const promise = new Promise(function(resolve){ - const promise = new Promise(function(resolve){ + setTimeout(function(){ - // Promises are bullshit, they will block the main thread - - setTimeout(function(){ - - resolve(method.apply(self, args)); + const fn = self[key]; + fn.async = true; + resolve(fn.apply(self, args)); + fn.async = false; + }); }); - }); - if(callback){ + if(callback){ - promise.then(callback); - return this; - } - else{ + promise.then(callback); + return this; + } + else{ - return promise; - } -} \ No newline at end of file + return promise; + } + }; +} diff --git a/src/cache.js b/src/cache.js index 76849e0..93da948 100644 --- a/src/cache.js +++ b/src/cache.js @@ -27,6 +27,7 @@ export default CacheClass; * @param {!string} query * @param {number|Object=} limit * @param {Object=} options + * @this {Document|Index} * @returns {Array} */ @@ -37,12 +38,12 @@ export function searchCache(query, limit, options){ query = query["query"]; } - let cache = /** @type {Document|Index} */ (this).cache.get(query); + let cache = this.cache.get(query); if(!cache){ - cache = /** @type {Document|Index} */ (this).search(query, limit, options); - /** @type {Document|Index} */ (this).cache.set(query, cache); + cache = this.search(query, limit, options); + this.cache.set(query, cache); } return cache; diff --git a/src/config.js b/src/config.js index e94c217..5d642f2 100644 --- a/src/config.js +++ b/src/config.js @@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = true; /** @define {boolean} */ export const SUPPORT_DOCUMENT = true; - -/** @define {boolean} */ -export const SUPPORT_PAGINATION = true; diff --git a/src/config/bundle.js b/src/config/bundle.js index e94c217..5d642f2 100644 --- a/src/config/bundle.js +++ b/src/config/bundle.js @@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = true; /** @define {boolean} */ export const SUPPORT_DOCUMENT = true; - -/** @define {boolean} */ -export const SUPPORT_PAGINATION = true; diff --git a/src/config/compact.js b/src/config/compact.js index 64e8fee..1d61270 100644 --- a/src/config/compact.js +++ b/src/config/compact.js @@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = false; /** @define {boolean} */ export const SUPPORT_DOCUMENT = true; - -/** @define {boolean} */ -export const SUPPORT_PAGINATION = false; diff --git a/src/config/light.js b/src/config/light.js index e38f9e1..92aa3f7 100644 --- a/src/config/light.js +++ b/src/config/light.js @@ -39,6 +39,3 @@ export const SUPPORT_SERIALIZE = false; /** @define {boolean} */ export const SUPPORT_DOCUMENT = false; - -/** @define {boolean} */ -export const SUPPORT_PAGINATION = false; diff --git a/src/document.js b/src/document.js index 5cfc0e9..54e011e 100644 --- a/src/document.js +++ b/src/document.js @@ -6,13 +6,24 @@ * https://github.com/nextapps-de/flexsearch */ -import { SUPPORT_ASYNC, SUPPORT_CACHE, SUPPORT_SERIALIZE, SUPPORT_STORE, SUPPORT_TAGS } from "./config.js"; +import { + + SUPPORT_ASYNC, + SUPPORT_CACHE, + SUPPORT_SERIALIZE, + SUPPORT_STORE, + SUPPORT_TAGS, + SUPPORT_WORKER + +} from "./config.js"; + import Index from "./index.js"; import Cache, { searchCache } from "./cache.js"; import { create_object } from "./common.js"; -import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js"; +import apply_async from "./async.js"; import { intersect, intersect_union } from "./intersect.js"; import { exportDocument, importDocument } from "./serialize.js"; +import WorkerAdapter from "./adapter.js"; /** * @param {Object=} options @@ -56,6 +67,16 @@ function Document(options){ options["cache"] = false; } + if(SUPPORT_WORKER){ + + this.worker = options["worker"]; + } + + if(SUPPORT_ASYNC){ + + this.async = false; + } + this.index = parse_descriptor.call(this, options); } @@ -100,7 +121,15 @@ function parse_descriptor(options){ item = options; } - index[key] = new Index(item, this.register); + if(this.worker){ + + index[key] = new WorkerAdapter(item); + } + else{ + + index[key] = new Index(item, this.register); + } + this.tree[i] = parse_tree(key, this.marker); this.field[i] = key; } @@ -425,7 +454,7 @@ Document.prototype.remove = function(id){ return this; }; -Document.prototype.search = function(query, limit, options){ +Document.prototype.search = async function(query, limit, options){ if(typeof query === "object"){ @@ -495,15 +524,38 @@ Document.prototype.search = function(query, limit, options){ field || (field = this.field); bool = bool && ((field.length > 1) || (tag && (tag.length > 1))); + let async_res = []; + + // use Promise.all to get a change of processing requests in parallel + + if(this.worker || this.async){ + + for(let i = 0, key; i < field.length; i++){ + + key = field[i]; + async_res[i] = this.index[key][this.async ? "searchAsync" : "search"](query, limit, field_options ? field_options[key] : options); + } + + async_res = await Promise.all(async_res); + } + // TODO solve this in one loop below for(let i = 0, res, key, len; i < field.length; i++){ key = field[i]; - // inherit options also when search? it is just for laziness, Object.assign() has a cost + if(this.worker || this.async){ + + res = async_res[i]; + } + else{ + + // inherit options also when search? it is just for laziness, Object.assign() has a cost + + res = this.index[key].search(query, limit, field_options ? field_options[key] : options); + } - res = this.index[key].search(query, limit, field_options ? field_options[key] : options); len = res.length; if(tag && len){ @@ -675,15 +727,11 @@ if(SUPPORT_CACHE){ if(SUPPORT_ASYNC){ - Document.prototype.addAsync = addAsync; - Document.prototype.appendAsync = appendAsync; - Document.prototype.searchAsync = searchAsync; - Document.prototype.updateAsync = updateAsync; - Document.prototype.removeAsync = removeAsync; + apply_async(Document.prototype); } if(SUPPORT_SERIALIZE){ Document.prototype.export = exportDocument; Document.prototype.import = importDocument; -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index d7c2bb4..f397843 100644 --- a/src/index.js +++ b/src/index.js @@ -7,18 +7,20 @@ */ import { + SUPPORT_PRESET, SUPPORT_CACHE, SUPPORT_ASYNC, - SUPPORT_WORKER, SUPPORT_SUGGESTION, SUPPORT_SERIALIZE + } from "./config.js"; + import { encode as default_encoder } from "./lang/latin/default.js"; import { create_object, create_object_array, concat, sort_by_length_down } from "./common.js"; import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js"; import { global_lang, global_charset } from "./global.js"; -import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js"; +import apply_async from "./async.js"; import { intersect } from "./intersect.js"; import Cache, { searchCache } from "./cache.js"; import apply_preset from "./presets.js"; @@ -116,18 +118,6 @@ function Index(options, _register){ this.cache = (tmp = options["cache"]) && new Cache(tmp); } - - if(SUPPORT_WORKER){ - - /** @private */ - this.worker = (tmp = options["worker"]) && new Worker("worker.js", { type: "module" }); - - if(tmp){ - - this.worker.onmessage = function(e){ console.log(e.data) }; - this.worker.postMessage({ task: "register", options: options }); - } - } } export default Index; @@ -775,11 +765,7 @@ if(SUPPORT_CACHE){ if(SUPPORT_ASYNC){ - Index.prototype.addAsync = addAsync; - Index.prototype.appendAsync = appendAsync; - Index.prototype.searchAsync = searchAsync; - Index.prototype.updateAsync = updateAsync; - Index.prototype.removeAsync = removeAsync; + apply_async(Index.prototype); } if(SUPPORT_SERIALIZE){ diff --git a/src/polyfill.js b/src/polyfill.js index 90cb6e9..17a039a 100644 --- a/src/polyfill.js +++ b/src/polyfill.js @@ -51,20 +51,17 @@ if(POLYFILL){ this.callback = null; - let self = this; + const self = this; - //setTimeout(function(){ + fn(function(val){ - fn(function(val){ + if(self.callback){ - if(self.callback){ - - self.callback(val); - self.callback = null; - self = null; - } - }); - //}); + self.callback(val); + // self.callback = null; + // self = null; + } + }); } /** diff --git a/src/webpack.js b/src/webpack.js index 5b5f4d9..3724eab 100644 --- a/src/webpack.js +++ b/src/webpack.js @@ -55,4 +55,4 @@ window["FlexSearch"] = { "Document": SUPPORT_DOCUMENT ? Document : null, "registerCharset": registerCharset, "registerLanguage": registerLanguage -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/worker.js b/src/worker.js index 9c0fa50..11f5254 100644 --- a/src/worker.js +++ b/src/worker.js @@ -8,43 +8,46 @@ onmessage = function(event) { switch(data["task"]){ - case "register": + case "create": const options = data["options"] || {}; options["cache"] = false; - // options["async"] = false; - // options["worker"] = false; - id = data["id"]; + + if(typeof options["encode"] === "string"){ + + options["encode"] = Function(options["encode"]); + } + index = new Index(options); break; - case "search": - - const results = index.search(data["query"], data); - - postMessage({ id, results }); - break; - case "add": - index.add(data["id"], data["content"]); + index.add.apply(index, data["args"]); + break; + + case "append": + + index.append.apply(index, data["args"]); + break; + + case "search": + + const results = index.search.apply(index, data["args"]); + //postMessage({ id: id, results: results }); + postMessage(results); break; case "update": - index.update(data["id"], data["content"]); + index.update.apply(index, data["args"]); break; case "remove": - index.remove(data["id"]); + index.remove.apply(index, data["args"]); break; - - // case "clear": - // - // index.clear(); - // break; } }; diff --git a/task/build.js b/task/build.js index 29cfe9b..448aaa6 100644 --- a/task/build.js +++ b/task/build.js @@ -59,7 +59,7 @@ var options = (function(argv){ if(index !== "RELEASE"){ - flag_str += " --define='" + index + "=" + val + "'"; + //flag_str += " --define='" + index + "=" + val + "'"; } arr[index] = val; @@ -111,8 +111,8 @@ let parameter = (function(opt){ //jscomp_error: "newCheckTypesExtraChecks", generate_exports: true, export_local_property_definitions: true, - language_in: "ECMASCRIPT6_STRICT", - language_out: language_out || "ECMASCRIPT5_STRICT", + language_in: "ECMASCRIPT_2017", + language_out: language_out || "ECMASCRIPT6_STRICT", process_closure_primitives: true, summary_detail_level: 3, warning_level: "VERBOSE",