angularjs - Is it possible to run Angular in a web worker? -
angularjs - Is it possible to run Angular in a web worker? -
i build spa app angular , have angular service "webservice" shared web worker. objective have 1 "webservice" shared can utilize same service in background (in web worker) , in front-end (the angular app). feasible ?
additional info: thought here synchronisation of info on remote server, have main app working "online|offline" mode, saving info local web-storage and|or remote server (using "webservice") , worker transparently using same service sync data...
could show code run app in worker ? give thanks feedback
yes, it's possible. bit of hack of web worker environment, can run angular in web worker, , have module used in both foreground , worker angular apps. reply takes , and extends code another of answers regarding unit testing
in main app, in factory/service, can have like
var worker = new $window.worker('worker.js');
then in worker.js
, can modify global scope plenty angular load:
// angular needs global window object self.window = self; // skeleton properties angular load , bootstrap. self.history = {}; self.document = { readystate: 'complete', queryselector: function() {}, createelement: function() { homecoming { pathname: '', setattribute: function() {} } } }; // load angular: must on same domain script self.importscripts('angular.js'); // set angular on global scope self.angular = window.angular; // standard angular module definitions self.importscripts('worker-app.js'); self.importscripts('shared-module.js'); // no root element seems work fine self.angular.bootstrap(null, ['worker-app']);
and can define shared-module
other:
angular.module('shared-module', []) .factory('sharedfactory', function() { homecoming { myfunc: function() { homecoming 'some-data'; } }; });
in worker-app.js
, can define main app, using shared module
var workerapp = angular.module('worker-app', ['shared-module']);
injecting shared components, example, run
callback of worker app.
workerapp.run(function(sharedfactory, $window) { $window.onmessage = function(e) { var workerresult = sharedfactory.myfunc(); $window.postmessage(workerresult); }; });
in foreground angular app, can include shared-module.js
via usual script tag, , utilize dependency injection access components usual
var foregroundapp = angular.module('forground-app', ['shared-module']); foregroundapp.controller(function(sharedfactory, $scope) { // utilize sharedfactory needed });
to confirm, instances of shared-module
in foreground , worker app different.
angularjs web-worker
Comments
Post a Comment