From f9d8536c400caba355ccc8a2cdf171dcd9cff555 Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Mon, 17 Jun 2019 22:20:03 +0300 Subject: [PATCH] fix: Improve transport-commons types (#1396) --- .../transport-commons/src/channels/index.ts | 48 ++++++++++--------- .../transport-commons/src/channels/mixins.ts | 27 ++++++----- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/packages/transport-commons/src/channels/index.ts b/packages/transport-commons/src/channels/index.ts index 8ef4dacf00..c2cf57a4bd 100644 --- a/packages/transport-commons/src/channels/index.ts +++ b/packages/transport-commons/src/channels/index.ts @@ -1,20 +1,20 @@ import Debug from 'debug'; -import { get, compact, flattenDeep, noop } from 'lodash'; +import { compact, flattenDeep, noop } from 'lodash'; import { Channel } from './channel/base'; import { CombinedChannel } from './channel/combined'; -import { channelMixin, publishMixin, keys } from './mixins'; +import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins'; import { Application, Service } from '@feathersjs/feathers'; const debug = Debug('@feathersjs/transport-commons/channels'); -const { CHANNELS, PUBLISHERS, ALL_EVENTS } = keys; +const { CHANNELS } = keys; declare module '@feathersjs/feathers' { interface ServiceAddons { - publish (callback: (data: T, hook: HookContext) => Channel): this; - publish (event: string, callback: (data: T, hook: HookContext) => Channel): this; + publish (publisher: Publisher): this; + publish (event: Event, publisher: Publisher): this; - registerPublisher (callback: (data: T, hook: HookContext) => Channel): this; - registerPublisher (event: string, callback: (data: T, hook: HookContext) => Channel): this; + registerPublisher (publisher: Publisher): this; + registerPublisher (event: Event, publisher: Publisher): this; } interface Application { @@ -23,11 +23,11 @@ declare module '@feathersjs/feathers' { channel (name: string[]): Channel; channel (...names: string[]): Channel; - publish (callback: (data: T, hook: HookContext) => Channel | Channel[] | void): Application; - publish (event: string, callback: (data: T, hook: HookContext) => Channel | Channel[] | void): Application; + publish (publisher: Publisher): this; + publish (event: Event, publisher: Publisher): this; - registerPublisher (callback: (data: T, hook: HookContext) => Channel | Channel[] | void): Application; - registerPublisher (event: string, callback: (data: T, hook: HookContext) => Channel | Channel[] | void): Application; + registerPublisher (publisher: Publisher): this; + registerPublisher (event: Event, publisher: Publisher): this; } } @@ -63,22 +63,24 @@ export function channels () { debug('Publishing event', event, hook.path); - const servicePublishers = (service as any)[PUBLISHERS]; - const appPublishers = (app as any)[PUBLISHERS]; + const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS]; + const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS]; // This will return the first publisher list that is not empty // In the following precedence - const callback = [ + const publisher = ( // 1. Service publisher for a specific event - get(servicePublishers, event), + servicePublishers[event] || // 2. Service publisher for all events - get(servicePublishers, ALL_EVENTS), - // 3. App publishers for a specific event - get(appPublishers, event), - // 4. App publishers for all events - get(appPublishers, ALL_EVENTS) - ].find(current => typeof current === 'function') || noop; - - Promise.resolve(callback(data, hook)).then(result => { + servicePublishers[keys.ALL_EVENTS] || + // 3. App publisher for a specific event + appPublishers[event] || + // 4. App publisher for all events + appPublishers[keys.ALL_EVENTS] || + // 5. No publisher + noop + ); + + Promise.resolve(publisher(data, hook)).then(result => { if (!result) { return; } diff --git a/packages/transport-commons/src/channels/mixins.ts b/packages/transport-commons/src/channels/mixins.ts index 3b0c9af2ed..b4101cf38a 100644 --- a/packages/transport-commons/src/channels/mixins.ts +++ b/packages/transport-commons/src/channels/mixins.ts @@ -9,9 +9,9 @@ const CHANNELS = Symbol('@feathersjs/transport-commons/channels'); const ALL_EVENTS = Symbol('@feathersjs/transport-commons/all-events'); export const keys = { - PUBLISHERS, - CHANNELS, - ALL_EVENTS + PUBLISHERS: PUBLISHERS as typeof PUBLISHERS, + CHANNELS: CHANNELS as typeof CHANNELS, + ALL_EVENTS: ALL_EVENTS as typeof ALL_EVENTS, }; export interface ChannelMixin { @@ -60,10 +60,14 @@ export function channelMixin () { return mixin; } -export interface PublishMixin { - [PUBLISHERS]: { [key: string]: Channel }; - publish (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any; - registerPublisher (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any; +export type Event = string|(typeof ALL_EVENTS); + +export type Publisher = (data: T, hook: HookContext) => Channel | Channel[] | void | Promise; + +export interface PublishMixin { + [PUBLISHERS]: { [ALL_EVENTS]?: Publisher, [key: string]: Publisher }; + publish (event: Event, publisher: Publisher): this; + registerPublisher (event: Event, publisher: Publisher): this; } export function publishMixin () { @@ -74,11 +78,11 @@ export function publishMixin () { return this.registerPublisher(...args); }, - registerPublisher (event, callback) { + registerPublisher (event, publisher) { debug('Registering publisher', event); - if (!callback && typeof event === 'function') { - callback = event; + if (!publisher && typeof event === 'function') { + publisher = event; event = ALL_EVENTS; } @@ -89,8 +93,7 @@ export function publishMixin () { const publishers = this[PUBLISHERS]; - // @ts-ignore - publishers[event] = callback; + publishers[event] = publisher; return this; }