StationModule.ts 1.5 KB

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