# HG changeset patch # User Lucas Thompson # Date 1494927895 -3600 # Node ID 38886ce7e2e5d79ab56482a8dca6bec74edda065 # Parent 89208e8af8cc9c5283aae72fea5a4ad2e165f905 Instantiate services on list and process, trying to ensure only one emscripten module instantiated at a time (hopefully allowing for garbage collection). diff -r 89208e8af8cc -r 38886ce7e2e5 src/app/services/feature-extraction/FeatureExtractionWorker.ts --- a/src/app/services/feature-extraction/FeatureExtractionWorker.ts Tue May 16 00:28:27 2017 +0100 +++ b/src/app/services/feature-extraction/FeatureExtractionWorker.ts Tue May 16 10:44:55 2017 +0100 @@ -2,7 +2,7 @@ * Created by lucas on 01/12/2016. */ -import {PiperVampService, ListRequest, ListResponse} from 'piper'; +import {PiperVampService, ListRequest, ListResponse, Service} from 'piper'; import { SimpleRequest } from 'piper/HigherLevelUtilities'; @@ -31,25 +31,28 @@ type LibraryKey = string; type RequireJs = (libs: string[], callback: (...libs: any[]) => void) => void; +type Factory = () => T; class AggregateStreamingService implements StreamingService { - private services: Map; + private services: Map>; constructor() { - this.services = new Map(); - // this.services.set( - // 'vamp-example-plugins', - // new PiperStreamingService(new PiperVampService(VampExamplePlugins())) - // ); + this.services = new Map>(); + this.services.set( + 'vamp-example-plugins', + () => new PiperStreamingService( + new PiperVampService(VampExamplePlugins()) + ) + ); } - addService(key: LibraryKey, service: PiperStreamingService): void { + addService(key: LibraryKey, service: Factory): void { this.services.set(key, service); } list(request: ListRequest): Promise { return Promise.all( - [...this.services.values()].map(client => client.list({})) + [...this.services.values()].map(client => client().list({})) ).then(allAvailable => ({ available: allAvailable.reduce( (all, current) => all.concat(current.available), @@ -66,8 +69,8 @@ protected dispatch(method: 'process', request: SimpleRequest): Observable { const key = request.key.split(':')[0]; - return this.services.has(key) ? - this.services.get(key)[method](request) : Observable.throw('Invalid key'); + return this.services.has(key) ? this.services.get(key)()[method](request) : + Observable.throw('Invalid key'); } } @@ -138,12 +141,15 @@ const key: LibraryKey = ev.data.params; if (this.remoteLibraries.has(key)) { this.requireJs([this.remoteLibraries.get(key)], (plugin) => { - // TODO a factory with more logic probably belongs in piper-js - const lib: any | EmscriptenModule = plugin.createLibrary(); - const isEmscriptenModule = typeof lib.cwrap === 'function'; - const service = new PiperStreamingService( - isEmscriptenModule ? new PiperVampService(lib) : lib // TODO - ); + + const service = () => { + // TODO a factory with more logic probably belongs in piper-js + const lib: any | EmscriptenModule = plugin.createLibrary(); + const isEmscriptenModule = typeof lib.cwrap === 'function'; + return new PiperStreamingService( + isEmscriptenModule ? new PiperVampService(lib) : lib // TODO + ); + }; this.service.addService(key, service); this.service.list({}).then(sendResponse); });