fix: Improve transport-commons types (#1396)

This commit is contained in:
Dmitrii Maganov 2019-06-17 22:20:03 +03:00 committed by David Luecke
parent 28888a1a6b
commit f9d8536c40
2 changed files with 39 additions and 34 deletions

View File

@ -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<T> {
publish (callback: (data: T, hook: HookContext<T>) => Channel): this;
publish (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
publish (publisher: Publisher<T>): this;
publish (event: Event, publisher: Publisher<T>): this;
registerPublisher (callback: (data: T, hook: HookContext<T>) => Channel): this;
registerPublisher (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
registerPublisher (publisher: Publisher<T>): this;
registerPublisher (event: Event, publisher: Publisher<T>): this;
}
interface Application<ServiceTypes> {
@ -23,11 +23,11 @@ declare module '@feathersjs/feathers' {
channel (name: string[]): Channel;
channel (...names: string[]): Channel;
publish<T> (callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
publish<T> (event: string, callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
publish<T> (publisher: Publisher<T>): this;
publish<T> (event: Event, publisher: Publisher<T>): this;
registerPublisher<T> (callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
registerPublisher<T> (event: string, callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
registerPublisher<T> (publisher: Publisher<T>): this;
registerPublisher<T> (event: Event, publisher: Publisher<T>): 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;
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(callback(data, hook)).then(result => {
Promise.resolve(publisher(data, hook)).then(result => {
if (!result) {
return;
}

View File

@ -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<T = any> = (data: T, hook: HookContext<T>) => Channel | Channel[] | void | Promise<Channel | Channel[] | void>;
export interface PublishMixin<T = any> {
[PUBLISHERS]: { [ALL_EVENTS]?: Publisher<T>, [key: string]: Publisher<T> };
publish (event: Event, publisher: Publisher<T>): this;
registerPublisher (event: Event, publisher: Publisher<T>): 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;
}