@@ -288,13 +288,22 @@ export default class TrackingService extends Service {
288
288
* @param {Number } customer The customer id to filter by
289
289
* @public
290
290
*/
291
- @dropTask
291
+ @task
292
292
* projects ( customer ) {
293
293
if ( ! customer ) {
294
294
// We can't test this because the UI prevents it
295
295
throw new Error ( "No customer selected" ) ;
296
296
}
297
297
298
+ const original = this . filterDuplicateTasks ( this . projects , customer ) ;
299
+ if ( original ) {
300
+ return yield original ;
301
+ }
302
+
303
+ // Give it 100ms to "collect" similar requests and
304
+ // increases the efficiency even more.
305
+ yield timeout ( 100 ) ;
306
+
298
307
return yield this . store . query ( "project" , { customer } ) ;
299
308
}
300
309
@@ -305,13 +314,38 @@ export default class TrackingService extends Service {
305
314
* @param {Number } project The project id to filter by
306
315
* @public
307
316
*/
308
- @dropTask
317
+ @task
309
318
* tasks ( project ) {
310
319
if ( ! project ) {
311
320
// We can't test this because the UI prevents it
312
321
throw new Error ( "No project selected" ) ;
313
322
}
314
323
324
+ const original = this . filterDuplicateTasks ( this . tasks , project ) ;
325
+ if ( original ) {
326
+ return yield original ;
327
+ }
328
+
329
+ // Give it 100ms to "collect" similar requests and
330
+ // increases the efficiency even more.
331
+ yield timeout ( 100 ) ;
332
+
315
333
return yield this . store . query ( "task" , { project } ) ;
316
334
}
335
+
336
+ /**
337
+ * Filters running tasks with the same arguments and returns the first
338
+ * task with matching arguments if there is more than one concurrent
339
+ * instance of it. Otherwise returns undefined.
340
+ */
341
+ filterDuplicateTasks ( task , argument ) {
342
+ const taskInstances = task . scheduler . taskInstances ;
343
+ if (
344
+ taskInstances . length > 1 &&
345
+ taskInstances . filter ( ( task ) => task . args [ 0 ] === argument ) . length > 1
346
+ ) {
347
+ /* istanbul ignore next */
348
+ return taskInstances . find ( ( task ) => task . args [ 0 ] === argument ) ;
349
+ }
350
+ }
317
351
}
0 commit comments