Skip to content

Commit

Permalink
fix: Improve transport-commons types (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored and daffl committed Jun 17, 2019
1 parent 28888a1 commit f9d8536
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
48 changes: 25 additions & 23 deletions packages/transport-commons/src/channels/index.ts
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 15 additions & 12 deletions packages/transport-commons/src/channels/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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;
}

Expand All @@ -89,8 +93,7 @@ export function publishMixin () {

const publishers = this[PUBLISHERS];

// @ts-ignore
publishers[event] = callback;
publishers[event] = publisher;

return this;
}
Expand Down

0 comments on commit f9d8536

Please # to comment.