changeset 323:72673c954216

Try chaining the list requests.
author Lucas Thompson <dev@lucas.im>
date Tue, 16 May 2017 11:15:43 +0100
parents 38886ce7e2e5
children e433a2da0ada
files src/app/services/feature-extraction/FeatureExtractionWorker.ts
diffstat 1 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/services/feature-extraction/FeatureExtractionWorker.ts	Tue May 16 10:44:55 2017 +0100
+++ b/src/app/services/feature-extraction/FeatureExtractionWorker.ts	Tue May 16 11:15:43 2017 +0100
@@ -51,15 +51,25 @@
   }
 
   list(request: ListRequest): Promise<ListResponse> {
-    return Promise.all(
-      [...this.services.values()].map(client => client().list({}))
-    ).then(allAvailable => ({
-        available: allAvailable.reduce(
-          (all, current) => all.concat(current.available),
-          []
-        )
+    //TODO refactor
+    const listThunks: (() => Promise<ListResponse>)[] = [
+      ...this.services.values()
+    ].map(client => () => client().list({}));
+
+    const concatAvailable = (running: ListResponse,
+                             nextResponse: Promise<ListResponse>)
+      : Promise<ListResponse> => {
+      return nextResponse.then(response => {
+        running.available = running.available.concat(response.available);
+        return running;
+      });
+    };
+
+    return listThunks.reduce((runningResponses, nextResponse) => {
+      return runningResponses.then(response => {
+        return concatAvailable(response, nextResponse());
       })
-    );
+    }, Promise.resolve({available: []}));
   }
 
   process(request: SimpleRequest): Observable<StreamingResponse> {
@@ -130,7 +140,7 @@
 
     this.workerScope.onmessage = (ev: MessageEvent) => {
       const sendResponse = (result) => {
-        console.warn(ev.data.method);
+        console.warn(ev.data.method, ev.data);
         this.workerScope.postMessage({
           method: ev.data.method,
           result: result