Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@cycle/time: surprising behavior of Time.diagram in combination with flatten #876

Open
rbelouin opened this issue Feb 23, 2019 · 0 comments

Comments

@rbelouin
Copy link

Code to reproduce the issue:

I have tried to write a high-level test for the routing of my application, with the help of Time.diagram. It kinda looks like this snippet:

it('renders the right component', done => {
  const Time = mockTimeSource();
  const DOM = mockDOMSource({});

  const route$ = Time.diagram('A--B--AB--');
  const ComponentA = sources => ({
    DOM: Time.diagram('a-a')
  });
  const ComponentB = sources => ({
    DOM: Time.diagram('bb')
  });

  const expected = Time.diagram('a-abb-abb-');
  const actual = AppBuilder(ComponentA, ComponentB)({ DOM, route$ }).DOM;

  Time.assertEqual(actual, expected);

  Time.run(done);
});

The test surprisingly didn't pass, and I have started to suspect that Time.diagram was not behaving like I was expecting it to be. So I have written this test for flatten in time/test/xstream.ts to challenge my assumptions:

describe('flatten', () => {
  it('works in combination with map and a stream factory', done => {
    const Time = mockTimeSource();

    const input = Time.diagram('-A-------B-------|').map(letter =>
      Time.diagram('x-x-x|')
    );

    const actual = input.flatten();

    const expected = Time.diagram('-x-x-x---x-x-x---|');

    Time.assertEqual(actual, expected);

    Time.run(done);
  });
});

Expected behavior:

As described above, I would expect the returned stream to match -x-x-x---x-x-x---|. Meaning that the stream returned by the mapper function starts emitting only when it starts being instantiated and consumed. That's at least my interpretation of how Time.diagram should work, I'm happy to discuss any reasons it shouldn't behave this way.

Actual behavior:

The stream actually returned is x-x-x------------| (where every x hides another one). This is again very surprising, because it means that the stream returned by Time.diagram has been able to emit events anterior to its instantiation.

I'm happy to help with the fix if this issue gets identified as a bug.

Versions of packages used:

@cycle/time at version 0.19.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant