annotate src/externals.ts @ 40:f7244f2155a3

Setup some scaffolding for bootstrapping a worker and loading into a service, providing a mechanism for implementing most of the logic which runs inside the worker in TypeScript.
author Lucas Thompson <dev@lucas.im>
date Thu, 01 Dec 2016 15:46:33 +0000
parents a1ca41c70351
children
rev   line source
dev@38 1 /**
dev@38 2 * Created by lucas on 01/12/2016.
dev@38 3 */
dev@38 4
dev@38 5 /* This is a really ad-hoc and crappy way of re-exporting modules,
dev@38 6 * whilst trying to reduce global namespace pollution
dev@38 7 * The main use case is providing access to npm modules inside a worker via importScripts
dev@38 8 * over using some compiled version (a la *.min.js, *.umd.js etc)
dev@38 9 * a better solution would be a custom webpack bundle,
dev@38 10 * but the current build system in angular-cli doesn't provide a way for custom webpack bundles
dev@38 11 * ....unless I am missing something
dev@38 12 *
dev@38 13 * this does, however, mean that modules will be loaded twice..
dev@38 14 * once by index.html and once by the worker..
dev@38 15 * one could potentially run a custom post build script
dev@38 16 * to remove the script tag in index.html generated by angular-cli for scripts.bundle
dev@38 17 * .. or find another way of doing this entirely....
dev@38 18 */
dev@38 19
dev@38 20 import extractionWorker from './app/services/feature-extraction/FeatureExtractionWorker';
dev@38 21
dev@38 22 const modules = {
dev@38 23 'feature-extraction-worker': extractionWorker,
dev@38 24 };
dev@38 25
dev@38 26 if (typeof (self as any).importScripts === 'function' /* in a worker */) {
dev@38 27
dev@38 28 self['require'] = (moduleName) => {
dev@38 29 if (modules.hasOwnProperty(moduleName))
dev@38 30 return modules[moduleName];
dev@38 31 else
dev@38 32 throw new Error(`Cannot find module '${moduleName}'`);
dev@38 33 };
dev@38 34 }