1
0

BaseModule.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import ModuleManager from "./ModuleManager";
  2. import { ModuleStatus } from "./types/Modules";
  3. // type ModuleName = keyof Modules;
  4. export default abstract class BaseModule {
  5. protected moduleManager: ModuleManager;
  6. protected name: string;
  7. protected status: ModuleStatus;
  8. /**
  9. * Base Module
  10. *
  11. * @param {ModuleManager} moduleManager Module manager class
  12. * @param {string} name Module name
  13. */
  14. public constructor(moduleManager: ModuleManager, name: string) {
  15. this.moduleManager = moduleManager;
  16. this.name = name;
  17. this.status = "LOADED";
  18. // console.log(`Module (${this.name}) starting`);
  19. }
  20. /**
  21. * getName - Get module name
  22. *
  23. * @returns {string} name
  24. */
  25. public getName(): string {
  26. return this.name;
  27. }
  28. /**
  29. * getStatus - Get module status
  30. *
  31. * @returns {ModuleStatus} status
  32. */
  33. public getStatus(): ModuleStatus {
  34. return this.status;
  35. }
  36. /**
  37. * setStatus - Set module status
  38. *
  39. * @param {ModuleStatus} status Module status
  40. */
  41. public setStatus(status: ModuleStatus): void {
  42. this.status = status;
  43. }
  44. /**
  45. * startup - Startup module
  46. */
  47. public startup(): Promise<void> {
  48. return new Promise(resolve => {
  49. console.log(`Module (${this.name}) starting`);
  50. this.setStatus("STARTING");
  51. resolve();
  52. });
  53. }
  54. /**
  55. * started - called with the module has started
  56. */
  57. protected started(): void {
  58. console.log(`Module (${this.name}) started`);
  59. this.setStatus("STARTED");
  60. }
  61. /**
  62. * shutdown - Shutdown module
  63. */
  64. public shutdown(): Promise<void> {
  65. return new Promise(resolve => {
  66. console.log(`Module (${this.name}) stopping`);
  67. this.setStatus("STOPPING");
  68. this.stopped();
  69. resolve();
  70. });
  71. }
  72. /**
  73. * stopped - called when the module has stopped
  74. */
  75. protected stopped(): void {
  76. console.log(`Module (${this.name}) stopped`);
  77. this.setStatus("STOPPED");
  78. }
  79. }