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

Added PubSub.waitFor #13

Merged
merged 2 commits into from
May 6, 2024
Merged

Conversation

dPaskhin
Copy link
Contributor

@dPaskhin dPaskhin commented May 3, 2024

No description provided.

* @param predicate A function that takes the message as a parameter and returns true if the message satisfies the condition, otherwise false.
* @returns A promise that resolves with the published message that satisfies the predicate.
*/
waitFor(predicate: (message: T) => boolean): Promise<T> {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
waitFor(predicate: (message: T) => boolean): Promise<T> {
waitFor<R extends T>(predicate: (message: T) => message is R): Promise<R>;
waitFor(predicate: (message: T) => boolean): Promise<T>;
waitFor(predicate: (message: T) => boolean) {

Comment on lines 59 to 70
const pubSub = new PubSub<number>();

pubSub
.waitFor(message => message === 42)
.then(message => {
expect(message).toBe(42);
});

pubSub.publish(10);
pubSub.publish(20);
pubSub.publish(42);
pubSub.publish(30);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const pubSub = new PubSub<number>();
pubSub
.waitFor(message => message === 42)
.then(message => {
expect(message).toBe(42);
});
pubSub.publish(10);
pubSub.publish(20);
pubSub.publish(42);
pubSub.publish(30);
const pubSub = new PubSub<number>();
const promise = pubSub.waitFor(message => message === 42);
pubSub.publish(10);
pubSub.publish(20);
pubSub.publish(42);
pubSub.publish(30);
await expect(promise).resolves.toBe(42);

pubSub.publish(30);
});

test('does not resolve if no message matches the predicate', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

This test is redundant. The scenario is covered in the preceding test.

Comment on lines 43 to 69
return new Promise(resolve => {
const unsubscribe = this.subscribe(message => {
if (predicate(message)) {
resolve(message);
unsubscribe();
}
});
});
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
return new Promise(resolve => {
const unsubscribe = this.subscribe(message => {
if (predicate(message)) {
resolve(message);
unsubscribe();
}
});
});
return new AbortablePromise((resolve, _reject, signal) => {
const unsubscribe = this.subscribe(message => {
if (predicate(message)) {
resolve(message);
unsubscribe();
}
});
signal.addEventLisetner('abort', unsubscribe);
});

@dPaskhin dPaskhin force-pushed the pubsub-wait-for branch from d780253 to fea3fe1 Compare May 6, 2024 15:33
@smikhalevski smikhalevski changed the title Added waitFor method in PubSub Added PubSub.waitFor May 6, 2024
@smikhalevski smikhalevski merged commit 6b49026 into smikhalevski:master May 6, 2024
1 check passed
@dPaskhin dPaskhin deleted the pubsub-wait-for branch May 6, 2024 15:59
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