Accessing a public method in a listener's class

Hi, I currently have a public method on my listener that i want to access from another listener, below is an example of my current code:
...
export class UserEvent extends Listener {
private interval: NodeJS.Timeout | null = null;

public override run() {
if (!config.enabled) return;
this.startInterval();
}

public async resetInterval() {
if (this.interval) {
clearInterval(this.interval);
this.startInterval();
}
}

private startInterval() {
this.interval = setInterval(async () => {
this.assignMember();
}, 1000 * 60 * 60);
}
...
...
export class UserEvent extends Listener {
private interval: NodeJS.Timeout | null = null;

public override run() {
if (!config.enabled) return;
this.startInterval();
}

public async resetInterval() {
if (this.interval) {
clearInterval(this.interval);
this.startInterval();
}
}

private startInterval() {
this.interval = setInterval(async () => {
this.assignMember();
}, 1000 * 60 * 60);
}
...
my goal here is to call resetInterval() from another listener, but i currently have no idea how to call it without calling new on the listener above.
Solution:
Seeing you use TypeScript, you can do this instead:```ts import type { UserEvent as TimerUserEvent } from './event-name.js'; export class UserEvent extends Listener { ......
Jump to solution
2 Replies
Solution
kyra
kyra•2w ago
Seeing you use TypeScript, you can do this instead:
import type { UserEvent as TimerUserEvent } from './event-name.js';

export class UserEvent extends Listener {
...

public myMethod() {
const timer = this.store.get('event-name') as TimerUserEvent;
timer.resetInterval();
}
}
import type { UserEvent as TimerUserEvent } from './event-name.js';

export class UserEvent extends Listener {
...

public myMethod() {
const timer = this.store.get('event-name') as TimerUserEvent;
timer.resetInterval();
}
}
That being said, pieces are supposed to be independent, so I would recommend you have a shared file for the two listeners and call from there rather than having a listener call another listener, it'll also solve casting and naming issues.
choke
chokeOP•2w ago
thanks, guess i overdid myself again 😅

Did you find this page helpful?