Created
May 17, 2021 14:29
-
-
Save jherr/23101f2cd7980a7839a76b6bdff45583 to your computer and use it in GitHub Desktop.
Code for No BT TS - Challenge 3
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
class EventProcessor { | |
handleEvent(eventName: ..., data: ...): void { | |
} | |
addHandler(handler: ...) { | |
} | |
getProcessedEvents(): ...[] { | |
} | |
} | |
interface EventMap { | |
login: { user?: string; name?: string; hasSession?: boolean }; | |
logout: { user?: string }; | |
} | |
class UserEventProcessor extends EventProcessor<EventMap> {} | |
const uep = new UserEventProcessor(); | |
uep.addHandler({ | |
filterLogin: ({ user }) => Boolean(user), | |
mapLogin: (data) => ({ | |
...data, | |
hasSession: Boolean(data.user && data.name), | |
}), | |
}); | |
uep.handleEvent("login", { | |
user: null, | |
name: "jack", | |
}); | |
uep.handleEvent("login", { | |
user: "tom", | |
name: "tomas", | |
}); | |
uep.handleEvent("logout", { | |
user: "tom", | |
}); | |
console.log(uep.getProcessedEvents()); | |
/* | |
Result: | |
[ | |
{ | |
eventName: 'login', | |
data: { user: 'tom', name: 'tomas', hasSession: true } | |
}, | |
{ eventName: 'logout', data: { user: 'tom' } } | |
] | |
*/ |
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
class EventProcessor { | |
handleEvent(eventName: ..., data: ...): void { | |
} | |
addFilter( | |
eventName: ..., | |
filter: (data: ...) => boolean | |
): void { | |
} | |
addMap(eventName: ..., map: (data: ...) => ...): void { | |
} | |
getProcessedEvents() { | |
} | |
} | |
interface EventMap { | |
login: { user?: string; name?: string; hasSession?: boolean }; | |
logout: { user?: string }; | |
} | |
class UserEventProcessor extends EventProcessor<EventMap> {} | |
const uep = new UserEventProcessor(); | |
uep.addFilter("login", ({ user }) => Boolean(user)); | |
uep.addMap("login", (data) => ({ | |
...data, | |
hasSession: Boolean(data.user && data.name), | |
})); | |
uep.handleEvent("login", { | |
user: null, | |
name: "jack", | |
}); | |
uep.handleEvent("login", { | |
user: "tom", | |
name: "tomas", | |
}); | |
uep.handleEvent("logout", { | |
user: "tom", | |
}); | |
console.log(uep.getProcessedEvents()); | |
/* | |
Result: | |
[ | |
{ | |
eventName: 'login', | |
data: { user: 'tom', name: 'tomas', hasSession: true } | |
}, | |
{ eventName: 'logout', data: { user: 'tom' } } | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the solution I came to