Truebase-com / TruthStack

Monorepo for the Truth technology stack.
10 stars 1 forks source link

Implement support for attribute value stacks #13

Open paul-go opened 5 years ago

paul-go commented 5 years ago

We should consider providing a kind of "undo" history on the attribute values applied to a branch. This is necessary when there are multiple recurrent functions affecting the same attribute. For example:

const clicked = reflex(false);
const dblclicked = reflex(false);

ml.div(
  on("click", () => clicked.flip() ? { attr: 1 } : {})),
  on("dblclick", () => dblclicked.flip() ? { attr: 2 } : {}))
);

Behavior:

At the current moment, the behavior actual behavior is a bit unpredictable (which is why I've labeled this as a bug)

kaaninel commented 5 years ago

We would probably need some kind of priority system if we want this behavior.

const clicked = reflex(false);
const dblclicked = reflex(false);
const result = priotirizedreflex({});
ml.div(
  on("click", () => result.set(1, clicked.flip() ? { attr: 1 } : {}))),
  on("dblclick", () => result..set(2, dblclicked.flip() ? { attr: 2 } : {})))
);

initial condition would have 0 priority and set function's first argument can take priority of that object.

function calculatePrioritized(values){
  const result = {};
  values.sort((a, b) => a.priority - b.priority).forEach(x => {
    Object.assign(result, x.data);
  }
  return result;
}

in most basic terms something like this.

calculatePrioritized([{priority: 1, data: {attr: 1}},{priority: 0, data: {attr: 2}}])
->{attr: 1}
calculatePrioritized([{priority: 1, data: {attr: 1}},{priority: 2, data: {attr: 2}}])
->{attr: 2}
calculatePrioritized([{priority: 1, data: {attr: 1}},{priority: 2, data: {attr: 2}}, {priority: 3, data:{}}])
->{attr: 2}