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

Computed properties inferring dependents attributes #25

Closed samuelsimoes closed 8 years ago

samuelsimoes commented 8 years ago

I was thinking that computed properties could have some sort of magic that dependent attributes would be read from the computer function's text body.

Example: you have a computed property about if you can add a ToDo, the canAddTodos property, that depends on this.data.todosCount, like this:

class Todo extends Fluxo.ObjectStore {
  static computed = { canAddTodos: [] };

  canAddTodos () {
    return this.data.todosCount <= 10;
  }
}

Reading the method body string we could infer that this computed property relies upon the todosCount store property, so we can cut the necessity to user declares the change:todosCount event to compute the value, speeding up the things.

What do you think?

samuelsimoes commented 8 years ago

The main idea: https://github.com/fluxo-js/fluxo/commit/0a2dbb54c5a4d4af96c8fbd4671ab6829a1eadd7

samuelsimoes commented 8 years ago

We can extend this behavior to subsets too.

And we can also infer the add and remove events automatically when we find this.where on some computer function. My main concern with this will be the nested stores feature, nested stores won't be caught by the patterns described above.

sobrinho commented 8 years ago

I really prefer to be explicit because this can break in all sort of things, i.e.:

canAddTodos () {
  const data = this.data;

  // alias
  return data.foo && data.bar;
}

canAddTodos () {
  // different context (i.e.: this on the function is about the SomeService)
  return SomeService.call(function () { return this.data.foo });
}

Although we can improve the current API to something like:

fullName (firstName, lastName) {
  return `${firstName} ${lastName}`;
}

In that case the dependent attributes would be explicit at the method definition.

samuelsimoes commented 8 years ago

On the second example, I need to declare change:firstName and change:lastName events? Or Fluxo will read the arguments and infer the dependent events?

sobrinho commented 8 years ago

They would be inferred.

Since these compute functions does never receive any argument, we could use the argument names to infer what is needed to the compute.

samuelsimoes commented 8 years ago

Great. I liked. :ok_hand: Can I Implement this?

sobrinho commented 8 years ago

"Go get 'em, Tiger."

samuelsimoes commented 8 years ago

Do you know some safe way to get the function arguments? Thus far I only know the regexp /\([\w|\s|,]+\)/ on the function's string, but it's fragile. :pensive:

var foo = function (bar) {};
foo.arguments; // => undefined
sobrinho commented 8 years ago

Apparently there is no native reflection for functions, only regexps: http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript

We could make some kind of transpiler (babel transpiler) but it doesn't worth at all.

Honestly, I don't think it's worth the complexity, too little benefit to too much complexity, explicit is always better than implicit (zen of python).

samuelsimoes commented 8 years ago

Alright, I also agree that It doesn't worth the complexity. Closing...