Skip to content

Commit 1c67632

Browse files
author
Piotr
committed
global event bus and some helper methods
1 parent 8e7953b commit 1c67632

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/components/icon/Filter.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<svg height="14" viewBox="0 0 1792 1792" width="14" xmlns="http://www.w3.org/2000/svg" fill="none">
3+
<path
4+
d="M1595 295q17 41-14 70l-493 493v742q0 42-39 59-13 5-25 5-27 0-45-19l-256-256q-19-19-19-45v-486l-493-493q-31-29-14-70 17-39 59-39h1280q42 0 59 39z"
5+
fill="currentColor" />
6+
</svg>
7+
</template>

src/demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function getLayout(json: boolean = true): Layout {
7777
return {
7878
text: line.json_content.level,
7979
facets: [
80-
{ name:"Level", value:line.json_content.level }
80+
{ name:"Custom facet", value:line.json_content.level.substr(0,3) }
8181
]
8282
}
8383
}`

src/event_bus.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// eventBus.ts
2+
import { reactive } from 'vue'
3+
4+
type EventCallback = (...args: any[]) => void
5+
type EventDefinition = Record<string, any[]>
6+
7+
// Generic event bus creator function - similar to defineEmits pattern
8+
export function defineEventBus<T extends EventDefinition>(events: T = {} as T) {
9+
// Type for event callbacks mapped to their parameter types
10+
type EventCallbacks = {
11+
[K in keyof T]: (...args: T[K]) => void
12+
}
13+
14+
// Type-safe event listeners storage
15+
const listeners: {
16+
[K in keyof T]?: EventCallbacks[K][]
17+
} = {}
18+
19+
const bus = reactive({
20+
// Register event listener with type checking
21+
on<K extends keyof T>(event: K, callback: EventCallbacks[K]) {
22+
if (!listeners[event]) {
23+
listeners[event] = []
24+
}
25+
listeners[event]!.push(callback)
26+
return () => bus.off(event, callback)
27+
},
28+
29+
// Remove event listener
30+
off<K extends keyof T>(event: K, callback?: EventCallbacks[K]) {
31+
if (!listeners[event]) return
32+
if (!callback) {
33+
delete listeners[event]
34+
return
35+
}
36+
listeners[event] = listeners[event]!.filter(cb => cb !== callback)
37+
},
38+
39+
// Emit event with type-checked arguments
40+
emit<K extends keyof T>(event: K, ...args: T[K]) {
41+
if (!listeners[event]) return
42+
listeners[event]!.forEach(callback => callback(...args))
43+
}
44+
})
45+
46+
return bus
47+
}
48+
49+
// Usage example: create a typed event bus
50+
export type GlobalEvents = {
51+
'searchbar-update': [value: string]
52+
}
53+
54+
// Create a global instance
55+
export const globalEventBus = defineEventBus<GlobalEvents>()
56+
57+
58+
// In component A.vue
59+
// Usage example
60+
// import { globalEventBus } from './eventBus'
61+
62+
// function userLogin() {
63+
// // Type checking ensures correct parameters
64+
// globalEventBus.emit('user:login', 'john_doe', Date.now())
65+
66+
// // This would cause a TypeScript error - wrong parameter types
67+
// // globalEventBus.emit('user:login', 123, 'wrong')
68+
// }
69+
70+
// // In component B.vue
71+
// import { globalEventBus } from './eventBus'
72+
// import { onMounted, onUnmounted } from 'vue'
73+
74+
// const loginHandler = (username: string, timestamp: number) => {
75+
// console.log(`${username} logged in at ${new Date(timestamp).toLocaleString()}`)
76+
// }
77+
78+
// onMounted(() => {
79+
// // Store the unsubscribe function
80+
// const unsubscribe = globalEventBus.on('user:login', loginHandler)
81+
82+
// // Alternative cleanup approach
83+
// onUnmounted(() => {
84+
// unsubscribe()
85+
// // Or: globalEventBus.off('user:login', loginHandler)
86+
// })
87+
// })

src/store.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ export const useMainStore = defineStore("main", () => {
427427
return initSettings.value?.updateVersion
428428
}
429429

430+
const isFacetActive = (name: string) => {
431+
for (let f in facets.value) {
432+
if (facets.value[f].name !== name) {
433+
continue
434+
}
435+
return facets.value[f].items.find(f => f.selected == true) != undefined
436+
}
437+
return false
438+
}
439+
430440
const notificationBar = computed(() => {
431441

432442
return false // for until its finished
@@ -509,6 +519,7 @@ export const useMainStore = defineStore("main", () => {
509519
displayRows,
510520

511521
facets,
522+
isFacetActive,
512523
searchbar,
513524
searchbarValid,
514525
searchClear,

0 commit comments

Comments
 (0)