All files / src/api PromisorFactory.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 1/1
100% Lines 5/5

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 64 65 66 67 68 69 70 71 72 73    26x 26x                                                                                                               26x 49x           26x            
import { Contract } from "@jonloucks/contracts-ts/api/Contract";
import { Promisor, PromisorType } from "@jonloucks/contracts-ts/api/Promisor";
import { create as createContract } from "@jonloucks/contracts-ts/api/RatifiedContract";
import { OptionalType, RequiredType, TransformType, guardFunctions } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Helper methods for creating and chaining Promisors used for Contracts.bind().
 */
export interface PromisorFactory {
 
  /**
   * Creates a Promisor that returns the given value every time it is claimed.
   *
   * @param deliverable the value to
   * @return The new Promisor
   * @param <T> the type of deliverable
   */
  createValue<T>(deliverable: OptionalType<T>): RequiredType<Promisor<T>>;
 
  /**
   * Creates a Promisor that only calls the source Promisor once and then always
   * returns that value.
   * Note: increment and decrementUsage are relayed to the source promisor.
   *
   * @param promisor the source Promisor
   * @return The new Promisor
   * @param <T> the type of deliverable
   */
  createSingleton<T>(promisor: PromisorType<T>): RequiredType<Promisor<T>>;
 
  /**
   * Reference counted, lazy loaded, with opt-in 'open' and 'close' invoked on deliverable.
   * Note: increment and decrementUsage are relayed to the source promisor.
   *
   * @param promisor the source promisor
   * @return the new Promisor
   * @param <T> the type of deliverable
   */
  createLifeCycle<T>(promisor: PromisorType<T>): RequiredType<Promisor<T>>;
 
  /**
   * Extract values from the deliverable of a source Promisor.
   * Note: increment and decrementUsage are relayed to the source promisor.
   *
   * @param promisor the source promisor
   * @param extractor the function that gets an object from the deliverable. For example Person  => Age
   * @return the new Promisor
   * @param <T> the type of deliverable
   * @param <R> the new Promisor deliverable type
   */
  createExtractor<T, R>(promisor: PromisorType<T>, extractor: TransformType<T, R>): RequiredType<Promisor<R>>;
}
 
/**
 * Type guard for PromisorFactory
 * 
 * @param value the value to check
 * @return true if value is PromisorFactory, false otherwise
 */
export function guard(instance: unknown): instance is RequiredType<PromisorFactory> {
  return guardFunctions(instance, 'createExtractor', 'createLifeCycle', 'createSingleton', 'createValue');
}
 
/**
 * The Contract for PromisorFactory implementation.
 */
export const CONTRACT: Contract<PromisorFactory> = createContract({
  test: guard,
  name: "PromisorFactory",
  typeName: "PromisorFactory"
});