Skip to content

Singleton observables#4

Merged
hntrl merged 6 commits intodevfrom
feature/singleton-observable
Apr 12, 2025
Merged

Singleton observables#4
hntrl merged 6 commits intodevfrom
feature/singleton-observable

Conversation

@hntrl
Copy link
Owner

@hntrl hntrl commented Apr 12, 2025

Introduces SingletonAsyncObservable; a utility class for observables that lets you access the value emitted by observables that emit one (and only one) value (like the observables returned from reduce(), count(), etc.) using native await syntax.

This makes the consumption of these single value operators a little bit more readable. For instance:

const obs = AsyncObservable.from([1, 2, 3]);
const singleton = obs.pipe(first());

// instead of this:
let firstValue: number | undefined;
await obs.subscribe((value) => {
  firstValue = value;
});
console.log(firstValue); // 1

// you can just do this:
console.log(await singleton); // 1

@changeset-bot
Copy link

changeset-bot bot commented Apr 12, 2025

🦋 Changeset detected

Latest commit: 1371b77

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@hntrl hntrl merged commit 4e8835f into dev Apr 12, 2025
2 checks passed
@hntrl hntrl deleted the feature/singleton-observable branch April 12, 2025 21:10
@hntrl hntrl mentioned this pull request Apr 14, 2025
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.

1 participant