1
1
import Debug from 'debug' ;
2
- import { get , compact , flattenDeep , noop } from 'lodash' ;
2
+ import { compact , flattenDeep , noop } from 'lodash' ;
3
3
import { Channel } from './channel/base' ;
4
4
import { CombinedChannel } from './channel/combined' ;
5
- import { channelMixin , publishMixin , keys } from './mixins' ;
5
+ import { channelMixin , publishMixin , keys , PublishMixin , Event , Publisher } from './mixins' ;
6
6
import { Application , Service } from '@feathersjs/feathers' ;
7
7
8
8
const debug = Debug ( '@feathersjs/transport-commons/channels' ) ;
9
- const { CHANNELS , PUBLISHERS , ALL_EVENTS } = keys ;
9
+ const { CHANNELS } = keys ;
10
10
11
11
declare module '@feathersjs/feathers' {
12
12
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;
15
15
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;
18
18
}
19
19
20
20
interface Application < ServiceTypes > {
@@ -23,11 +23,11 @@ declare module '@feathersjs/feathers' {
23
23
channel ( name : string [ ] ) : Channel ;
24
24
channel ( ...names : string [ ] ) : Channel ;
25
25
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 ;
28
28
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 ;
31
31
}
32
32
}
33
33
@@ -63,22 +63,24 @@ export function channels () {
63
63
64
64
debug ( 'Publishing event' , event , hook . path ) ;
65
65
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 ] ;
68
68
// This will return the first publisher list that is not empty
69
69
// In the following precedence
70
- const callback = [
70
+ const publisher = (
71
71
// 1. Service publisher for a specific event
72
- get ( servicePublishers , event ) ,
72
+ servicePublishers [ event ] ||
73
73
// 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 => {
82
84
if ( ! result ) {
83
85
return ;
84
86
}
0 commit comments