In order to “separate the concerns” in the logic of one of my components, I want to create an object that is only meant to listen to some events and compute a field accordingly.
My problem is that it needs to unsubscribe from the listening when disposed. To do that, I’m using detached().
It seems to work.
But is it a good idea? This so-called component has no view. It acts more like a proxy service. But it does rely on Aurelia’s “@computedFrom
”.
So… Is there another way? Is it horrifying?
@autoinject
export class MyListener implements ComponentAttached, ComponentDetached {
// @ts-ignore
// This value has no meaning in itself; it is used to trigger recalculations
private mToggle: boolean = false;
constructor(private mEventSubscriber: EventSubscriber) {
this.mEventSubscriber.subscribe(Events.SOMETHING_CHANGED, () => {
this.mToggle = !this.mToggle;
});
}
@computedFrom("mToggle")
get someImportantValue(): boolean {
return someComplicatedCalculations(...);
}
public detached(): void {
this.mEventSubscriber.removeAll();
}
}