# All files / src/app/shared/services task-manager.service.ts

100% Statements 35/35
85.71% Branches 6/7
100% Functions 6/6
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    6428x

    28x

    28x
    28x

    28x

    4x
    4x
    4x

    28x

    28x
    298x

    298x
    298x

    295x

    57x
    57x
    57x
    57x
    2x
    2x

    1x

    1x

    2x

    28x
    4x

    28x
    8x

    2x

    2x

    28x
     import { Injectable } from '@angular/core';

    import * as _ from 'lodash';

    import { ExecutingTask } from '../models/executing-task';
    import { FinishedTask } from '../models/finished-task';
    import { Task } from '../models/task';
    import { ServicesModule } from './services.module';
    import { SummaryService } from './summary.service';

    class TaskSubscription {
      name: string;
      metadata: object;
      onTaskFinished: (finishedTask: FinishedTask) => any;

      constructor(name, metadata, onTaskFinished) {
        this.name = name;
        this.metadata = metadata;
        this.onTaskFinished = onTaskFinished;
      }
    }

    @Injectable({
      providedIn: ServicesModule
    })
    export class TaskManagerService {
      subscriptions: Array<TaskSubscription> = [];

      constructor(summaryService: SummaryService) {
        summaryService.subscribe((data: any) => {
          if (!data) {
            return;
          }
          const executingTasks = data.executing_tasks;
          const finishedTasks = data.finished_tasks;
          const newSubscriptions: Array<TaskSubscription> = [];
          for (const subscription of this.subscriptions) {
            const finishedTask = <FinishedTask>this._getTask(subscription, finishedTasks);
            const executingTask = <ExecutingTask>this._getTask(subscription, executingTasks);
            if (finishedTask !== null && executingTask === null) {
              subscription.onTaskFinished(finishedTask);
            }
            if (executingTask !== null) {
              newSubscriptions.push(subscription);
            }
            this.subscriptions = newSubscriptions;
          }
        });
      }

      subscribe(name, metadata, onTaskFinished: (finishedTask: FinishedTask) => any) {
        this.subscriptions.push(new TaskSubscription(name, metadata, onTaskFinished));
      }

      _getTask(subscription: TaskSubscription, tasks: Array<Task>): Task {
        for (const task of tasks) {
          if (task.name === subscription.name && _.isEqual(task.metadata, subscription.metadata)) {
            return task;
          }
        }
        return null;
      }
    }