Skip to content

Commit f9d8536

Browse files
vonagamdaffl
authored andcommitted
fix: Improve transport-commons types (#1396)
1 parent 28888a1 commit f9d8536

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

packages/transport-commons/src/channels/index.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import Debug from 'debug';
2-
import { get, compact, flattenDeep, noop } from 'lodash';
2+
import { compact, flattenDeep, noop } from 'lodash';
33
import { Channel } from './channel/base';
44
import { CombinedChannel } from './channel/combined';
5-
import { channelMixin, publishMixin, keys } from './mixins';
5+
import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins';
66
import { Application, Service } from '@feathersjs/feathers';
77

88
const debug = Debug('@feathersjs/transport-commons/channels');
9-
const { CHANNELS, PUBLISHERS, ALL_EVENTS } = keys;
9+
const { CHANNELS } = keys;
1010

1111
declare module '@feathersjs/feathers' {
1212
interface ServiceAddons<T> {
13-
publish (callback: (data: T, hook: HookContext<T>) => Channel): this;
14-
publish (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
13+
publish (publisher: Publisher<T>): this;
14+
publish (event: Event, publisher: Publisher<T>): this;
1515

16-
registerPublisher (callback: (data: T, hook: HookContext<T>) => Channel): this;
17-
registerPublisher (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
16+
registerPublisher (publisher: Publisher<T>): this;
17+
registerPublisher (event: Event, publisher: Publisher<T>): this;
1818
}
1919

2020
interface Application<ServiceTypes> {
@@ -23,11 +23,11 @@ declare module '@feathersjs/feathers' {
2323
channel (name: string[]): Channel;
2424
channel (...names: string[]): Channel;
2525

26-
publish<T> (callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
27-
publish<T> (event: string, callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
26+
publish<T> (publisher: Publisher<T>): this;
27+
publish<T> (event: Event, publisher: Publisher<T>): this;
2828

29-
registerPublisher<T> (callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
30-
registerPublisher<T> (event: string, callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
29+
registerPublisher<T> (publisher: Publisher<T>): this;
30+
registerPublisher<T> (event: Event, publisher: Publisher<T>): this;
3131
}
3232
}
3333

@@ -63,22 +63,24 @@ export function channels () {
6363

6464
debug('Publishing event', event, hook.path);
6565

66-
const servicePublishers = (service as any)[PUBLISHERS];
67-
const appPublishers = (app as any)[PUBLISHERS];
66+
const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS];
67+
const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS];
6868
// This will return the first publisher list that is not empty
6969
// In the following precedence
70-
const callback = [
70+
const publisher = (
7171
// 1. Service publisher for a specific event
72-
get(servicePublishers, event),
72+
servicePublishers[event] ||
7373
// 2. Service publisher for all events
74-
get(servicePublishers, ALL_EVENTS),
75-
// 3. App publishers for a specific event
76-
get(appPublishers, event),
77-
// 4. App publishers for all events
78-
get(appPublishers, ALL_EVENTS)
79-
].find(current => typeof current === 'function') || noop;
80-
81-
Promise.resolve(callback(data, hook)).then(result => {
74+
servicePublishers[keys.ALL_EVENTS] ||
75+
// 3. App publisher for a specific event
76+
appPublishers[event] ||
77+
// 4. App publisher for all events
78+
appPublishers[keys.ALL_EVENTS] ||
79+
// 5. No publisher
80+
noop
81+
);
82+
83+
Promise.resolve(publisher(data, hook)).then(result => {
8284
if (!result) {
8385
return;
8486
}

packages/transport-commons/src/channels/mixins.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const CHANNELS = Symbol('@feathersjs/transport-commons/channels');
99
const ALL_EVENTS = Symbol('@feathersjs/transport-commons/all-events');
1010

1111
export const keys = {
12-
PUBLISHERS,
13-
CHANNELS,
14-
ALL_EVENTS
12+
PUBLISHERS: PUBLISHERS as typeof PUBLISHERS,
13+
CHANNELS: CHANNELS as typeof CHANNELS,
14+
ALL_EVENTS: ALL_EVENTS as typeof ALL_EVENTS,
1515
};
1616

1717
export interface ChannelMixin {
@@ -60,10 +60,14 @@ export function channelMixin () {
6060
return mixin;
6161
}
6262

63-
export interface PublishMixin {
64-
[PUBLISHERS]: { [key: string]: Channel };
65-
publish (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any;
66-
registerPublisher (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any;
63+
export type Event = string|(typeof ALL_EVENTS);
64+
65+
export type Publisher<T = any> = (data: T, hook: HookContext<T>) => Channel | Channel[] | void | Promise<Channel | Channel[] | void>;
66+
67+
export interface PublishMixin<T = any> {
68+
[PUBLISHERS]: { [ALL_EVENTS]?: Publisher<T>, [key: string]: Publisher<T> };
69+
publish (event: Event, publisher: Publisher<T>): this;
70+
registerPublisher (event: Event, publisher: Publisher<T>): this;
6771
}
6872

6973
export function publishMixin () {
@@ -74,11 +78,11 @@ export function publishMixin () {
7478
return this.registerPublisher(...args);
7579
},
7680

77-
registerPublisher (event, callback) {
81+
registerPublisher (event, publisher) {
7882
debug('Registering publisher', event);
7983

80-
if (!callback && typeof event === 'function') {
81-
callback = event;
84+
if (!publisher && typeof event === 'function') {
85+
publisher = event;
8286
event = ALL_EVENTS;
8387
}
8488

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

9094
const publishers = this[PUBLISHERS];
9195

92-
// @ts-ignore
93-
publishers[event] = callback;
96+
publishers[event] = publisher;
9497

9598
return this;
9699
}

0 commit comments

Comments
 (0)