import { AnyConstructor, AnyFunction, Mixin } from "../class/Mixin.js" //--------------------------------------------------------------------------------------------------------------------- export interface GenericCalculation { context : unknown calculation : AnyFunction isCalculationStarted () : boolean isCalculationCompleted () : boolean startCalculation (...args : unknown[]) : IteratorResult continueCalculation (value : unknown) : IteratorResult cleanupCalculation () readonly result : unknown } //--------------------------------------------------------------------------------------------------------------------- export class CalculationGen extends Mixin( [], (base : AnyConstructor) => class CalculationGen extends base implements GenericCalculation { context : unknown calculation : AnyFunction iterator : Generator = undefined iterationResult : IteratorResult = undefined isCalculationStarted () : boolean { return Boolean(this.iterator || this.iterationResult) } isCalculationCompleted () : boolean { return Boolean(this.iterationResult && this.iterationResult.done) } get result () : unknown { return this.iterationResult && this.iterationResult.done ? this.iterationResult.value : undefined } startCalculation (...args : unknown[]) : IteratorResult { const iterator : this[ 'iterator' ] = this.iterator = this.calculation.call(this.context || this, ...args) return this.iterationResult = iterator.next() } continueCalculation (value : unknown) : IteratorResult { // MAYBE: cleanup the `this.iterator` right away if `done` // still need to cleanup, since there might be unfinished calculations return this.iterationResult = this.iterator.next(value) } cleanupCalculation () { this.iterationResult = undefined this.iterator = undefined } } ){} //--------------------------------------------------------------------------------------------------------------------- export const SyncCalculationStarted = Symbol('SyncCalculationStarted') const fixedIterationResult : IteratorResult = { done : true, value : undefined } export class CalculationSync extends Mixin( [], (base : AnyConstructor) => class CalculationSync extends base implements GenericCalculation { context : unknown calculation : AnyFunction calculationResult : unknown = undefined isCalculationStarted () : boolean { return this.calculationResult !== undefined } isCalculationCompleted () : boolean { return this.calculationResult !== undefined && this.calculationResult !== SyncCalculationStarted } get result () : unknown { return this.calculationResult !== undefined && this.calculationResult !== SyncCalculationStarted ? this.calculationResult : undefined } startCalculation (...args : unknown[]) : IteratorResult { // this assignment allows other code to observe, that calculation has started this.calculationResult = SyncCalculationStarted this.calculationResult = fixedIterationResult.value = this.calculation.call(this.context || this, ...args) return fixedIterationResult } continueCalculation (value : unknown) : IteratorResult { throw new Error("Can not continue synchronous calculation") } cleanupCalculation () { this.calculationResult = undefined } } ){}