-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
src/main/PubSub.ts
Outdated
* @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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
src/test/PubSub.test.ts
Outdated
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
src/test/PubSub.test.ts
Outdated
pubSub.publish(30); | ||
}); | ||
|
||
test('does not resolve if no message matches the predicate', () => { |
There was a problem hiding this comment.
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.
src/main/PubSub.ts
Outdated
return new Promise(resolve => { | ||
const unsubscribe = this.subscribe(message => { | ||
if (predicate(message)) { | ||
resolve(message); | ||
unsubscribe(); | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |
}); |
No description provided.