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

Specifying the async iterator #50

Closed
jakearchibald opened this issue Oct 8, 2020 · 2 comments
Closed

Specifying the async iterator #50

jakearchibald opened this issue Oct 8, 2020 · 2 comments

Comments

@jakearchibald
Copy link

Sorry @domenic, I think I need your help on this one.

If we were to solve #27, I'd go for an API like:

class SystemFonts {
  async* matchAll(query = {}) {
    while (true) {
      const match = await getNextMatchSomehow(query);
      if (match) yield match;
      else return;
    }
  }

  [Symbol.asyncIterator]() {
    return this.matchAll();
  }
}

self.systemFonts = new SystemFonts();

In this case:

for await (const match of self.systemFonts) 

…is an alias of:

for await (const match of self.systemFonts.matchAll())  

But matchAll provides the ability to query:

for await (const match of self.systemFonts.matchAll({ name: 'foo' }))  

You could also do:

for await (const match of self.systemFonts[Symbol.asyncIterator]({ name: 'foo' }))  

…but I think it's worth providing the named method for ergonomics.

However, I can't see a way in IDL to declare a method that returns an async iterator like this. I guess I could call the @@asynciterator directly, but that seems not-how-we-usually-do-things.

@domenic
Copy link

domenic commented Oct 8, 2020

Async iterators can take arguments! So there's no need for matchAll(); you can just use self.systemFonts[Symbol.asyncIterator]({ name: 'foo' }), or self.systemFonts.values({ name: 'foo' }). This is done in IDL by using the syntax

async iterable<value_type>(optional SomeOptionsDict options = {});

For specifying this, see https://streams.spec.whatwg.org/#rs-asynciterator plus the IDL block at https://streams.spec.whatwg.org/#rs-class-definition for an example.

@jakearchibald
Copy link
Author

Ohhh, self.systemFonts.values({ name: 'foo' }) provides the ergonomics I was looking for. I felt self.systemFonts[Symbol.asyncIterator]({ name: 'foo' }) was too ugly.

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

2 participants