Created
February 22, 2023 20:55
-
-
Save agjs/acc17c2daacd12eb12bbcb1740270812 to your computer and use it in GitHub Desktop.
event emitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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