Skip to content

Commit

Permalink
fix(types): add typings for EventBus
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jan 13, 2025
1 parent e3be739 commit a527b0b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ EventBus.on('signaling-join-room', (payload) => {
store.dispatch('updateLastJoinedConversationToken', token)
})

EventBus.on('signaling-recording-status-changed', (token, status) => {
EventBus.on('signaling-recording-status-changed', ([token, status]) => {
store.dispatch('setConversationProperties', { token, properties: { callRecording: status } })

if (status !== CALL.RECORDING.FAILED) {
Expand Down
13 changes: 5 additions & 8 deletions src/services/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
*/

import mitt from 'mitt'
import type { Emitter, EventType, Handler, WildcardHandler } from 'mitt'

type Events = Record<EventType, unknown>
type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>
type ExtendedEmitter = Emitter<Events> & {
once<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void
once(type: '*', handler: WildcardHandler<Events>): void
_onceHandlers: Map<keyof Events | '*', Map<GenericEventHandler, GenericEventHandler>>
}
import type {
Events,
GenericEventHandler,
ExtendedEmitter,
} from '../types/eventBusTypes.ts'

export const EventBus: ExtendedEmitter = mitt() as ExtendedEmitter

Expand Down
56 changes: 56 additions & 0 deletions src/types/eventBusTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
// import type { components, operations } from './openapi/openapi-full.ts'

import type { Emitter, EventType, Handler, WildcardHandler } from 'mitt'
import type { Route } from 'vue-router'

import type { ChatMessage, Conversation, Participant } from './index.ts'
import type { components } from './openapi/openapi-full.ts'

// List of used events across the app
export type Events = Record<EventType, unknown> & {
'audio-player-ended': number,
'conversations-received': { singleConversation: boolean },
'deleted-session-detected': void,
'duplicate-session-detected': void,
'editing-message': void,
'editing-message-processing': { messageId: number, value: boolean },
'focus-chat-input': void,
'focus-message': number, // TODO: listener method can receive ...[messageId, smooth, highlightAnimation]
'forbidden-route': { error: string },
'joined-conversation': { token: string },
'message-height-changed': { heightDiff: number },
'poll-drafts-open': void,
'poll-editor-open': number,
'refresh-peer-list': void,
'retry-message': number,
'route-change': { from: Route, to: Route },
'scroll-chat-to-bottom': { smooth?: boolean, force?: boolean },
'should-refresh-chat-messages': void,
'should-refresh-conversations': { token: string, properties: Partial<Conversation> } | { all: true } | void,
'signaling-join-call': [string, number],
'signaling-join-call-failed': [string, { meta: components['schemas']['OCSMeta'], data: { error: string } }],
'signaling-join-room': [string],
'signaling-participant-list-changed': void,
'signaling-recording-status-changed': [string, number],
'signaling-servers-updated': { server: string, verify: boolean }[],
'signaling-users-changed': [(Partial<Participant> & ({ sessionId: string } | { nextcloudSessionId: string }))[]],
'signaling-users-in-room': [{ sessionId: string, userId: string }[]],
'smart-picker-open': void,
'switch-to-conversation': { token: string },
'talk:poll-added': { token: string, message: ChatMessage },
'upload-discard': void,
'upload-finished': void,
'upload-start': void,
}

// Extended event for mitt() library
export type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>
export type ExtendedEmitter = Emitter<Events> & {
once<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void
once(type: '*', handler: WildcardHandler<Events>): void
_onceHandlers: Map<keyof Events | '*', Map<GenericEventHandler, GenericEventHandler>>
}
2 changes: 1 addition & 1 deletion src/utils/signaling.js
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ Signaling.Standalone.prototype.processRoomMessageEvent = function(token, data) {
EventBus.emit('should-refresh-chat-messages')
break
case 'recording':
EventBus.emit('signaling-recording-status-changed', token, data.recording.status)
EventBus.emit('signaling-recording-status-changed', [token, data.recording.status])
break
default:
console.error('Unknown room message event', data)
Expand Down

0 comments on commit a527b0b

Please sign in to comment.