Skip to content

Instantly share code, notes, and snippets.

@leofab86
Last active May 28, 2019 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leofab86/c4c6bd52bb99addd21e78a62144b6f4b to your computer and use it in GitHub Desktop.
Save leofab86/c4c6bd52bb99addd21e78a62144b6f4b to your computer and use it in GitHub Desktop.
Fuzzy search automcomplete v1.2.4
//ww1.js
self.importScripts('...the search engine script, provides the SearchEngine constructor');
let searchEngine;
let port;
function initiate (data, port) {
searchEngine = new SearchEngine(data);
port = port;
port.onmessage = search;
}
/* search is attached to the port as the message handler so it
runs when communicating with the workerArray only */
function search (e) {
const {searchTerm} = e.data;
const message = {
searchResults: searchEngine.search(searchTerm)
};
port.postMessage(message)
}
/* self.onmessage is the handler that responds to messages from
the main thread, which only fires during initiation */
self.onmessage = function(e) {
const {data} = e.data;
initiate(data, e.ports[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment