Skip to content

Conversation

@cowboyd
Copy link
Member

@cowboyd cowboyd commented Jun 19, 2025

Motivation

setInterval() is a core JavaScript API that allows you to setup global state. As such, there really isn't any reason that you shouldn't consume it using structured concurrency.

Approach

This adds an interval() function that constructs a stream that emits an item every time that the interval is triggered.

let startTime = Date.now();

for (let _ of yield* each(interval(10))) {
  let elapsed = Date.now() - startTime;
  console.log(`elapsed time: ${elapsed} ms`);
  yield* each.next();
}

@cowboyd cowboyd requested review from jbolda, neurosnap and taras June 19, 2025 11:22
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 19, 2025

Open in StackBlitz

npm i https://pkg.pr.new/thefrontside/effection@1005

commit: 1bbc61d

Copy link
Member

@taras taras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this change remains compatible with https://docs.deno.com/examples/mocking_tutorial/#faking-time?

@cowboyd
Copy link
Member Author

cowboyd commented Jun 20, 2025

@taras It definitely should because the interval helper is just another caller to setInterval() in the same way that sleep() is just a client of setTimeout(). It doesn't patch in any way.

We could actually use it to write a test to find out! .... but we'd have to make sure that it was compatible with nodejs

@taras
Copy link
Member

taras commented Jun 20, 2025

🤦 I'm sorry, I read this in passing. I didn't notice that this is a contrib update - it doesn't change the underlying sleep command.

@taras taras self-requested a review June 20, 2025 13:11
Copy link
Member

@taras taras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@jbolda jbolda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems great with me!

[`setInterval()`][1] is a core JavaScript API that allows you to setup
global state. As such, there really isn't any reason that you
shouldn't consume it using structured concurrency.

This adds an `interval()` function that constructs a stream that emits
an item every time that the interval is triggered.

```ts
let startTime = Date.now();

for (let _ of yield* each(interval(10))) {
  let elapsed = Date.now() - startTime;
  console.log(`elapsed time: ${elapsed} ms`);
}
```

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval
@cowboyd cowboyd merged commit fe97560 into v3 Aug 6, 2025
5 checks passed
@cowboyd cowboyd deleted the interval-stream branch August 6, 2025 15:36
taras pushed a commit that referenced this pull request Nov 12, 2025
✨ add interval() helper to consume a stream of intervals
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.

4 participants