1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-10-01 15:46:40 +02:00
Files
flexsearch/dist/module/worker.js
2021-06-05 13:38:59 +02:00

1 line
2.2 KiB
JavaScript

import FlexSearch from"./flexsearch.js";const worker_stack={},inline_supported="undefined"!=typeof Blob&&"undefined"!=typeof URL&&URL.createObjectURL;export default function init(a,b,c,d,e){let f=a;const g=inline_supported?URL.createObjectURL(new Blob([(RELEASE?"":"var RELEASE = '"+RELEASE+"';var DEBUG = false;var PROFILER = false;var SUPPORT_PRESET = true;var SUPPORT_SUGGESTION = true;var SUPPORT_ENCODER = true;var SUPPORT_CACHE = true;var SUPPORT_ASYNC = true;var SUPPORT_SERIALIZE = true;var SUPPORT_INFO = true;var SUPPORT_DOCUMENT = true;var SUPPORT_WHERE = true;var SUPPORT_PAGINATION = true;var SUPPORT_OPERATOR = true;var SUPPORT_CALLBACK = true;var SUPPORT_WORKER = true;")+"("+c.toString()+")()"],{type:"text/javascript"})):f+(RELEASE?"."+RELEASE:"")+".js";return f+="-"+b,worker_stack[f]||(worker_stack[f]=[]),worker_stack[f][e]=new Worker(g),worker_stack[f][e].onmessage=d,!1,worker_stack[f][e]}function worker_module(){let a,b;self.onmessage=function(c){const d=c.data;if(d)if(d.search){const c=b.search(d.content,d.threshold?{limit:d.limit,threshold:d.threshold,where:d.where}:d.limit);self.postMessage({id:a,content:d.content,limit:d.limit,result:c})}else if(d.add)b.add(d.id,d.content);else if(d.update)b.update(d.id,d.content);else if(d.remove)b.remove(d.id);else if(d.clear)b.clear();else if(d.info){const c=b.info();c.worker=a,console.log(c)}else d.register&&(a=d.id,d.options.cache=!1,d.options.async=!1,d.options.worker=!1,b=new Function(d.register.substring(d.register.indexOf("{")+1,d.register.lastIndexOf("}")))(),b=new b(d.options))}}export function addWorker(a,b,c,d){const e=init("flexsearch","id"+a,worker_module,function(a){const b=a.data;b&&b.result&&d(b.id,b.content,b.result,b.limit,b.where,b.cursor,b.suggest)},b),f=FlexSearch.toString();return c.id=b,e.postMessage({register:f,options:c,id:b}),e}FlexSearch.prototype.worker_handler=function(a,b,c,d){return this._task_completed!==this.worker&&(this._task_result=this._task_result.concat(c),this._task_completed++,d&&this._task_result.length>=d&&(this._task_completed=this.worker),this._task_completed===this.worker&&(this.cache&&this._cache.set(b,this._task_result),this._current_callback&&this._current_callback(this._task_result))),this};