mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 15:44:45 -04:00
feat: add patch to add generic types to eventemitter2
EventEmitter2 has types, but they're very basic and not very type safe. I created this patch, because my improved types haven't been merged into the official package. Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
11c2f57e4b
commit
caa53e3556
16 changed files with 445 additions and 22 deletions
|
@ -3,6 +3,7 @@
|
|||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { EventMap } from 'eventemitter2';
|
||||
|
||||
export const eventModuleConfig = {
|
||||
wildcard: false,
|
||||
|
@ -18,3 +19,7 @@ export enum NoteEvent {
|
|||
PERMISSION_CHANGE = 'note.permission_change' /** noteId: The id of the [@link Note], which permissions are changed. **/,
|
||||
DELETION = 'note.deletion' /** noteId: The id of the [@link Note], which is being deleted. **/,
|
||||
}
|
||||
|
||||
export interface NoteEventMap extends EventMap {
|
||||
[NoteEvent.PERMISSION_CHANGE]: (noteId: number) => void;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import {
|
|||
MaximumDocumentLengthExceededError,
|
||||
NotInDBError,
|
||||
} from '../errors/errors';
|
||||
import { NoteEvent } from '../events';
|
||||
import { NoteEvent, NoteEventMap } from '../events';
|
||||
import { Group } from '../groups/group.entity';
|
||||
import { GroupsService } from '../groups/groups.service';
|
||||
import { HistoryEntry } from '../history/history-entry.entity';
|
||||
|
@ -54,7 +54,7 @@ export class NotesService {
|
|||
@Inject(forwardRef(() => AliasService)) private aliasService: AliasService,
|
||||
private realtimeNoteService: RealtimeNoteService,
|
||||
private realtimeNoteStore: RealtimeNoteStore,
|
||||
private eventEmitter: EventEmitter2,
|
||||
private eventEmitter: EventEmitter2<NoteEventMap>,
|
||||
) {
|
||||
this.logger.setContext(NotesService.name);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import {
|
|||
} from '../config/guest_access.enum';
|
||||
import noteConfiguration, { NoteConfig } from '../config/note.config';
|
||||
import { PermissionsUpdateInconsistentError } from '../errors/errors';
|
||||
import { NoteEvent } from '../events';
|
||||
import { NoteEvent, NoteEventMap } from '../events';
|
||||
import { Group } from '../groups/group.entity';
|
||||
import { GroupsService } from '../groups/groups.service';
|
||||
import { SpecialGroup } from '../groups/groups.special';
|
||||
|
@ -36,7 +36,7 @@ export class PermissionsService {
|
|||
private readonly logger: ConsoleLoggerService,
|
||||
@Inject(noteConfiguration.KEY)
|
||||
private noteConfig: NoteConfig,
|
||||
private eventEmitter: EventEmitter2,
|
||||
private eventEmitter: EventEmitter2<NoteEventMap>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
encodeMetadataUpdatedMessage,
|
||||
} from '@hedgedoc/commons';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { EventEmitter2 } from 'eventemitter2';
|
||||
import { EventEmitter2, EventMap } from 'eventemitter2';
|
||||
import { Awareness } from 'y-protocols/awareness';
|
||||
|
||||
import { Note } from '../../notes/note.entity';
|
||||
|
@ -16,10 +16,15 @@ import { WebsocketAwareness } from './websocket-awareness';
|
|||
import { WebsocketConnection } from './websocket-connection';
|
||||
import { WebsocketDoc } from './websocket-doc';
|
||||
|
||||
export interface MapType extends EventMap {
|
||||
destroy: () => void;
|
||||
beforeDestroy: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a note currently being edited by a number of clients.
|
||||
*/
|
||||
export class RealtimeNote extends EventEmitter2 {
|
||||
export class RealtimeNote extends EventEmitter2<MapType> {
|
||||
protected logger: Logger;
|
||||
private readonly websocketDoc: WebsocketDoc;
|
||||
private readonly websocketAwareness: WebsocketAwareness;
|
||||
|
|
|
@ -7,13 +7,13 @@ import { EventEmitter2 } from 'eventemitter2';
|
|||
import { Mock } from 'ts-mockery';
|
||||
|
||||
import { Note } from '../../../notes/note.entity';
|
||||
import { RealtimeNote } from '../realtime-note';
|
||||
import { MapType, RealtimeNote } from '../realtime-note';
|
||||
import { WebsocketAwareness } from '../websocket-awareness';
|
||||
import { WebsocketDoc } from '../websocket-doc';
|
||||
import { mockAwareness } from './mock-awareness';
|
||||
import { mockWebsocketDoc } from './mock-websocket-doc';
|
||||
|
||||
class MockRealtimeNote extends EventEmitter2 {
|
||||
class MockRealtimeNote extends EventEmitter2<MapType> {
|
||||
constructor(
|
||||
private note: Note,
|
||||
private doc: WebsocketDoc,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue