I’ve recently started using aurelia-store to tackle global filters in our application (e.g. when toggling a global tag filter, all resources throughout the app should be filtered on that tag), though I’m running in some issues and would like your advise.
Take the following sample component:
@connectTo<State>({
selector: {
foo: store => store.state.pipe(pluck("foo")),
filters: store => store.state.pipe(pluck("filters"))
}
})
export class MySampleComponent {
foo;
filters;
filteredFoo;
private _doSomethingHeavy() {
// Heavy lifting
}
private _computeFilteredFoo() {
// complex computation
}
fooChanged() {
this._doSomethingHeavy();
this._computeFilteredFoo();
}
filtersChanged() {
this._computeFilteredFoo();
}
}
With this component there are 2 major issues:
-
Whenever anything in the state changes (even something unrelated), both foo and filters are also triggered as changed, I can assume this is because the overall state changed. This kills performance however since now any state change will compute heavy work and causes rendering issues. I would be able to work around this by either mutating the existing state instead or do some complex merging with a local copy, neither sound right. What would be the best practice here?
-
Whenever fooChanged or filtersChanged gets called, neither foo or filters have been set yet, so I would either have to accept the new state as an argument, or schedule a processing job in the background. Is this a bug? For clarification:
// assuming current foo is 1
store.dispatch(updateFoo, 2);
fooChanged(newFoo) {
console.log(this.foo); // 1
console.log(newfoo); // 2
window.setTimeout(() => {
console.log(this.foo); // 2
);
}