Skip to content

Commit

Permalink
fix(ui): Event bus method types (fix: #15759) (#15765)
Browse files Browse the repository at this point in the history
* fix(ui): Make off method optional again

* fix(ui): Fix naming in docs

* feat(ui): Improve ctx type of EventBus

* Update event-bus-util.md

---------

Co-authored-by: Yusuf Kandemir <yusuf.kandemir@outlook.com.tr>
Co-authored-by: Razvan Stoenescu <razvan.stoenescu@gmail.com>
  • Loading branch information
3 people committed Apr 28, 2023
1 parent cea8485 commit 940a02f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/pages/quasar-utils/event-bus-util.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bus.on('some-event', (arg1, arg2, arg3) => {
bus.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value')
```

If using Typescript, then events can also be typed strictly:
When using TypeScript the events can be strongly-typed:

```js
// Quasar v2.11.11+
Expand Down
20 changes: 16 additions & 4 deletions ui/types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,23 @@ interface Callbacks {
[key: string]: (...args: any[]) => void;
}

export class EventBus<T extends Callbacks> {
on<K extends keyof T>(event: K, callback: T[K], ctx?: any): this;
once<K extends keyof T>(event: K, callback: T[K], ctx?: any): this;
export class EventBus<T extends Callbacks = Callbacks> {
on<K extends keyof T>(
event: K,
callback: T[K],
...ctx: unknown extends ThisParameterType<T[K]>
? []
: [ctx: ThisParameterType<T[K]>]
): this;
once<K extends keyof T>(
event: K,
callback: T[K],
...ctx: unknown extends ThisParameterType<T[K]>
? []
: [ctx: ThisParameterType<T[K]>]
): this;
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>): this;
off<K extends keyof T>(event: K, callback: T[K]): this;
off<K extends keyof T>(event: K, callback?: T[K]): this;
}

interface CreateMetaMixinContext extends ComponentPublicInstance {
Expand Down

0 comments on commit 940a02f

Please sign in to comment.