-
In reactive systems like rxjs you can have observables that keep state. Would be like a mobx.computed that keeps state returns it's last state. Is it possible in mobx? |
Beta Was this translation helpful? Give feedback.
Answered by
urugator
Sep 17, 2022
Replies: 1 comment
-
Side effects in computeds are generally discouraged, but it's probably fine to mutate non-observable that acts as a local state and is not exposed to anything else: function scan(reduce, initialValue, keepAlive = false) {
let prevValue = initialValue;
let index = 0;
const _computed = computed(() => {
prevValue = reduce(prevValue, index)
index++;
return prevValue;
}, { keepAlive })
if (!keepAlive) {
// Reset if unsubscribed
onBecomeUnobserved(_computed, () => {
prevValue = initialValue;
index = 0;
})
}
return {
get() {
return _computed.get();
}
}
} Did not test. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rickmed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Side effects in computeds are generally discouraged, but it's probably fine to mutate non-observable that acts as a local state and is not exposed to anything else:
Did not test.