Search code examples
angularweb-worker

Angular 2 Web Worker - UI not running


I've got an angular2 application running in a web worker. By the looks of it, everything is running as I would expect, but nothing seems to be happening with the DOM. For example, I show a pre-loader while the app is loading, but it never gets replaced with the actual application UI.

<my-app>Loading...</my-app>

When the AppModule loads, nothing renders in my-app... even though I see all my components and services running exactly as I would expect inside the background worker... the whole app is running fine - just no UI.

import { NgModule } from '@angular/core';
import { HttpModule, XSRFStrategy } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { WorkerAppModule } from '@angular/platform-webworker';
import { WORKER_APP_LOCATION_PROVIDERS } from '@angular/platform-webworker';

import { AppComponent } from './app.component';
import { NoXSRFStrategy } from './common/backends/XSRFStrategies';

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        FormsModule,
        routing,  

        WorkerAppModule         
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        { provide: XSRFStrategy, useClass: NoXSRFStrategy },

        WORKER_APP_LOCATION_PROVIDERS           
    ],
    bootstrap: [AppComponent]
})
export class AppModule {    
    constructor() { }    
}

In my main.ts

UI_THREAD_CHANNEL = 'UI_THREAD_CHANNEL'
bootstrapWorkerUi('/webworker-loader.js').then((hWnd) => {

                // register methods the WebWorker needs to run on the UI thread.
                let brokerFactory: ServiceMessageBrokerFactory = hWnd.injector.get(ServiceMessageBrokerFactory);
                let broker = brokerFactory.createMessageBroker(UI_THREAD_CHANNEL, false);

                broker.registerMethod('redirect', [PRIMITIVE], (href: string) => {
                    window.location.href = href;
                });
            });

In my webworker-main.ts

import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app.module';
const platform = platformWorkerAppDynamic();
platform.bootstrapModule(AppModule);

Everything works as I would expect when I don't use the web-worker. I'm really close to having this working I believe... Just can't find what I'm missing. I suspect there is either something I'm not loading - or something I'm not aware of related to routing.


Solution

  • Figured it out.

    The issue is that an additional provider must be supplied to bootstrapWorkerUi in main.ts - specifically WORKER_UI_LOCATION_PROVIDERS from @angular/platform-webworker.

    The new (working) main.ts

    import { WORKER_UI_LOCATION_PROVIDERS, bootstrapWorkerUi, 
    ServiceMessageBrokerFactory } from '@angular/platform-webworker';
    
    UI_THREAD_CHANNEL = 'UI_THREAD_CHANNEL'
    bootstrapWorkerUi('/webworker-loader.js', WORKER_UI_LOCATION_PROVIDERS).then((hWnd) => {
    
        // register methods the WebWorker needs to run on the UI thread.
        let brokerFactory: ServiceMessageBrokerFactory = hWnd.injector.get(ServiceMessageBrokerFactory);
        let broker = brokerFactory.createMessageBroker(UI_THREAD_CHANNEL, false);
    
        broker.registerMethod('redirect', [PRIMITIVE], (href: string) => {
            window.location.href = href;
        });
    });