All files / src/auxiliary AtomicBoolean.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x                                                                                               1x 20x        
import { RequiredType, guardFunctions } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Responsibility: An atomic boolean interface for thread-safe boolean operations.
 */
export interface AtomicBoolean {
 
    /**
     * Get the current boolean value.
     */
    get(): boolean;
 
    /**
     * Set the current boolean value.
     * @param newValue the new value
     */
    set(newValue: boolean): void;
 
    /**
     * Atomically set the value to the given updated value if the current value === the expected value.
     * 
     * @param expectedValue the expected current value
     * @param newValue the requested new value 
     * @return true if successful. False return indicates that the actual value was not equal to the expected value.
     */
    compareAndSet(expectedValue: boolean, newValue: boolean): boolean;
 
    /**     
     * Atomically sets to the given value and returns the previous value.
     * 
     * @param newValue the new value
     * @return the previous value
     */
    getAndSet(newValue: boolean): boolean;
 
    /**
     * Helpful method for converting to primitive types.
     * @param hint 
     */
    [Symbol.toPrimitive](hint: string): string | boolean | number;
}
 
/**
 * Type guard for AtomicBoolean interface.
 * 
 * @param instance the instance to check
 * @returns true if the instance implements AtomicBoolean
 */
export function guard(instance: unknown): instance is RequiredType<AtomicBoolean> {
    return guardFunctions(instance, "compareAndSet", "get", "set", "getAndSet");
}