Skip to content

Fix lack of timer cleanup when terminated by sink.#13

Merged
atomrc merged 2 commits intoatomrc:masterfrom
bjnsn:sink-terminate
Mar 5, 2021
Merged

Fix lack of timer cleanup when terminated by sink.#13
atomrc merged 2 commits intoatomrc:masterfrom
bjnsn:sink-terminate

Conversation

@bjnsn
Copy link
Copy Markdown
Contributor

@bjnsn bjnsn commented Mar 2, 2021

Cleans up timer when terminated by sink. This resolves an issue that shows up when using debounce when switching between callbag streams (ala RXJS SwitchMap).

See https://stackblitz.com/edit/typescript-ufcfks?file=index.ts

import { interval, merge, map, forEach, pipe, flatten } from "callbag-basics";
import of from "callbag-of";
import wait from "callbag-wait";
import { debounce } from "callbag-debounce";

pipe(
  // start with 0, then pass 1 after 4 seconds
  merge(
    of(0),
    pipe(
      of(1),
      wait(2000)
    )
  ),
  map(val => {
    if (val === 0) {
      // when 0, pass a stream with some noisy input data
      return pipe(
        // some data that shows up randomly
        merge(interval(1000 / 5), interval(1000 / 6), interval(1000 / 7)),
        // but debounce it
        debounce(111),
        map(val => `noisy ${val}`)
      );
    } else {
      // when 1, pass a stream of 'complete'
      return of("complete");
    }
  }),
  // take the values from the most recent stream
  // ala RXJS switchmap
  flatten,
  // but we recieve a noisy data value after
  // switching to the complete stream because
  // debounce fails to handle the 'terminate'
  // signal from its sink
  forEach(val => console.log(val))
);

@atomrc
Copy link
Copy Markdown
Owner

atomrc commented Mar 3, 2021

Hey @bjnsn
Great thanks for the PR, looks good to me. One thing, though, do you think you could add tests to cover this scenario?
That would be awesome!

@bjnsn
Copy link
Copy Markdown
Contributor Author

bjnsn commented Mar 4, 2021

Test submitted!

@atomrc
Copy link
Copy Markdown
Owner

atomrc commented Mar 5, 2021

Awesome thanks. Will deploy new version very soon 🙏

@atomrc atomrc merged commit 157d86c into atomrc:master Mar 5, 2021
@atomrc
Copy link
Copy Markdown
Owner

atomrc commented Mar 5, 2021

Published under v 3.0.0. Thanks again @bjnsn

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

Successfully merging this pull request may close these issues.

2 participants