Search code examples
angularweb-worker

Working example for web worker with router


Is there any working example (plunkr) for angular2 BETA 6 web worker with router?

UPDATE:

My real issue is that there is no uptodate documentation on this for beta... but as suggested...

My current issue is that I am able to load angular2, but the application itself is not initiated.

app/main.ts

import 'es6-shim';
import 'zone.js/dist/zone.min.js';
import 'reflect-metadata';
import 'rxjs/add/operator/map';
import {platform, Provider} from 'angular2/core';
import {
  WORKER_RENDER_APPLICATION,
  WORKER_RENDER_PLATFORM,
  WORKER_SCRIPT
} from 'angular2/platform/worker_render';

platform([WORKER_RENDER_PLATFORM])
  .application([
    WORKER_RENDER_APPLICATION,
    new Provider(WORKER_SCRIPT, {useValue: 'loader.js'})
  ]);

loader.js

importScripts('jspm_packages/system.src.js', 'config.js');
System.import('./app/worker')
    .catch(function (error) {
    console.error('ERROR @ loader :', error);
});

If I use .application function, I get error:

Error: Cannot use asyncronous app initializers with application. Use asyncApplication instead

worker.ts (.application function)

import {Provider, provide, platform, PLATFORM_DIRECTIVES} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {App} from './app';

import {WORKER_APP_APPLICATION, WORKER_APP_PLATFORM, WORKER_APP_ROUTER} from 'angular2/platform/worker_app';

platform([WORKER_APP_PLATFORM])
    .application([
        WORKER_APP_APPLICATION,
        ROUTER_PROVIDERS,
        HTTP_PROVIDERS,
        WORKER_APP_ROUTER
    ])
     .then((ref) => {
         console.log('bootstrapping', ref);
         ref.bootstrap(App)
     });

If I use asyncApplication, than the first console log is written out, but in the bootstrap function the second not...

main.ts (.asyncApplication function)

import {Provider, provide, platform, PLATFORM_DIRECTIVES} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {App} from './app';

import {WORKER_APP_APPLICATION, WORKER_APP_PLATFORM, WORKER_APP_ROUTER} from 'angular2/platform/worker_app';

platform([WORKER_APP_PLATFORM])
    .asyncApplication(() => {
        console.log('async app load');
        return Promise.resolve([
            ROUTER_PROVIDERS,
            WORKER_APP_ROUTER,
            WORKER_APP_APPLICATION,
            HTTP_PROVIDERS
        ]);
    })
     .then((ref) => {
         console.log('bootstrapping', ref);
         ref.bootstrap(App)
     });

How can I initiate the web worker part to be able to load the main component (App)?


Solution

  • I have found this sample in the angular repository: https://github.com/angular/angular/tree/master/modules/playground/src/web_workers/router

    And actually the problem was that I didn't include: WORKER_RENDER_ROUTER in the main.ts (UI part)

    Also didnt add to the W_W part:

    new Provider(LocationStrategy, {useClass: HashLocationStrategy})
    

    So the full code if somebody interested:

    worker.ts:

    import 'es6-shim';
    import 'zone.js/dist/zone.min.js';
    import 'reflect-metadata';
    import 'rxjs/add/operator/map';
    import {Provider, provide, platform, PLATFORM_DIRECTIVES} from 'angular2/core';
    import {ROUTER_PROVIDERS} from 'angular2/router';
    import {HashLocationStrategy, LocationStrategy} from "angular2/router";
    import {HTTP_PROVIDERS} from 'angular2/http';
    import {App} from './app';
    
    import {
        WORKER_APP_APPLICATION,
        WORKER_APP_PLATFORM,
        WORKER_APP_ROUTER
    } from 'angular2/platform/worker_app';
    
    let platformRef = platform([WORKER_APP_PLATFORM])
        .asyncApplication(null,[
            WORKER_APP_APPLICATION,
            WORKER_APP_ROUTER,
            HTTP_PROVIDERS,
            new Provider(LocationStrategy, {useClass: HashLocationStrategy})
        ]);
    platformRef.then((ref) => {
        ref.bootstrap(App)
    });
    

    main.ts:

    import 'es6-shim';
    import 'zone.js/dist/zone.min.js';
    import 'reflect-metadata';
    import 'rxjs/add/operator/map';
    import {platform, Provider} from 'angular2/core';
    import {
      WORKER_RENDER_APP,
      WORKER_RENDER_PLATFORM,
      WORKER_SCRIPT,
      WORKER_RENDER_ROUTER
    } from 'angular2/platform/worker_render';
    import {BrowserPlatformLocation} from "angular2/src/router/browser_platform_location";
    import {MessageBasedPlatformLocation} from "angular2/src/web_workers/ui/platform_location";
    
    platform([WORKER_RENDER_PLATFORM])
      .application([
        WORKER_RENDER_APP,
        new Provider(WORKER_SCRIPT, {useValue: 'loader.js'}),
        WORKER_RENDER_ROUTER
      ]);