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

♻️ Return to Stream as Operation #859

Merged
merged 3 commits into from
Dec 16, 2023
Merged

♻️ Return to Stream as Operation #859

merged 3 commits into from
Dec 16, 2023

Conversation

cowboyd
Copy link
Member

@cowboyd cowboyd commented Dec 16, 2023

Motivation

Now that we have each(), you end up writing subscriptions more often than you use them. The idea behind subscribe() was that yielding directly to a stream to instantiate a subscription was confusing, which at first blush is still true.

However, the decision to add a subscribe() method directly was made on the assumption that you would be using subscriptions by hand more often and so a little more
ceremony around authoring subscriptions was warranted.

That doesn't seem to hold anymore now that we have each() since it's almost always the
right thing to do instead of using to the subscription directly. The only case is when you're doing meta-level stuff in which case, understanding the underlying stream/subscription mechanics as a precursor is ok.

For example, adapting to this event emitter goes from:

export function onEmit(emitter, name) {
  return {
    subscribe() {
      return resource(function*(provide) {
        let signal = createSignal();
        emitter.on(name, signal.send);
        try {
          yield* provide(yield* signal.subscribe());
        } finally {
          emitter.off(name, signal.send);
        }
      });
    }
  }
}

to this:

export function onEmit(emitter, name) {
  return resource(function*(provide) {
    let signal = createSignal();
    emitter.on(name, signal.send);
    try {
      yield* provide(yield* signal);
    } finally {
      emitter.off(name, signal.send);
    }
  });
}

Approach

Optimize for making streams easy to define

Now that we have each(), you end up writing subscriptions more often
than you use them. The idea behind subscribe() was that yielding
directly to a stream to instantiate a subscription was confusing,
which at first blush is still true.

However, the decision to add a subscribe() method directly was made on
the assumption that you would be using subscriptions by hand more
often and so a little more
ceremony around authoring subscriptions was warranted.

That doesn't seem to hold anymore now that we have each() since it's
almost always the
right thing to do instead of using to the subscription directly. The
only case is when you're doing meta-level stuff in which case,
understanding the underlying stream/subscription mechanics as a
precursor is ok.

For example, adapting to this event emitter goes from:

```js
export function onEmit(emitter, name) {
  return {
    subscribe() {
      return resource(function*(provide) {
        let signal = createSignal();
        emitter.on(name, signal.send);
        try {
          yield* provide(yield* signal.subscribe());
        } finally {
          emitter.off(name, signal.send);
        }
      });
    }
  }
}
```

to this:

```js
export function onEmit(emitter, name) {
  return resource(function*(provide) {
    let signal = createSignal();
    emitter.on(name, signal.send);
    try {
      yield* provide(yield* signal);
    } finally {
      emitter.off(name, signal.send);
    }
  });
}
```
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.

👍 very nice

@taras taras merged commit 78e0d59 into v3 Dec 16, 2023
3 checks passed
@taras taras deleted the stream-as-operation branch December 16, 2023 15:13
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