All files / src/classes CoreBridge.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 5/5
100% Lines 12/12

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                                                  1x 1x 1x 1x       1x 1x 1x       2x       2x 2x   2x       2x      
import { BridgeState, type ICoreBridge, type ICoreKernelModule } from '../lib';
import CoreLogChannel from './CoreLogChannel';
import { XUtil } from '../utils';
 
/**
 * Manages a bidirectional bridge between two kernel modules.
 *
 * The bridge exposes a simple lifecycle: it can be connected to its source
 * and target modules, its state can be updated, and callers can await a
 * specific state transition.
 *
 * @extends CoreLogChannel
 * @implements ICoreBridge
 */
export default class CoreBridge extends CoreLogChannel implements ICoreBridge {
  private state: BridgeState;
 
  private src: ICoreKernelModule<any, any, any, any, any>;
 
  private target: ICoreKernelModule<any, any, any, any, any>;
 
  constructor(
    src: ICoreKernelModule<any, any, any, any, any>,
    target: ICoreKernelModule<any, any, any, any, any>,
  ) {
    super(`BR:${src.getName()} => ${target.getName()}`, src.getKernel());
    this.src = src;
    this.target = target;
    this.state = BridgeState.init;
  }
 
  connect(): void {
    this.src.addSrcBridge(this);
    this.target.addTarBridge(this);
    this.debug('connected');
  }
 
  setState(state: BridgeState): void {
    this.state = state;
  }
 
  async waitForState(state: BridgeState): Promise<boolean> {
    while (this.state !== state) {
      await XUtil.sleep(1000);
    }
    return true;
  }
 
  getTarget(): ICoreKernelModule<any, any, any, any, any> {
    return this.target;
  }
}