All files / src/api Concurrency.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 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        12x     12x                                                                                                                                                                                                           12x 68x                         12x      
import { Completable, Config as CompletableConfig } from "@jonloucks/concurrency-ts/api/Completable";
import { Completion } from "@jonloucks/concurrency-ts/api/Completion";
import { OnCompletion } from "@jonloucks/concurrency-ts/api/OnCompletion";
import { StateMachine, Config as StateMachineConfig } from "@jonloucks/concurrency-ts/api/StateMachine";
import { Consumer, ConsumerType, guardFunctions, Supplier, SupplierType } from "@jonloucks/concurrency-ts/api/Types";
import { Waitable, Config as WaitableConfig } from "@jonloucks/concurrency-ts/api/Waitable";
import { Open } from "@jonloucks/contracts-ts/api/Open";
import { Contract, Contracts, createContract, OptionalType, RequiredType } from "@jonloucks/contracts-ts";
 
export {
  Completable, 
  CompletableConfig,
  Completion,
  StateMachine,
  StateMachineConfig,
  Waitable,
  WaitableConfig,
  OnCompletion,
  Consumer,
  ConsumerType,
  Supplier,
  SupplierType,
  Contracts
}
 
/**
 * The configuration used to create a new Concurrency instance.
 */
export interface Config {
  /**
   * @return the contracts, some use case have their own Contracts instance.
   */
  contracts?: Contracts;
 
    /**
   * an optional list of events that when fired will shutdown Contracts
   * the default is no events.
   * It can be a custom event or system events like 'beforeExit', 'SIGINT', 'SIGTERM'
   * @return list of shutdown event names
   */
  shutdownEvents?: string[];
}
 
export { Config as ConcurrencyConfig };
 
/**
 * The Concurrency API
 */
export interface Concurrency extends Open {
 
  /**
   * Create a new Waitable with the given configuration
   *
   * @param config the waitable configuration
   * @return the waitable
   * @param <T> the type of waitable
   */
  createWaitable<T>(config: WaitableConfig<T>): RequiredType<Waitable<T>>;
 
  /**
   * Create a new StateMachine
   *
   * @param config the state machine configuration
   * @return the new StateMachine
   * @param <T> the type of each state
   * @throws IllegalArgumentException if initialState is null
   */
  createStateMachine<T>(config: StateMachineConfig<T>): RequiredType<StateMachine<T>>;
 
  /**
   * Create a new Completable
   *
   * @param config the completable configuration
   * @return the new Completable
   * @param <T> the type of completion value
   */
  createCompletable<T>(config: CompletableConfig<T>): RequiredType<Completable<T>>;
 
  /**
   * Guaranteed execution: complete later block.
   * Either the delegate successfully takes ownership of the OnCompletion or
   * a final FAILED completion is dispatched
   *
   * @param onCompletion the OnCompletion callback
   * @param delegate the intended delegate to receive the OnCompletion
   * @param <T> the completion value type
   */
  completeLater<T>(onCompletion: RequiredType<OnCompletion<T>>, delegate: RequiredType<ConsumerType<OnCompletion<T>>>): void;
 
  /**
   * Guaranteed execution: complete now block
   * When this method finishes, it is guaranteed the OnCompletion will have received a final completion.
   * Exceptions will result in a FAILED completion
   * Exceptions will be rethrown.
   *
   * @param onCompletion the OnCompletion callback
   * @param successBlock executed to determine the final completion value for an activity
   * @return the final completion value
   * @param <T> the completion value type
   */
  completeNow<T>(onCompletion: RequiredType<OnCompletion<T>>, successBlock: RequiredType<SupplierType<T>>): OptionalType<T>;
}
 
/**
 * Determine if the given instance is a Concurrency
 *
 * @param instance the instance to test
 * @return true if the instance is a Concurrency
 */
export function guard(instance: unknown): instance is RequiredType<Concurrency> {
  return guardFunctions(instance,
    'createWaitable',
    'createStateMachine',
    'createCompletable',
    'completeLater',
    'completeNow',
    'open'
  );
}
 
/**
 * The Concurrency contract
 */
export const CONTRACT: Contract<Concurrency> = createContract<Concurrency>({
  name: "Concurrency",
  test: guard
});