# HG changeset patch # User Lucas Thompson # Date 1480607193 0 # Node ID f7244f2155a3b2d6011ea583ee3a80d8ac137e0a # Parent e6eb133fa47c39e86679472b10c8fdea40f9fbfa 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. diff -r e6eb133fa47c -r f7244f2155a3 src/app/app.module.ts --- 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] }) diff -r e6eb133fa47c -r f7244f2155a3 src/app/services/feature-extraction/FeatureExtractionWorker.ts --- /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); + } +} diff -r e6eb133fa47c -r f7244f2155a3 src/app/services/feature-extraction/feature-extraction.service.spec.ts --- /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(); + })); +}); diff -r e6eb133fa47c -r f7244f2155a3 src/app/services/feature-extraction/feature-extraction.service.ts --- /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); + }; + } + +} diff -r e6eb133fa47c -r f7244f2155a3 src/bootstrap-feature-extraction-worker.js --- /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);