realar-project / realar

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

high: new `flow` brainstorm (signal.combine consistency) #68

Closed betula closed 3 years ago

betula commented 3 years ago
test('should work signal combine', async () => {
  const spy = jest.fn();
  const v = value(1);
  const s = signal.from(v.select(v => v + v));

// `signal.from` should be a stoppable selector, not a new signal, with a change argument subscription.
// `flow` - is a stoppable selector (maybe simple selector is enough, and make another way to create stopping for flow)
// for example `flow.filter`, and `wrap.filter` for making stoppable flow, maybe its enough. Because breaking flow a change type to `Ensurable`

// but stoppable selector is very very interesting - is the possibility to stop flow, without the creation of new `on` subscription.

// simplest way for `sel` and `on` - is making stop exception.
const s = sel(() => {
  if (v.val > 0) stop(); // This will be next exported function from reactive-box
  // It is a special exception throwing
});

// It's possible to use in another case:
on(() => {
  if (v.val > 0) stop(); // <-- its will work automatically)) because `on` used `sel` inside.
}, fn);

// ===

  const c = signal.combine(v, s);
  c.watch(v => spy(v));

  expect(c.val).toEqual([1, 2]);
  s(10);
  s(10);
  v(2);
  v(2);

  expect(spy).toHaveBeenNthCalledWith(1, [1, 10]);
  expect(spy).toHaveBeenNthCalledWith(2, [1, 10]);
  expect(spy).toHaveBeenNthCalledWith(3, [2, 10]); // Todo: hmm
  expect(spy).toHaveBeenNthCalledWith(4, [2, 4]);
  expect(spy).toBeCalledTimes(4);
});
betula commented 3 years ago

Probably combine function not so good decision. Another way:

export const allReady = ready.from(() => {
  if (!frontReady.val) return;
  if (backReady.val || !sharedCardPage().card?.image_fullscreen_back) {} else return;
  return true;
});
betula commented 3 years ago

or

export const allReady = ready.from(
  signal.from(() => {
    if (!frontReady.val) return;
    if (backReady.val || !sharedCardPage().card?.image_fullscreen_back) {} else return;
    return true;
  }).flow.filter(v => v)
);
betula commented 3 years ago

Liked https://github.com/betula/realar/issues/70 (about stoppable/stop and flow breaking)

betula commented 3 years ago

Task: The flow implementation: https://github.com/betula/reactive-box/issues/9

betula commented 3 years ago

Flow on discussion. The "combine" implemented as "value.combine" in 0.6.0