Skip to content

Instantly share code, notes, and snippets.

@agjs
Created February 22, 2023 20:55
Show Gist options
  • Save agjs/acc17c2daacd12eb12bbcb1740270812 to your computer and use it in GitHub Desktop.
Save agjs/acc17c2daacd12eb12bbcb1740270812 to your computer and use it in GitHub Desktop.
event emitter
import { EventEmitter } from "events";
class NotificationSystem extends EventEmitter {
constructor() {
this.#init()
}
#init() {
this.on("follow", async (targetId, metadata) => {
const notification = await Models.notification.create({
type: "follow",
targetId,
metadata: {
actorId: metadata.actorId,
},
});
console.log(notification);
});
}
notify(type, targetId, metadata) {
if (!Models)
throw new Error(
"setupNotifications must be called before notifications can be used"
);
if (!targetId) throw new Error("Target is required");
if (type === "follow") {
console.log("Notification is follow");
notificationEmitter.emit("follow", targetId, metadata);
}
}
}
const notifications = new NotificationSystem();
export const notify = notifications.notify;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment