fluxo-js / fluxo

Fluxo is a simple, lightweight (~300LOC) and dependency free data infrastructure lib based on Facebook Flux and Backbone.js. It's compatible with React.js, but you can use with whatever you want to the view/component layer.
15 stars 3 forks source link

Subset and computed properties first computation order #18

Closed samuelsimoes closed 8 years ago

samuelsimoes commented 8 years ago

Today, the subsets are computed on the store construction after the computed properties first compution. This works for most of cases, but you could get on this kind of situation:

class Todos extends Fluxo.CollectionStore {
  pendingCount () {
    return this.subsets.pending.stores.length;
  }

  pending () {
    return this.where({ done: false });
  }
};

Todos.computed = {
  pendingCount: ["change:pending"]
};

Todos.subset = {
  pending: ["stores:change:done"]
};

var todos = new Todos([{ done: false }, { done: true }]);

todos.data.pendingCount // 0;

We can easily fix this computing subsets first on the construction, but Isn't hard imagine situations where subset can depend on some computed property.

I think we need a way to compute every computed property and subset on first object construction computing the dependencies first.

I already tried something, but it doesn't look a simple task to get this with elegant code.

samuelsimoes commented 8 years ago

Like discussed on chat, let first fix this with a more simple approach, computing subsets before computed properties (https://github.com/fluxo-js/fluxo/commit/f2c7c8aface54906c58f5cf5b5b845a786ca874f), if this kind of dependency managment turns into a necessary thing, we reconsiders.

samuelsimoes commented 8 years ago

I just have an epiphany moment to fix this issue, not that I had this problem recently but I really think the Fluxo's first computation on store construction is inconsistent with what happen on the subsequent state mutations, so I thought on a little tweak that could address this issue.

The basically idea is:

  1. Start a wildcard listener to collect all events.
  2. Do the Fluxo.ObjectStore#set on the constructor.
  3. Do the Fluxo.CollectionStore#setStores on the constructor (if it's a collection).
  4. Register the computed and subsets.
  5. Retrigger all events collected by the listener on the first step and everything will work on the chosen order by the dependent events.

Take a look in the branch https://github.com/fluxo-js/fluxo/compare/smart-first-computation.

What do you think?