UNPKG

@cleerlycode/cornerstone-wado-image-loader

Version:
175 lines (135 loc) 5.91 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>webWorker/webWorker.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#addTask">addTask</a></li><li><a href="global.html#arrayBufferToString">arrayBufferToString</a></li><li><a href="global.html#cacheSizeInBytes">cacheSizeInBytes</a></li><li><a href="global.html#cancelTask">cancelTask</a></li><li><a href="global.html#framesAreFragmented">framesAreFragmented</a></li><li><a href="global.html#getNumberString">getNumberString</a></li><li><a href="global.html#getNumberValues">getNumberValues</a></li><li><a href="global.html#getStatistics">getStatistics</a></li><li><a href="global.html#getTransferSyntaxForContentType">getTransferSyntaxForContentType</a></li><li><a href="global.html#getUncompressedImageFrame">getUncompressedImageFrame</a></li><li><a href="global.html#getValue">getValue</a></li><li><a href="global.html#handleMessageFromWorker">handleMessageFromWorker</a></li><li><a href="global.html#handler">handler</a></li><li><a href="global.html#initialize">initialize</a></li><li><a href="global.html#loadCodecs">loadCodecs</a></li><li><a href="global.html#loadWebWorkerTask">loadWebWorkerTask</a></li><li><a href="global.html#registerLoaders">registerLoaders</a></li><li><a href="global.html#registerTaskHandler">registerTaskHandler</a></li><li><a href="global.html#setPixelDataType">setPixelDataType</a></li><li><a href="global.html#setTaskPriority">setTaskPriority</a></li><li><a href="global.html#spawnWebWorker">spawnWebWorker</a></li><li><a href="global.html#startTaskOnWebWorker">startTaskOnWebWorker</a></li><li><a href="global.html#unpackBinaryFrame">unpackBinaryFrame</a></li></ul> </nav> <div id="main"> <h1 class="page-title">webWorker/webWorker.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>// an object of task handlers const taskHandlers = {}; // Flag to ensure web worker is only initialized once let initialized = false; // the configuration object passed in when the web worker manager is initialized let config; /** * Initialization function that loads additional web workers and initializes them * @param data */ function initialize (data) { // console.log('web worker initialize ', data.workerIndex); // prevent initialization from happening more than once if (initialized) { return; } // save the config data config = data.config; // load any additional web worker tasks if (data.config.webWorkerTaskPaths) { for (let i = 0; i &lt; data.config.webWorkerTaskPaths.length; i++) { self.importScripts(data.config.webWorkerTaskPaths[i]); } } // initialize each task handler Object.keys(taskHandlers).forEach(function (key) { taskHandlers[key].initialize(config.taskConfiguration); }); // tell main ui thread that we have completed initialization self.postMessage({ taskType: 'initialize', status: 'success', result: { }, workerIndex: data.workerIndex }); initialized = true; } /** * Function exposed to web worker tasks to register themselves * @param taskHandler */ export function registerTaskHandler (taskHandler) { if (taskHandlers[taskHandler.taskType]) { console.log('attempt to register duplicate task handler "', taskHandler.taskType, '"'); return false; } taskHandlers[taskHandler.taskType] = taskHandler; if (initialized) { taskHandler.initialize(config.taskConfiguration); } } /** * Function to load a new web worker task with updated configuration * @param data */ function loadWebWorkerTask (data) { config = data.config; self.importScripts(data.sourcePath); } /** * Web worker message handler - dispatches messages to the registered task handlers * @param msg */ self.onmessage = function (msg) { // console.log('web worker onmessage', msg.data); // handle initialize message if (msg.data.taskType === 'initialize') { initialize(msg.data); return; } // handle loadWebWorkerTask message if (msg.data.taskType === 'loadWebWorkerTask') { loadWebWorkerTask(msg.data); return; } // dispatch the message if there is a handler registered for it if (taskHandlers[msg.data.taskType]) { taskHandlers[msg.data.taskType].handler(msg.data, function (result, transferList) { self.postMessage({ taskType: msg.data.taskType, status: 'success', result, workerIndex: msg.data.workerIndex }, transferList); }); return; } // not task handler registered - send a failure message back to ui thread console.log('no task handler for ', msg.data.taskType); console.log(taskHandlers); self.postMessage({ taskType: msg.data.taskType, status: 'failed - no task handler registered', workerIndex: msg.data.workerIndex }); }; </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>