microstates icon indicating copy to clipboard operation
microstates copied to clipboard

Stable Queries

Open cowboyd opened this issue 5 years ago • 3 comments

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.

cowboyd avatar Jul 25 '18 18:07 cowboyd

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.

taras avatar Jul 25 '18 19:07 taras

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.

cowboyd avatar Jul 25 '18 23:07 cowboyd

That sounds 🔥

taras avatar Jul 26 '18 00:07 taras