This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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}), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//workerArray.js | |
const ports = {}; | |
let cache = {}; | |
let queue; | |
function initiatePort (workerName, port) { | |
ports[workerName] = port; | |
const webWorker = ports[workerName]; | |
webWorker.inUse = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//webWorker.js | |
self.importScripts('...the search engine script, provides the SearchEngine constructor'); | |
let searchEngine; | |
let cache = {} | |
function initiateSearchEngine (data) { | |
searchEngine = new SearchEngine(data); | |
cache = {}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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') |
NewerOlder