datavis-tech / reactive-model

A library for reactive dataflow programming.
MIT License
61 stars 6 forks source link

Minimal Rewrite #45

Closed curran closed 6 years ago

curran commented 6 years ago

This library feels too heavy, with all the submodules, and some features like serialization that have not seen much use in practice. Proposed new API, inspired by https://observablehq.com :

Before:

var my = ReactiveModel()
  ("a") // Create the property "a" with no default value.
  ("b", function (a){
    return a + 1;
  }, "a");
})

After:

const model = reactiveModel({
  b: [a => a + 1, 'a']
})

Before:

function increment(x){ return x + 1; }
var my = ReactiveModel()
  ("a", 5) // Create the property "a" with a default value of 5.
  ("b", increment, "a")
  ("c", increment, "b");

After:

const increment = x => x + 1;
const model = reactiveModel({
  a: 5,
  b: [increment, 'a'],
  c: [increment, 'b']
})

Before:

function add(x, y){ return x + y; }

var my = ReactiveModel()
  ("c", 5)
  ("d", 10)
  ("e", add, ["c", "d"]);

After:

// An object will be used when more than one dependency,
// because long argument lists was a problem when using the library.
// In this project https://github.com/unhcr/dataviz-streamgraph-explorer
cosnt add = ({x, y}) => x + y;
const model = reactiveModel({
  c: 5,
  d: 10,
  e: [add, 'c, d']
})

Before:

var my = ReactiveModel()
  ("firstName")
  ("lastName");
my("fullName", function (firstName, lastName){
  return firstName + " " + lastName;
}, "firstName, lastName");
my.firstName("Jane").lastName("Smith");

After:

const model = reactiveModel({
  fullName: ({firstName, lastName}) => `${firstName} ${lastName}`
});

// Setting like this will let us propagate the changes synchronously,
// when setting multiple fields, not relying on requestAnimationFrame,
// which complicated the implementation unnecessarily.
model.set({
  firstName: 'Jane',
  lastName: 'Smith'
});
curran commented 6 years ago

Done https://github.com/datavis-tech/topologica