Extending Listener base class

So I wanted to extend the Listener base class by running some code for every listener before the actual run handler is invoked. Ideally I'd like to shim it somehow so existing listeners still just implement run so removing or adding more shims like this is easily possible without renaming methods implemented in the listener itself.
13 Replies
BigBrainAFK
BigBrainAFKOP9mo ago
I am expecting this just isnt possible with how inheritance works and would require changes to the Listener class of saphhire
Favna
Favna9mo ago
export class MyCustomListener extends Listener() {

public override run(the args go here) {
customCodeGoesHere();
super.run(the args go here);
}
export class MyCustomListener extends Listener() {

public override run(the args go here) {
customCodeGoesHere();
super.run(the args go here);
}
isn't this just what you want? Then you extend MyCustomListener in your pieces that need to run the code and extends Listener for those that dont
BigBrainAFK
BigBrainAFKOP9mo ago
yeah but this doesnt work cause super has no run method as its an abstract method oh wait it overrides the run yeah no still limited due to abstract not being callable/accessible
Favna
Favna9mo ago
oh wait right yeah well then you dont need to call the super at all. just extend the class then make your class extend your extended class
// src/listeners/custom.ts

export class MyEvent extends MyCustomListener {
public override run(...) {
super.run();
}
}
// src/listeners/custom.ts

export class MyEvent extends MyCustomListener {
public override run(...) {
super.run();
}
}
// src/other/or/something/MyCustomListener.ts
export class MyCustomListener extends Listener {

public override run(the args go here) {
customCodeGoesHere();

}
}
// src/other/or/something/MyCustomListener.ts
export class MyCustomListener extends Listener {

public override run(the args go here) {
customCodeGoesHere();

}
}
BigBrainAFK
BigBrainAFKOP9mo ago
yeah that would fully override the run of the custom listener though
Favna
Favna9mo ago
edited
BigBrainAFK
BigBrainAFKOP9mo ago
ah yeah the super call is what i wanna sort of avoid as it would mean duplicate code again (i could just call another function the without having a custom listener). i guess ill just have a different abstract function in my custom class and run that one from its implementation of run and then ill just have to compomise with that
vladdy
vladdy9mo ago
you technically could override our internal _run method for it, but be careful
BigBrainAFK
BigBrainAFKOP9mo ago
yeah no thats smth id not want to do personally. they are internal for good reasons usually. might be something nice to have though as an extendable optional feature to have pre and post functions on the listener class which can be handled by a custom listener class
Favna
Favna9mo ago
just keep in mind that if we add that, then an error in prerun or postrun wont end up in ListenerError https://github.com/sapphiredev/framework/blob/4cc93a925916346bd713dff1e015dc0092e10bcf/src/lib/structures/Listener.ts#L98-L101 we'd get PreListenerError and PostListenerError
BigBrainAFK
BigBrainAFKOP9mo ago
yeah that would be fine though as it also means that its easier to know if the error is from the listener itself or a pre/post condition
Favna
Favna9mo ago
only thing I'm doubting about now is how far we want to take this. For example do we want all Pieces to support it. And if we do, then how do we handle messageRun vs chatInputRun vs contextMenuRun for commands.
vladdy
vladdy9mo ago
commands already have pre and post events realistically, all _run does is call your actual run method and emit listenererror if an error is thrown
Want results from more Discord servers?
Add your server