Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4707416

Browse filesBrowse files
committed
refactored event-bus
1 parent 863e257 commit 4707416
Copy full SHA for 4707416

File tree

Expand file treeCollapse file tree

5 files changed

+25
-80
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+25
-80
lines changed

‎src/App.vue

Copy file name to clipboardExpand all lines: src/App.vue
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { AppNavbar } from '@/common'
1515
import { ref } from 'vue'
1616
import { useNotifications } from '@/composables'
1717
import { config } from '@config'
18-
import { Bus, ErrorHandler } from '@/helpers'
18+
import { bus, BUS_EVENTS, ErrorHandler } from '@/helpers'
1919
import { NotificationPayload } from '@/types'
2020
2121
const isAppInitialized = ref(false)
@@ -34,16 +34,16 @@ const init = async () => {
3434
}
3535
3636
const initNotifications = () => {
37-
Bus.on(Bus.eventList.success, payload =>
37+
bus.on(BUS_EVENTS.success, payload =>
3838
showToast('success', payload as NotificationPayload),
3939
)
40-
Bus.on(Bus.eventList.warning, payload =>
40+
bus.on(BUS_EVENTS.warning, payload =>
4141
showToast('warning', payload as NotificationPayload),
4242
)
43-
Bus.on(Bus.eventList.error, payload =>
43+
bus.on(BUS_EVENTS.error, payload =>
4444
showToast('error', payload as NotificationPayload),
4545
)
46-
Bus.on(Bus.eventList.info, payload =>
46+
bus.on(BUS_EVENTS.info, payload =>
4747
showToast('info', payload as NotificationPayload),
4848
)
4949
}

‎src/forms/LoginForm.vue

Copy file name to clipboardExpand all lines: src/forms/LoginForm.vue
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { AppButton } from '@/common'
3131
import { InputField } from '@/fields'
3232
3333
import { reactive } from 'vue'
34-
import { Bus, ErrorHandler, email, required } from '@/helpers'
34+
import { bus, ErrorHandler, email, required, BUS_EVENTS } from '@/helpers'
3535
import { useI18n } from 'vue-i18n'
3636
import { useForm, useFormValidation } from '@/composables'
3737
@@ -56,7 +56,7 @@ const submit = async () => {
5656
5757
disableForm()
5858
try {
59-
Bus.success(t('login-form.login-success-msg'))
59+
bus.emit(BUS_EVENTS.success, t('login-form.login-success-msg'))
6060
} catch (error) {
6161
ErrorHandler.process(error)
6262
}

‎src/helpers/error-handler.ts

Copy file name to clipboardExpand all lines: src/helpers/error-handler.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import log from 'loglevel'
2-
import { Bus } from '@/helpers'
2+
import { bus, BUS_EVENTS } from '@/helpers'
33
import { i18n } from '@/localization'
44

55
export class ErrorHandler {
66
static process(error: Error | unknown, errorMessage = ''): void {
77
const msgTranslation = errorMessage || ErrorHandler._getErrorMessage(error)
8-
Bus.error(msgTranslation)
8+
bus.emit(BUS_EVENTS.error, msgTranslation)
99

1010
ErrorHandler.processWithoutFeedback(error)
1111
}

‎src/helpers/event-bus.ts

Copy file name to clipboard
+7-62Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,17 @@
11
import { EventEmitter } from '@distributedlab/tools'
22

3-
import { NotificationPayload } from '@/types'
4-
5-
enum EVENTS {
3+
export enum BUS_EVENTS {
64
error = 'error',
75
warning = 'warning',
86
success = 'success',
97
info = 'info',
108
}
119

12-
const isFunctionsEqual = (
13-
a: (...params: unknown[]) => unknown,
14-
b: (...params: unknown[]) => unknown,
15-
) => {
16-
return (
17-
a.toString().replaceAll(' ', '').replaceAll('\n', '') ===
18-
b.toString().replaceAll(' ', '').replaceAll('\n', '')
19-
)
20-
}
21-
22-
export class EventBus {
23-
readonly #emitter: EventEmitter<Record<EVENTS, unknown>>
24-
25-
constructor() {
26-
this.#emitter = new EventEmitter<Record<EVENTS, unknown>>()
27-
}
28-
29-
public get eventList(): Readonly<typeof EVENTS> {
30-
return EVENTS
31-
}
32-
33-
on(eventName: EVENTS, handlerFn: (payload?: unknown) => void): void {
34-
if (
35-
this.#emitter.handlers[EVENTS.success]?.find(el => {
36-
return isFunctionsEqual(el, payload => {
37-
handlerFn(payload as NotificationPayload)
38-
})
39-
})
40-
) {
41-
return
42-
}
43-
44-
this.#emitter.on(eventName, handlerFn)
45-
}
46-
47-
emit(eventName: EVENTS, payload: NotificationPayload): void {
48-
this.#emitter.emit(eventName, payload)
49-
}
50-
51-
off(eventName: EVENTS, handlerFn: (payload: unknown) => void): void {
52-
this.#emitter.off(eventName, handlerFn)
53-
}
54-
55-
success(payload: NotificationPayload): void {
56-
this.#emitter.emit(EVENTS.success, payload)
57-
}
58-
59-
error(payload: NotificationPayload): void {
60-
this.#emitter.emit(EVENTS.error, payload)
61-
}
62-
63-
warning(payload: NotificationPayload): void {
64-
this.#emitter.emit(EVENTS.warning, payload)
65-
}
66-
67-
info(payload: NotificationPayload): void {
68-
this.#emitter.emit(EVENTS.info, payload)
69-
}
10+
export type DefaultBusEventMap = {
11+
[BUS_EVENTS.success]: unknown
12+
[BUS_EVENTS.error]: unknown
13+
[BUS_EVENTS.warning]: unknown
14+
[BUS_EVENTS.info]: unknown
7015
}
7116

72-
export const Bus = new EventBus()
17+
export const bus = new EventEmitter<DefaultBusEventMap>()

‎src/pages/UiKitPage.vue

Copy file name to clipboardExpand all lines: src/pages/UiKitPage.vue
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,27 @@
258258
<div class="ui-kit-page__buttons">
259259
<app-button
260260
size="small"
261-
:text="'Bus.success'"
261+
:text="'bus.success'"
262262
color="success"
263-
@click="() => Bus.success('Some success message')"
263+
@click="() => bus.emit(BUS_EVENTS.success, 'Some success message')"
264264
/>
265265
<app-button
266266
size="small"
267-
:text="'Bus.error'"
267+
:text="'bus.error'"
268268
color="error"
269-
@click="() => Bus.error('Some error message')"
269+
@click="() => bus.emit(BUS_EVENTS.error, 'Some error message')"
270270
/>
271271
<app-button
272272
size="small"
273-
:text="'Bus.warning'"
273+
:text="'bus.warning'"
274274
color="warning"
275-
@click="() => Bus.warning('Some warning message')"
275+
@click="() => bus.emit(BUS_EVENTS.warning, 'Some warning message')"
276276
/>
277277
<app-button
278278
size="small"
279-
:text="'Bus.info'"
279+
:text="'bus.info'"
280280
color="info"
281-
@click="() => Bus.info('Some info message')"
281+
@click="() => bus.emit(BUS_EVENTS.info, 'Some info message')"
282282
/>
283283

284284
<app-button
@@ -511,7 +511,7 @@ import {
511511
import LoginForm from '@/forms/LoginForm.vue'
512512
513513
import { reactive, ref } from 'vue'
514-
import { Bus, ErrorHandler } from '@/helpers'
514+
import { bus, BUS_EVENTS, ErrorHandler } from '@/helpers'
515515
516516
const isModalShown = ref(false)
517517
const isCollapseShown = ref(false)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.