Create a simple event shorthand
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Bad 2020-10-26 23:16:47 +01:00
parent d983385e16
commit e08b895694
3 changed files with 20 additions and 39 deletions

View file

@ -50,6 +50,17 @@ class MatrixEvent extends ElemJS {
}
}
function simpleEvent(filter, render) {
return class extends MatrixEvent {
render() {
super.render()
return this.text(render(this.data))
}
static canRender(event) {
return filter(event)
}
}
}
module.exports = {MatrixEvent}
module.exports = {MatrixEvent, simpleEvent}

View file

@ -1,31 +1,12 @@
const {MatrixEvent} = require("./event")
const {simpleEvent} = require("./event")
const UnknownMembership = simpleEvent((e) => e.type == "m.room.member", (e) => "unknown membership event")
function createMembershipEvent(membership, message) {
return class extends MatrixEvent {
render() {
super.render()
return this.text(message(this.data))
}
static canRender(event) {
return event.type == "m.room.member" && event.content.membership == membership
}
}
}
class UnknownMembership extends MatrixEvent {
render() {
super.render()
return this.text("Unsupported membership event")
}
static canRender(event) {
return event.type == "m.room.member"
}
return simpleEvent((e) => e.type == "m.room.member" && e.content.membership === membership, message)
}
const JoinedEvent = createMembershipEvent("join", () => "joined the room")
const JoinedEvent = createMembershipEvent("join", (e) => {console.log(e); return "joined the room"})
const InvitedEvent = createMembershipEvent("invite", (e) => `invited ${e.content.displayname} the room`)
const LeaveEvent = createMembershipEvent("leave", () => "left the room")

View file

@ -1,15 +1,4 @@
const {MatrixEvent} = require("./event")
class UnknownEvent extends MatrixEvent {
render() {
super.render()
this.text("Cannot render event")
}
static canRender(_event) {
return true
}
}
const {simpleEvent} = require("./event")
const UnknownEvent = simpleEvent(() => true, () => "Cannot render event")
console.log(UnknownEvent)
module.exports = [UnknownEvent]