Unsubscribe.ts 815 B

123456789101112131415161718192021222324252627282930
  1. import Job from "@/Job";
  2. import EventsModule from "@/modules/EventsModule";
  3. import { JobOptions } from "@/types/JobOptions";
  4. export default class Unsubscribe extends Job {
  5. public constructor(
  6. payload?: any,
  7. options?: Omit<JobOptions, "runDirectly">
  8. ) {
  9. super(EventsModule, payload, options);
  10. }
  11. protected override async _validate() {
  12. if (typeof this._payload !== "object")
  13. throw new Error("Payload must be an object");
  14. if (typeof this._payload.channel !== "string")
  15. throw new Error("Channel must be a string");
  16. }
  17. protected override async _authorize() {}
  18. protected async _execute({ channel }: { channel: string }) {
  19. const socketId = this._context.getSocketId();
  20. if (!socketId) throw new Error("No socketId specified");
  21. await EventsModule.unsubscribeSocket(channel, socketId);
  22. }
  23. }