changeset 40:f7244f2155a3

Setup some scaffolding for bootstrapping a worker and loading into a service, providing a mechanism for implementing most of the logic which runs inside the worker in TypeScript.
author Lucas Thompson <dev@lucas.im>
date Thu, 01 Dec 2016 15:46:33 +0000
parents e6eb133fa47c
children 738d366f6083
files src/app/app.module.ts src/app/services/feature-extraction/FeatureExtractionWorker.ts src/app/services/feature-extraction/feature-extraction.service.spec.ts src/app/services/feature-extraction/feature-extraction.service.ts src/bootstrap-feature-extraction-worker.js
diffstat 5 files changed, 74 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/app.module.ts	Thu Dec 01 15:45:07 2016 +0000
+++ b/src/app/app.module.ts	Thu Dec 01 15:46:33 2016 +0000
@@ -9,6 +9,7 @@
 import { AudioFileOpenComponent } from './audio-file-open/audio-file-open.component';
 import { PlaybackControlComponent } from './playback-control/playback-control.component';
 import { AudioPlayerService } from "./services/audio-player/audio-player.service";
+import { FeatureExtractionService } from "./services/feature-extraction/feature-extraction.service";
 
 function createAudioContext(): AudioContext {
   return new (
@@ -33,7 +34,8 @@
   providers: [
     {provide: HTMLAudioElement, useValue: new Audio()}, // TODO use something more generic than HTMLAudioElement
     {provide: 'AudioContext', useValue: createAudioContext()}, // use a string token, Safari doesn't seem to like AudioContext
-    AudioPlayerService
+    AudioPlayerService,
+    FeatureExtractionService
   ],
   bootstrap: [AppComponent]
 })
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/app/services/feature-extraction/FeatureExtractionWorker.ts	Thu Dec 01 15:46:33 2016 +0000
@@ -0,0 +1,27 @@
+/**
+ * Created by lucas on 01/12/2016.
+ */
+
+// TODO TypeScript has a .d.ts file for webworkers, but for some reason it clashes with the typings for dom and causes compiler errors
+interface WorkerGlobalScope {
+  onmessage: (this: this, ev: MessageEvent) => any;
+  postMessage(data: any): void;
+}
+
+interface MessageEvent {
+  readonly data: any;
+}
+
+export default class FeatureExtractionWorker {
+  private workerScope: WorkerGlobalScope;
+
+  constructor(workerScope: WorkerGlobalScope) {
+    console.log('ctor');
+    this.workerScope = workerScope;
+    this.workerScope.onmessage = (ev: MessageEvent) => {
+      console.log(ev.data);
+    };
+    let counter = 0;
+    setInterval(() => this.workerScope.postMessage(counter++), 1000);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/app/services/feature-extraction/feature-extraction.service.spec.ts	Thu Dec 01 15:46:33 2016 +0000
@@ -0,0 +1,16 @@
+/* tslint:disable:no-unused-variable */
+
+import { TestBed, async, inject } from '@angular/core/testing';
+import { FeatureExtractionService } from './feature-extraction.service';
+
+describe('FeatureExtractionService', () => {
+  beforeEach(() => {
+    TestBed.configureTestingModule({
+      providers: [FeatureExtractionService]
+    });
+  });
+
+  it('should ...', inject([FeatureExtractionService], (service: FeatureExtractionService) => {
+    expect(service).toBeTruthy();
+  }));
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/app/services/feature-extraction/feature-extraction.service.ts	Thu Dec 01 15:46:33 2016 +0000
@@ -0,0 +1,19 @@
+import { Injectable } from '@angular/core';
+
+@Injectable()
+export class FeatureExtractionService {
+
+  private worker: Worker;
+
+  constructor() {
+    this.worker = new Worker('bootstrap-feature-extraction-worker.js');
+  }
+
+  testMessageStream() {
+    this.worker.postMessage('anything');
+    this.worker.onmessage = (ev: MessageEvent) => {
+      console.log(ev.data);
+    };
+  }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bootstrap-feature-extraction-worker.js	Thu Dec 01 15:46:33 2016 +0000
@@ -0,0 +1,9 @@
+/**
+ * Created by lucas on 01/12/2016.
+ */
+let window = {};
+importScripts('inline.bundle.js'); // provides webpackJsonp
+const webpackJsonp = window['webpackJsonp'];
+importScripts('scripts.bundle.js'); // needs webpackJsonp, hence above - provides require
+
+new (require('feature-extraction-worker'))(self);