Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 web workers with ES5

I have gone down a path of using Angular2 but writing ES5 code, which means that the examples and guidance I find usually must be translated from answers relevant to TypeScript.

Can someone help me with an ES5 version of:

Bootstrapping the application. In TypeScript I see it done as:

import {WORKER_APP_PLATFORM, WORKER_APP_APPLICATION} from "angular2/platform/worker_app";
import {platform} from "angular2/core";
platform([WORKER_APP_PLATFORM]).application([WORKER_APP_APPLICATION]).bootstrap(myApp)

Accessing the web_workers component:

import {Component} from 'angular2/web_worker/worker';
@Component({ ... ])

I assumed the latter would be achieved by calling

ng.web_worker.worker.Component({ ... })

But that seems to not be the case: ng.web_worker is undefined.

The problem might be that I seem to not be able to include web_worker/ui.js properly. When I include it instead of bundles/angular2-all.umd.js I only get error messages about require being undefined. When I explicitly include RequireJS in my project I get a bunch of other errors.

Any help will be greatly appreciated!

like image 515
MatsGA Avatar asked Jan 13 '16 13:01

MatsGA


People also ask

Does angular use web workers?

Web workers enables JavaScript application to run the CPU-intensive in the background so that the application main thread concentrate on the smooth operation of UI. Angular provides support for including Web workers in the application.

How many Web Workers Web Workers can run concurrently JavaScript?

A web worker is a JavaScript program running on a different thread, in parallel with main thread. The browser creates one thread per tab. The main thread can spawn an unlimited number of web workers, until the user's system resources are fully consumed.

Is Web worker a thread?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.


1 Answers

Can I ask, why do you want to do it with ES5? You can easily use SystemJS and ES6 if you don't like Typescript. Typescript would be almost identical to ES6.

In case you still want to do it with ES5, you need to change the imports to require calls:

var WORKER_APP_PLATFORM = require("angular2/platform/worker_app").WORKER_APP_PLATFORM;
var WORKER_APP_APPLICATION = require("angular2/platform/worker_app").WORKER_APP_APPLICATION;
var platform = require("angular2/core").platform;
platform([WORKER_APP_PLATFORM]).application([WORKER_APP_APPLICATION]).bootstrap(myApp)

Another example:

var Component = require('angular2/web_worker/worker').Component;

You get the idea. You also don't need RequireJS... You can use SystemJS. You should import the main file like this:

System.import("main")

SystemJS will resolve all the require calls async, but in the code, the require calls look sync.

like image 163
carlosdubusm Avatar answered Sep 21 '22 06:09

carlosdubusm