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

feat(event-bus): Use instanceof check for of type operator #1930

Merged

Conversation

yackinn
Copy link
Contributor

@yackinn yackinn commented Dec 9, 2022

No description provided.

@yackinn
Copy link
Contributor Author

yackinn commented Dec 12, 2022

@michaelbromley Is this feature set fine for now?

@yackinn
Copy link
Contributor Author

yackinn commented Dec 14, 2022

Let me know what we need tests for when we've agreed on an api for the event bus.

@michaelbromley
Copy link
Member

michaelbromley commented Dec 15, 2022

Hi,

Thanks for your contribution :) I've spent a bit more time thinking about this. I'd like to only add the filter() method. The reasons are:

  1. It is the most flexible.
  2. I want to add as few new APIs as possible, as a general rule.
  3. It's trivial to use a e => e instanceof MyBaseEvent predicate to get the ofInstance() functionality.
  4. I think ofInstance() might cause confusion - it's not obvious by the name alone what the difference is between ofType() and ofInstance(). Both methods take a type - not an instance - as an argument. The distinction is quite subtle and it forces the user to think too much about the internal implementation details.
  5. With .filter(), that implementation detail is made explicit in user code.

As this is a new feature, it should be made against the minor branch too.

For tests, you should add one or two tests to the event-bus.spec.ts file testing some predicate functions and asserting that events are correctly being filtered according to the predicate.

@yackinn
Copy link
Contributor Author

yackinn commented Dec 18, 2022

@michaelbromley Please check and verify

@@ -81,9 +81,27 @@ export class EventBus implements OnModuleDestroy {
ofType<T extends VendureEvent>(type: Type<T>): Observable<T> {
return this.eventStream.asObservable().pipe(
takeUntil(this.destroy$),
filter(e => (e as any).constructor === type),
filter(e => e.constructor === type),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The typecast doesn't seem to be needed.

@yackinn yackinn changed the base branch from master to minor December 18, 2022 12:29
@yackinn
Copy link
Contributor Author

yackinn commented Dec 19, 2022

Why are the jobs failing?

@michaelbromley michaelbromley merged commit 7eabaa7 into vendure-ecommerce:minor Dec 19, 2022
@michaelbromley
Copy link
Member

The jobs are failing because of a config issue after I added the latest plugin - nothing to do with your code.

Thanks very much for your contribution!

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