Skip to content

Instantly share code, notes, and snippets.

@leofab86
leofab86 / workerArrayController.js
Last active May 28, 2019 17:07
Fuzzy search automcomplete v1.2.4
//workerArrayController.js
export default class WorkerArrayController {
constructor ({data, handleResults, arraySize}) {
this.workerArray = new Worker('... path to workerArray.js');
let i = 1;
this.webWorkers = {};
while (i <= arraySize) {
const workerName = `ww${i}`;
@leofab86
leofab86 / ww1.js
Last active May 28, 2019 15:47
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;
@leofab86
leofab86 / webWorker.js
Last active May 23, 2019 18:08
Fuzzy search autocomplete v1.2.2
//webWorker.js
self.importScripts('...the search engine script, provides the SearchEngine constructor');
let searchEngine;
let cache = {}
//thought I would add a simple cache... Wait till you see those deletes now :)
function initiateSearchEngine (data) {
//initiate the search engine with the 13,000 item data set
@leofab86
leofab86 / searchResults-setTimeout.js
Last active May 23, 2019 17:05
Fuzzy search autocomplete v1.1.1
//searchResults.js
componentDidUpdate(prevProps) {
const {searchTerm, searchEngine} = this.props;
if(searchTerm && searchTerm !== prevProps.searchTerm) {
/* stick the update with the expensive search method into
a setTimeout callback: */
const setTimeoutCallback = () => {
this.setState({
searchResults: searchEngine.search(searchTerm)
@leofab86
leofab86 / searchResults-fetch.js
Last active May 23, 2019 17:04
Fuzzy search autocomplete v1.2.0
//searchResults.js
componentDidUpdate(prevProps) {
const {searchTerm, searchEngine, data} = this.props;
if(searchTerm && searchTerm !== prevProps.searchTerm) {
/* ping the search route with the searchTerm and update state
with the results when they return: */
fetch(`testSearch`, {
method: 'POST',
body: JSON.stringify({data, searchTerm}),
@leofab86
leofab86 / searchResults-promise.js
Last active May 23, 2019 17:03
Fuzzy search autocomplete v1.1.1
//searchResults.js
componentDidUpdate(prevProps) {
const {searchTerm, searchEngine} = this.props;
if(searchTerm && searchTerm !== prevProps.searchTerm) {
/* stick the update with the expensive search method into
a promise callback: */
Promise.resolve().then(() => {
this.setState({
searchResults: searchEngine.search(searchTerm)
@leofab86
leofab86 / workerArray.js
Last active May 23, 2019 16:46
Fuzzy search automcomplete v1.2.4
//workerArray.js
const ports = {};
let cache = {};
let queue;
function initiatePort (workerName, port) {
ports[workerName] = port;
const webWorker = ports[workerName];
webWorker.inUse = false;
@leofab86
leofab86 / searchResults-confirm.js
Last active May 23, 2019 16:33
Fuzzy search autocomplete v1.2.2
//searchResults.js
export default class SearchResults extends React.Component {
constructor (props) {
super();
this.state = {
searchResults: [],
}
this.webWorker = new Worker('...path to webWorker.js')
this.webWorker.postMessage({data: props.data})
@leofab86
leofab86 / webWorker-confirm.js
Last active May 23, 2019 16:32
Fuzzy search autocomplete v1.2.2
//webWorker.js
self.importScripts('...the search engine script, provides the SearchEngine constructor');
let searchEngine;
let cache = {}
function initiateSearchEngine (data) {
searchEngine = new SearchEngine(data);
cache = {};
}
@leofab86
leofab86 / searchResults.js
Last active May 23, 2019 04:38
Fuzzy search autocomplete v1.2.2
//searchResults.js
export default class SearchResults extends React.Component {
constructor (props) {
super();
this.state = {
searchResults: [],
}
//initiate the webworker:
this.webWorker = new Worker('...path to webWorker.js')