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

convert Subscription from bare operation to iterator. #696

Merged
merged 1 commit into from
Apr 25, 2023

Conversation

cowboyd
Copy link
Member

@cowboyd cowboyd commented Apr 24, 2023

Motivation

fixes #693

In order to simplify working with streams and subscriptions, we need to be able to disambiguate them easily. The relationship between Stream and Subscription is the same as that between Iterable -> Iterator and AsyncIterable -> AsyncIterator.

Approach

This converts Subscription into an API that is highly analogous to AsyncIterator. The next() method returns an Operation which yields an IteratorResult So in the same way AsyncIterator has:

next(): Promise<IteratorResult>;

The Subscription api will look like:

next(): Operation<IteratorResult>;

Once we have this in place, it is trivial to create a stream out of any Subscription:

function streamable(subscription: Subscription): Stream {
  return ({
    *[Symbol.iterator]() {
      return subscription;
    }
  });
}

We may decide later to rename Stream and Subscription to something else that re-enforces this analogy further. However, we haven't been able to settle on anything that obviously clicks, so we'll defer that for now, and instead focus on the API.

> fixes #693

In order to simplify working with streams and subscriptions, we need
to be able to disambiguate them easily. The relationship between
`Stream` and `Subscription` is the same as that between `Iterable`
-> `Iterator` and `AsyncIterable` -> `AsyncIterator`.

This converts `Subscription` into an API that is highly analogous to
`AsyncIterator`. The `next()` method returns an `Operation` which
yields an `IteratorResult` So in the same way `AsyncIterator` has:

```ts
next(): Promise<IteratorResult>;
```

The `Subscription` api will look like:

```ts
next(): Operation<IteratorResult>;
```

Once we have this in place, it is trivial to create a stream out of
any Subscription:

```ts
function streamable(subscription: Subscription): Stream {
  return ({
    *[Symbol.iterator]() {
      return subscription;
    }
  });
}
```
@cowboyd cowboyd merged commit d47a7ac into v3 Apr 25, 2023
@cowboyd cowboyd deleted the revamp-subscription branch April 25, 2023 21:02
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.

None yet

2 participants