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

Make Poll an async iterable #386

Merged
merged 6 commits into from
Sep 2, 2022
Merged

Conversation

afshin
Copy link
Member

@afshin afshin commented Aug 31, 2022

In this PR, the IPoll interface and the Poll class extend AsyncIterable (by implementing a Symbol.asyncIterator method) to support for-await...of loops.

This PR also adds a README.md file for the @lumino/polling package.

This functionality may be a natural fit for streams of events from the new Jupyter event system, other WebSocket connections, or other stream sources.

cc: @Zsailer @andrii-i @davidbrochart @3coins @vidartf (since you all have some interest in events and/or iterators)


Example usage

The tests in this PR are illustrative. Here is one of them with some annotation:

Here, we set up the testing state variables and create a new Poll instance.

const total = 2;
let i = 0;
poll = new Poll({
  auto: false,
  factory: () => Promise.resolve(++i > total ? poll.dispose() : void 0),
  frequency: { interval: Poll.IMMEDIATE },
  name: '@lumino/polling:Poll#[Symbol.asyncIterator]-3'
});
const expected = `started ${'resolved '.repeat(total)}disposed`;
const ticker: IPoll.Phase<any>[] = [];

Then the poll is started:

void poll.start();

Instead of connecting to the ticked signal or awaiting the tick promise, we can now use a for-await...of loop:

for await (const state of poll) {
  ticker.push(state.phase);
  if (poll.isDisposed) {
    break;
  }
}

And we check to make sure the results are as expected:

// ticker and expected are both: 'started resolved resolved disposed'
expect(ticker.join(' ')).to.equal(expected);

Note for consumers of async iterators

The Poll class itself only uses ES6 (and DOM) types in its lib collection but in order to use for-await...of loops in TypeScript, you will need to use ES2018 or above in your lib array in tsconfig.json.


Fixes #385
cf. partially addresses #339

@afshin afshin added the enhancement New feature or request label Aug 31, 2022
@afshin afshin added this to the Lumino 2 milestone Aug 31, 2022
@afshin afshin self-assigned this Aug 31, 2022
@afshin afshin changed the title Make Poll an async iterator Make Poll an async iterable Aug 31, 2022
@blink1073
Copy link
Contributor

Maybe add those notes to the README for polling?

@afshin
Copy link
Member Author

afshin commented Aug 31, 2022

I was going to ask "what README?" but the question answers itself 🤣

Copy link
Contributor

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

Copy link
Member

@fcollonval fcollonval left a comment

Choose a reason for hiding this comment

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

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Poll an async iterable
3 participants