All files / src/api WaitableNotify.ts

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

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  3x                                                               3x 40x  
import { AutoClose, RequiredType } from "@jonloucks/contracts-ts";
import { guardFunctions } from "@jonloucks/concurrency-ts/api/Types";
import { Type as ConsumerType } from "@jonloucks/concurrency-ts/auxiliary/Consumer";
import { Type as PredicateType } from "@jonloucks/concurrency-ts/auxiliary/Predicate";
 
export { AutoClose, ConsumerType, PredicateType, RequiredType };
 
/**
 * Notify lister when condition is satisfied
 * @param <T> the type of value
 */
export interface WaitableNotify<T> {
 
  /**
   * When condition is satisfied the listener is invoked
   * Note: It is likely the listener will be called within a write lock context.
   * Deadlocks could happen of listener is waiting on another thread to acquire a lock to this WaitableNotify
   *
   * @param predicate the predicate to test if the value should be passed to listener
   * @param listener the listener
   * @return AutoClose which removes the listener
   * @throws IllegalArgumentException if predicate is null or the listener is null
 */
  notifyWhile(predicate: RequiredType<PredicateType<T>>, listener: RequiredType<ConsumerType<T>>): RequiredType<AutoClose>;
}
 
/**
 * Duck type guard check for WaitableNotify
 * 
 * @param value  the value to check
 * @param <T>    the type of value  
 * @returns true if value is WaitableNotify, false otherwise
 */
export function guard<T>(value: unknown): value is RequiredType<WaitableNotify<T>> {
  return guardFunctions(value, 'notifyWhile');
}