All files / src/api Convenience.ts

100% Statements 32/32
100% Branches 0/0
100% Functions 18/18
100% Lines 32/32

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1502x   2x   2x 2x   2x             3x 8x 4x 3x 3x 3x 4x 8x 3x                                                             2x 2x                 2x 12x                     2x 1x                 2x 5x                   2x 1x                       2x 2x                     2x 1x                         2x 1x                 2x 1x      
import { Contract, ContractConfig, CONTRACTS, createContract } from "@jonloucks/contracts-ts";
import { Promisor, PromisorType } from "@jonloucks/contracts-ts/api/Promisor";
import { AutoClose, inlineAutoClose } from "@jonloucks/contracts-ts/api/AutoClose";
import { BindStrategyType } from "@jonloucks/contracts-ts/api/BindStrategy";
import { CONTRACT as PROMISOR_FACTORY } from "@jonloucks/contracts-ts/api/PromisorFactory";
import { CONTRACT as REPOSITORY_FACTORY } from "@jonloucks/contracts-ts/api/RepositoryFactory";
import { Repository, Config as RepositoryConfig } from "@jonloucks/contracts-ts/api/Repository";
import { OptionalType, RequiredType, Transform, TransformType, isNumber, isFunction, isString, isBoolean, isObject, guardFunctions } from "@jonloucks/contracts-ts/api/Types";
 
export {
  AutoClose,
  BindStrategyType,
  Contract,
  ContractConfig,
  CONTRACTS,
  createContract,
  guardFunctions,
  inlineAutoClose,
  isBoolean,
  isFunction,
  isNumber,
  isObject,
  isString,
  OptionalType,
  Promisor,
  PromisorType,
  Repository,
  RepositoryConfig,
  RequiredType,
  Transform,
  TransformType,
};
 
/**
 * @module Convenience
 * @description
 * 
 * This module provides convenience functions for creating  types
 * using the shared global CONTRACTS instance. For performance-sensitive
 * applications, consider using factory instances directly to avoid the
 * overhead of enforcing the factory contract on each creation. 
 * 
 * Internal Note: To avoid circular dependencies, other modules should not
 * import from this module. Instead, they should import directly from the
 * source modules of the types. 
 */
 
/**
 * Claim a deliverable from a Contract via the shared global CONTRACTS instance.
 * 
 * @param contract the Contract to claim from
 * @returns the deliverable
 */
export function claim<T>(contract: RequiredType<Contract<T>>): OptionalType<T> {
  return CONTRACTS.claim(contract);
}
 
/**
 * Enforce a deliverable from a Contract via the shared global CONTRACTS instance.
 * 
 * @param contract the Contract to enforce from
 * @returns the deliverable
 */
export function enforce<T>(contract: RequiredType<Contract<T>>): RequiredType<T> {
  return CONTRACTS.enforce(contract);
}
 
/**
 * Bind a Promisor to a Contract via the shared global CONTRACTS instance.
 * 
 * @param contract the Contract to bind to
 * @param promisor the PromisorType to bind
 * @param bindStrategy optional BindStrategyType
 * @returns an AutoClose to manage the binding lifecycle
 */
export function bind<T>(contract: RequiredType<Contract<T>>, promisor: PromisorType<T>, bindStrategy?: BindStrategyType): RequiredType<AutoClose> {
  return CONTRACTS.bind(contract, promisor, bindStrategy);
}
 
/**
 * Check if a Contract is bound via the shared global CONTRACTS instance.
 * 
 * @param contract the Contract to check
 * @returns true if the Contract is bound, false otherwise
 */
export function isBound<T>(contract: RequiredType<Contract<T>>): boolean {
  return CONTRACTS.isBound(contract);
}
 
/**
 * Creates a Promisor that returns the given value every time it is claimed.
 *
 * @param deliverable the value to return from the Promisor
 * @return The new Promisor
 * @param <T> the type of deliverable
 */
export function createValue<T>(deliverable: OptionalType<T>): RequiredType<Promisor<T>> {
  return enforce(PROMISOR_FACTORY).createValue(deliverable);
}
 
/**
 * 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
 */
export function createSingleton<T>(promisor: PromisorType<T>): RequiredType<Promisor<T>> {
  return enforce(PROMISOR_FACTORY).createSingleton(promisor);
}
 
/**
 * 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
 */
export function createLifeCycle<T>(promisor: PromisorType<T>): RequiredType<Promisor<T>> {
  return enforce(PROMISOR_FACTORY).createLifeCycle(promisor);
}
 
/**
 * 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
 */
export function createExtractor<T, R>(promisor: PromisorType<T>, extractor: TransformType<T, R>): RequiredType<Promisor<R>> {
  return enforce(PROMISOR_FACTORY).createExtractor(promisor, extractor);
}
 
/**
 * Factory method to create a Repository via the shared global REPOSITORY_FACTORY instance.
 * 
 * @param config optional configuration for the Repository
 * @returns the Repository implementation
 */
export function createRepository(config?: RepositoryConfig): RequiredType<Repository> {
  return enforce(REPOSITORY_FACTORY).createRepository(config);
}