realar-project / realar

5 kB Advanced state manager for React
MIT License
44 stars 0 forks source link

normal: add interceptor method #67

Closed betula closed 3 years ago

betula commented 3 years ago

const a = value(5);
const b = value(1);

const unsub = a.interceptor((new_value, current_value, stop?) => {
  if (new_value == 1) {
    b.set(10);
    stop();
    return;
  }
  return current_value + new_value;
});
// stoppable supported

a(1); // b.val === 10, a.val === 5
a(2); // a.val === 7
betula commented 3 years ago

Think about syntax for sub interceptions

const v = value(0);
const s = signal(0);
v.sub.intercept(s, (v_value, new_signal_value, stop ) => {
  ///
});
v.intercept.sub // alias

// or
v.sub.intercept(s, (v_value, new_signal_value, prev_signal_value ) => {
  stoppable.stop();
});
v.intercept.sub // alias
betula commented 3 years ago

Added to "the stream of conciseness 0.7+ roadmap"