All files / src/classes CoreTriggerService.ts

83.33% Statements 5/6
100% Branches 0/0
75% Functions 3/4
83.33% Lines 5/6

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                                                                                                        1x 1x 1x 2x                     1x      
import CoreService from './CoreService';
import type {
  ICoreCache,
  ICoreClient,
  ICoreKernel,
  ICoreKernelModule,
  ICorePresenter,
  IDataBase,
} from '../lib';
 
/**
 * Abstract service that starts automatically when a specified trigger event is emitted by the kernel.
 *
 * The service inherits from {@link CoreService} and listens for the kernel event identified by
 * {@link CoreTriggerService.triggerName}. When the event fires, the service's {@link CoreService.start}
 * method is invoked, ensuring that the service is activated in response to external signals.
 *
 * @template K extends ICoreKernel<any> = ICoreKernel<any>
 * @template T extends IDataBase<any, any> | null = any
 * @template P extends ICoreClient | null = any
 * @template C extends ICoreCache | null = any
 * @template E extends ICorePresenter<any> | null = any
 * @extends CoreService<K, T, P, C, E>
 */
export default abstract class CoreTriggerService<
  K extends ICoreKernel<any> = ICoreKernel<any>,
  T extends IDataBase<any, any> | null = any,
  P extends ICoreClient | null = any,
  C extends ICoreCache | null = any,
  E extends ICorePresenter<any> | null = any,
  D = any,
> extends CoreService<K, T, P, C, E> {
  triggerName: string;
 
  /**
   * Creates an instance that registers a listener on the kernel for a specified trigger.
   *
   * When the kernel emits the event named {@link triggerName}, the listener will
   * invoke {@link start} on this instance. The {@link skipAutoStart} flag determines
   * whether the instance should start automatically during construction or not.
   *
   * @param {string} name The name of the instance.
   * @param {string} triggerName The kernel event name that will trigger a start.
   * @param {ICoreKernelModule<K, T, P, C, E>} module The module associated with this instance.
   * @param {boolean} [skipAutoStart] If true, the instance will not start automatically.
   * @return {void}
   */
  constructor(
    name: string,
    triggerName: string,
    module: ICoreKernelModule<K, T, P, C, E>,
  ) {
    super(name, module, true);
    this.triggerName = triggerName;
    this.getKernel().on(triggerName, async (_, payload) => {
      return this.onTrigger(payload);
    });
  }
 
  abstract onTrigger(payload?: D): Promise<boolean>;
 
  async start() {
    return true;
  }
 
  async stop() {
    return true;
  }
}