All files / modules/70-pipeline/factories/PubSubPipelineAction PubSubAction.ts

60% Statements 9/15
50% Branches 2/4
50% Functions 3/6
60% Lines 9/15

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              66x 66x     8x 8x       8x         30x 30x 30x         30x                  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
export class PubSubAction<TTypes, T, TResult> {
  private subMap = new Map<TTypes, Array<(arg: T) => Promise<TResult>>>()
 
  subscribe(type: TTypes, cb: (arg: T) => Promise<TResult>): void {
    const item = this.subMap.get(type)
    Iif (item) {
      item.push(cb)
      this.subMap.set(type, item)
    } else {
      this.subMap.set(type, [cb])
    }
  }
 
  publish(type: TTypes, arg: T): Promise<TResult[]> {
    const item = this.subMap.get(type)
    const promiseAr: Array<Promise<TResult>> = []
    Iif (item) {
      item.forEach(cb => {
        promiseAr.push(cb(arg))
      })
    }
    return Promise.all(promiseAr)
  }
  unsubscribe(type: TTypes): void {
    this.subMap.delete(type)
  }
  unsubscribeAll(): void {
    this.subMap.clear()
  }
}