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 150 151 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 111x 45x 11x 11x 10x 18x 18x 18x 18x 9x 18x 13x 13x 10x 9x 9x 18x 18x 1x 1x 18x 10x 21x 21x 20x 16x 16x 16x 12x 16x 46x 40x 40x 40x 37x 16x 12x 21x 1x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x | import { Completable, Config } from "@jonloucks/concurrency-ts/api/Completable";
import { Completion, CompletionState } from "@jonloucks/concurrency-ts/api/Completion";
import { getStateMachineConfig as getCompletionStateConfig } from "@jonloucks/concurrency-ts/api/CompletionState";
import { Idempotent } from "@jonloucks/contracts-ts/auxiliary/Idempotent";
import { OnCompletion } from "@jonloucks/concurrency-ts/api/OnCompletion";
import { StateMachine } from "@jonloucks/concurrency-ts/api/StateMachine";
import { Waitable } from "@jonloucks/concurrency-ts/api/Waitable";
import { WaitableNotify } from "@jonloucks/concurrency-ts/api/WaitableNotify";
import { completionCheck, onCompletionCheck } from "@jonloucks/concurrency-ts/auxiliary/Checks";
import { AUTO_CLOSE_FACTORY, AutoClose, AutoCloseFactory, Contracts, isPresent, OptionalType } from "@jonloucks/contracts-ts";
import { AutoCloseMany, Close, inlineAutoClose } from "@jonloucks/contracts-ts/api/AutoClose";
import { contractsCheck } from "@jonloucks/contracts-ts/auxiliary/Checks";
import { AtomicBoolean, createAtomicBoolean, createIdempotent } from "@jonloucks/contracts-ts/auxiliary/Convenience";
import { IllegalStateException } from "@jonloucks/contracts-ts/auxiliary/IllegalStateException";
import { create as createStateMachine } from "./StateMachine.impl";
import { create as createWaitable } from "./Waitable.impl";
/**
* Create a new Completable
*
* @param config the completable configuration
* @return the new Completable
* @param <T> the type of completion value
*/
export function create<T>(config: Config<T>): Completable<T> {
return CompletableImpl.internalCreate(config);
}
// ---- Implementation details below ----
interface Observer<T> extends OnCompletion<T>, Close {
};
class CompletableImpl<T> implements Completable<T> {
open(): AutoClose {
return this.#idempotent.open();
}
notifyState(): WaitableNotify<CompletionState> {
return this.#completionStateMachine;
}
notifyValue(): WaitableNotify<T> {
return this.#waitableValue;
}
getCompletion(): OptionalType<Completion<T>> {
return this.#completion;
}
notify(onCompletion: OnCompletion<T>): AutoClose {
const referentOnCompletion: OnCompletion<T> = onCompletionCheck(onCompletion);
const firstClose: AtomicBoolean = createAtomicBoolean(true);
const isLive: AtomicBoolean = createAtomicBoolean(true);
const removeObserver = (v: Observer<T>): void => {
this.#observers.delete(v);
};
const observer: Observer<T> = {
onCompletion: function (completion: Completion<T>): void {
Eif (isLive.get()) {
referentOnCompletion.onCompletion(completion);
}
},
close: function (): void {
if (firstClose.compareAndSet(true, false)) {
isLive.set(false);
removeObserver(this);
}
}
};
this.#observers.add(observer);
if (isPresent(this.#completion)) {
try {
observer.onCompletion(this.#completion!);
} catch (e) {
removeObserver(observer);
throw e;
}
}
return inlineAutoClose((): void => {
observer.close();
});
}
onCompletion(completion: Completion<T>): void {
const validCompletion: Completion<T> = completionCheck(completion);
this.assertNotRejecting();
if (this.#completionStateMachine.setState("onCompletion", validCompletion.state)) {
this.#completion = validCompletion;
const value = validCompletion.value;
if (this.isCompleted() && isPresent(value)) {
this.#waitableValue.consume(value!);
}
this.notifyObservers(validCompletion);
}
}
isCompleted(): boolean {
return this.#completionStateMachine.isCompleted();
}
private realOpen(): AutoClose {
this.#closeMany.add(this.#waitableValue.open());
this.#closeMany.add(this.#completionStateMachine.open());
return inlineAutoClose((): void => {
this.#closeMany.close();
});
}
private notifyObservers(newValue: Completion<T>): void {
for (const observer of this.#observers) {
observer.onCompletion(newValue);
}
}
private assertNotRejecting(): void {
switch (this.#idempotent.getState()) {
case 'OPENABLE':
case 'DESTROYED':
case 'CLOSED':
throw new IllegalStateException("Completable is rejecting.");
}
}
static internalCreate<T>(config: Config<T>): Completable<T> {
return new CompletableImpl<T>(config);
}
private constructor(config: Config<T>) {
const contracts: Contracts = contractsCheck(config.contracts);
const closeFactory: AutoCloseFactory = contracts.enforce(AUTO_CLOSE_FACTORY);
this.#closeMany = closeFactory.createAutoCloseMany();
this.#waitableValue = createWaitable<T>({ contracts: contracts, initialValue: config.initialValue });
this.#idempotent = createIdempotent({ contracts: contracts, open: () => this.realOpen() });
this.#completionStateMachine = createStateMachine(getCompletionStateConfig());
}
readonly #completionStateMachine: StateMachine<CompletionState>;
readonly #idempotent: Idempotent;
#completion: OptionalType<Completion<T>> = null;
readonly #waitableValue: Waitable<T>;
readonly #closeMany: AutoCloseMany;
readonly #observers: Set<Observer<T>> = new Set<Observer<T>>();
}; |