comparison src/app/services/feature-extraction/FeatureExtractionWorker.ts @ 322:38886ce7e2e5

Instantiate services on list and process, trying to ensure only one emscripten module instantiated at a time (hopefully allowing for garbage collection).
author Lucas Thompson <dev@lucas.im>
date Tue, 16 May 2017 10:44:55 +0100
parents 89208e8af8cc
children 72673c954216
comparison
equal deleted inserted replaced
321:89208e8af8cc 322:38886ce7e2e5
1 /** 1 /**
2 * Created by lucas on 01/12/2016. 2 * Created by lucas on 01/12/2016.
3 */ 3 */
4 4
5 import {PiperVampService, ListRequest, ListResponse} from 'piper'; 5 import {PiperVampService, ListRequest, ListResponse, Service} from 'piper';
6 import { 6 import {
7 SimpleRequest 7 SimpleRequest
8 } from 'piper/HigherLevelUtilities'; 8 } from 'piper/HigherLevelUtilities';
9 import { VampExamplePlugins } from 'piper/ext/VampExamplePluginsModule'; 9 import { VampExamplePlugins } from 'piper/ext/VampExamplePluginsModule';
10 import { 10 import {
29 29
30 type LibraryUri = string; 30 type LibraryUri = string;
31 type LibraryKey = string; 31 type LibraryKey = string;
32 32
33 type RequireJs = (libs: string[], callback: (...libs: any[]) => void) => void; 33 type RequireJs = (libs: string[], callback: (...libs: any[]) => void) => void;
34 type Factory<T> = () => T;
34 35
35 class AggregateStreamingService implements StreamingService { 36 class AggregateStreamingService implements StreamingService {
36 private services: Map<LibraryKey, PiperStreamingService>; 37 private services: Map<LibraryKey, Factory<PiperStreamingService>>;
37 38
38 constructor() { 39 constructor() {
39 this.services = new Map<LibraryKey, PiperStreamingService>(); 40 this.services = new Map<LibraryKey, Factory<PiperStreamingService>>();
40 // this.services.set( 41 this.services.set(
41 // 'vamp-example-plugins', 42 'vamp-example-plugins',
42 // new PiperStreamingService(new PiperVampService(VampExamplePlugins())) 43 () => new PiperStreamingService(
43 // ); 44 new PiperVampService(VampExamplePlugins())
45 )
46 );
44 } 47 }
45 48
46 addService(key: LibraryKey, service: PiperStreamingService): void { 49 addService(key: LibraryKey, service: Factory<PiperStreamingService>): void {
47 this.services.set(key, service); 50 this.services.set(key, service);
48 } 51 }
49 52
50 list(request: ListRequest): Promise<ListResponse> { 53 list(request: ListRequest): Promise<ListResponse> {
51 return Promise.all( 54 return Promise.all(
52 [...this.services.values()].map(client => client.list({})) 55 [...this.services.values()].map(client => client().list({}))
53 ).then(allAvailable => ({ 56 ).then(allAvailable => ({
54 available: allAvailable.reduce( 57 available: allAvailable.reduce(
55 (all, current) => all.concat(current.available), 58 (all, current) => all.concat(current.available),
56 [] 59 []
57 ) 60 )
64 } 67 }
65 68
66 protected dispatch(method: 'process', 69 protected dispatch(method: 'process',
67 request: SimpleRequest): Observable<StreamingResponse> { 70 request: SimpleRequest): Observable<StreamingResponse> {
68 const key = request.key.split(':')[0]; 71 const key = request.key.split(':')[0];
69 return this.services.has(key) ? 72 return this.services.has(key) ? this.services.get(key)()[method](request) :
70 this.services.get(key)[method](request) : Observable.throw('Invalid key'); 73 Observable.throw('Invalid key');
71 } 74 }
72 } 75 }
73 76
74 class ThrottledReducingAggregateService extends AggregateStreamingService { 77 class ThrottledReducingAggregateService extends AggregateStreamingService {
75 constructor() { 78 constructor() {
136 switch (ev.data.method) { 139 switch (ev.data.method) {
137 case 'import': 140 case 'import':
138 const key: LibraryKey = ev.data.params; 141 const key: LibraryKey = ev.data.params;
139 if (this.remoteLibraries.has(key)) { 142 if (this.remoteLibraries.has(key)) {
140 this.requireJs([this.remoteLibraries.get(key)], (plugin) => { 143 this.requireJs([this.remoteLibraries.get(key)], (plugin) => {
141 // TODO a factory with more logic probably belongs in piper-js 144
142 const lib: any | EmscriptenModule = plugin.createLibrary(); 145 const service = () => {
143 const isEmscriptenModule = typeof lib.cwrap === 'function'; 146 // TODO a factory with more logic probably belongs in piper-js
144 const service = new PiperStreamingService( 147 const lib: any | EmscriptenModule = plugin.createLibrary();
145 isEmscriptenModule ? new PiperVampService(lib) : lib // TODO 148 const isEmscriptenModule = typeof lib.cwrap === 'function';
146 ); 149 return new PiperStreamingService(
150 isEmscriptenModule ? new PiperVampService(lib) : lib // TODO
151 );
152 };
147 this.service.addService(key, service); 153 this.service.addService(key, service);
148 this.service.list({}).then(sendResponse); 154 this.service.list({}).then(sendResponse);
149 }); 155 });
150 } else { 156 } else {
151 console.error('Non registered library key.'); // TODO handle error 157 console.error('Non registered library key.'); // TODO handle error