Coder Social home page Coder Social logo

rolaveric / angular-webworker-playground Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 173 KB

Playground and testing project for Angular's `platform-webworker`

Home Page: https://legacy-to-the-edge.com/2018/05/01/platform-webworker-in-angular/

License: MIT License

TypeScript 36.62% JavaScript 10.76% HTML 52.16% CSS 0.45%
angular web-worker

angular-webworker-playground's Introduction

angular-webworker-playground

Playground for seeing platform-webworker from Angular in action, and testing popular UI libraries with it.

This project is a typical Angular CLI built application, with a few additions:

  • src/lib/* folders for different platforms.
  • Scripts and entry points for all 3 platform types: browser, server, and webworker: npm/yarn run start-browser, npm/yarn run start-server, and npm/yarn run start-webworker

Tips for using platform-webworker

To call a service on the UI at startup, add it to the PLATFORM_INITIALIZER multi provider:

function platformInitFnFactory(injector: Injector) {
  return () => {
    const zone: NgZone = injector.get(NgZone);
    zone.runGuarded(() => injector.get(MyUiService).start());
  };
}

export const platformInitMyUiServiceProvider: FactoryProvider = {
  provide: PLATFORM_INITIALIZER,
  useFactory: platformInitFnFactory,
  multi: true,
  deps: [Injector]
};

To expose methods on the UI, use ServiceMessageBrokerFactory:

  • Create a message broker using createMessageBroker(CHANNEL), where CHANNEL is the same string used by the service on the UI and the client on the Worker.
constructor(
  private messageBrokerFactory: ServiceMessageBrokerFactory,
  private myService: MyService
) {
  this.messageBroker = this.messageBrokerFactory.createMessageBroker(MY_SERVICE_CHANNEL);
}
  • Call registerMethod() on the message broker to expose a method to the client, in a method called by the PLATFORM_INITIALIZER factory:
start() {
  this.messageBroker.registerMethod(
    // Method name
    'myMethod',
    // Expected argument types: 1 === Primitive, 2 === Render store object (ie. HTML element) 
    [1, 2],
    // Actual function
    this.myService.myMethod.bind(this.myService),
    // Expected return type (null for nothing)
    PRIMITIVE
  );
}

To call methods on the UI from the Worker, use ClientMessageBrokerFactory:

  • Create a message broker using createMessageBroker(CHANNEL), where CHANNEL is the same string used by the service on the UI and the client on the Worker.
  • Call runOnService() on the message broker, using FnArg and UiArguments to define the method and arguments:
myMethod(value: string, nativeElement: any): Promise<boolean> {
  const fnArgs = [
    new FnArg(value, 1), // 1 === Primitive
    new FnArg(nativeElement, 2) // 2 === Render store object (ie. HTML element)
  ];
  const args = new UiArguments('myMethod', fnArgs);
  return this.messageBroker.runOnService(args, PRIMITIVE) as Promise<boolean>;
}

To call a service on the Worker at startup (eg. To load data from the UI), add it to the APP_INITIALIZER multi provider:

export function appInitFnFactory(myService: MyWorkerService, zone: NgZone) {
  return () => zone.runGuarded(() => myService.init());
}

export const appInitMyServiceProvider: FactoryProvider = {
  provide: APP_INITIALIZER,
  useFactory: appInitFnFactory,
  multi: true,
  deps: [MyWorkerService, NgZone]
};

Tips for using platform-server

Handling DOM interactions in platform-server is much simpler for 2 reasons:

  1. platform-server uses Domino to simulate the DOM on the server
  2. platform-server shouldn't need to deal with most key or mouse events, since it's just rendering the first paint

The important things with platform-server are:

  • Use TransferState to avoid redoing work.
  • If platform-server takes too long, your UX will be a blank page till it's ready. If you're just using platform-server for SEO, or if your #1 priority is battery optimization, that's fine. Otherwise you should prevent platform-server from running anything that takes too long.

Known platform-webworker issues

  • $event.target is undefined for all mouse events
  • $event.target.checked is undefined for change events
  • Code splitting/lazy loading does not currently work
    This is more an issue with Webpack, which sets up everything expecting to use window and JSONP.
    Likely a Webpack plugin/loader that can solve this.

TODO

  • Demo Angular features - specially those with known issues
  • Test Angular v6's providedIn: root behavior
  • Demo ngx-bootstrap UI components
  • Demo Angular Material UI components
  • Log github issues for the problems that were found
  • Find other UI libraries to test
  • Setup scripts to switch CSS frameworks (bootstrap 3, bootstrap 4, and material)
  • Find webpack plugin/loader for lazy loading on web workers

angular-webworker-playground's People

Contributors

rolaveric avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.