microstates
microstates copied to clipboard
Stable Queries
Queries are achieved via computed properties. They are ways of collecting and viewing a set of microstates that are already defined within a given microstate. For example:
import { filter } from 'microstates';
class TodoMVC {
todos = [Todo];
get completed() {
return filter(this.todos, todo => todo.state.isCompleted)
}
}
This is great, except when it comes to creating an identity out of a microstate. There we map over each node in the microstate tree and assign an identity to it. The problem is that Filter
and Union
objects aren't microstates and won't be mapped. We need to make the identity mapping traverse queries like a Filter
so that it can get to (and create identities for) microstates on the other side.
question How important is it for the identity for each microstate to be the same inside every point in the tree. For example, should the same instance of Todo
be represented by the same identity in both the todos
array and the completed
array. My guess is yes.
How important is it for the identity for each microstate to be the same inside every point in the tree. For example, should the same instance of Todo be represented by the same identity in both the todos array and the completed array. My guess is yes.
My guess is also yes. If we can do this, then we should.
Ok, I think I know how to do this too. We can keep a cache of identities against Type, value
pairs through the course of mapping the tree. Each time we map the identity tree, we throw away the old cache and start with a new one, so the cache will never grow beyond the values actually contained within the tree at a given time.
That sounds 🔥