StationsModule.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import JobContext from "@/JobContext";
  2. import { UniqueMethods } from "@/types/Modules";
  3. import BaseModule from "@/BaseModule";
  4. export default class StationsModule extends BaseModule {
  5. /**
  6. * Station Module
  7. */
  8. public constructor() {
  9. super("stations");
  10. this._dependentModules = ["data"];
  11. }
  12. /**
  13. * startup - Startup station module
  14. */
  15. public override async startup() {
  16. await super.startup();
  17. this.log("Station Startup");
  18. await super._started();
  19. }
  20. /**
  21. * shutdown - Shutdown station module
  22. */
  23. public override async shutdown() {
  24. await super.shutdown();
  25. await super._stopped();
  26. }
  27. /**
  28. * addToQueue - Add media to queue
  29. *
  30. * @param payload - Payload
  31. */
  32. public async addToQueue(context: JobContext, payload: { songId: string }) {
  33. const { songId } = payload;
  34. context.log(`Adding song ${songId} to the queue.`);
  35. await new Promise((resolve, reject) => {
  36. setTimeout(() => {
  37. if (Math.round(Math.random())) reject(new Error("Test321"));
  38. else resolve(true);
  39. }, Math.random() * 1000);
  40. });
  41. }
  42. public async addA(context: JobContext) {
  43. context.log("ADDA");
  44. await context.jobQueue.runJob("stations", "addB", {}, { priority: 5 });
  45. return { number: 123 };
  46. }
  47. public async addB(context: JobContext) {
  48. context.log("ADDB");
  49. await context.jobQueue.runJob("stations", "addToQueue", {
  50. songId: "test"
  51. });
  52. await context.jobQueue.runJob("stations", "addC", {});
  53. }
  54. public async addC(context: JobContext) {
  55. context.log("ADDC");
  56. // await new Promise(() => {});
  57. }
  58. }
  59. export type StationsModuleJobs = {
  60. [Property in keyof UniqueMethods<StationsModule>]: {
  61. payload: Parameters<UniqueMethods<StationsModule>[Property]>[1];
  62. returns: Awaited<ReturnType<UniqueMethods<StationsModule>[Property]>>;
  63. };
  64. };