All files / src/impl SingletonPromisor.impl.ts

100% Statements 14/14
100% Branches 2/2
100% Functions 6/6
100% Lines 14/14

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        25x   25x 25x                 25x 9x                       17x 9x   17x       9x       9x       9x       9x       9x 9x      
import { Promisor } from "@jonloucks/contracts-ts/api/Promisor";
import { OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
import { AtomicBoolean } from "@jonloucks/contracts-ts/auxiliary/AtomicBoolean";
import { AtomicReference } from "@jonloucks/contracts-ts/auxiliary/AtomicReference";
import { promisorCheck } from "@jonloucks/contracts-ts/auxiliary/Checks";
 
import { create as createAtomicBoolean } from "./AtomicBoolean.impl";
import { create as createAtomicReference } from "./AtomicReference.impl";
 
/**
 * Factory method to create an Singleton promisor implementation
 * 
 * @param referent the source promisor
 * @param <T> the type of deliverable
 * @returns the new Singleton Promisor implementation
 */
export function create<T>(referent: Promisor<T>): RequiredType<Promisor<T>> {
  return SingletonPromisorImpl.internalCreate<T>(referent);
}
 
// ---- Implementation details below ----
 
/**
 * Implementation of a Singleton Promisor
 * @param <T> The type of deliverable
 */
class SingletonPromisorImpl<T> implements Promisor<T> {
 
  demand(): OptionalType<T> {
    if (this.firstTime.compareAndSet(true, false)) {
      this.singletonRef.set(this.referent.demand());
    }
    return this.singletonRef.get();
  }
 
  incrementUsage(): number {
    return this.referent.incrementUsage();
  }
 
  decrementUsage(): number {
    return this.referent.decrementUsage();
  }
 
  static internalCreate<T>(referent: Promisor<T>): RequiredType<Promisor<T>> {
    return new SingletonPromisorImpl<T>(referent);
  }
 
  private constructor(referent: Promisor<T>) {
    this.referent = promisorCheck(referent);
  }
 
  private readonly referent: Promisor<T>;
  private readonly singletonRef: AtomicReference<OptionalType<T>> = createAtomicReference<OptionalType<T>>();
  private readonly firstTime: AtomicBoolean = createAtomicBoolean(true);
}