Skip to content

Commit 98fe8f8

Browse files
authored
fix: Add registerPublisher alias for .publish (#1302)
1 parent ebce79b commit 98fe8f8

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const { CHANNELS, PUBLISHERS, ALL_EVENTS } = keys;
1111
declare module '@feathersjs/feathers' {
1212
interface ServiceAddons<T> {
1313
publish (callback: (data: T, hook: HookContext<T>) => Channel): this;
14-
1514
publish (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
15+
16+
registerPublisher (callback: (data: T, hook: HookContext<T>) => Channel): this;
17+
registerPublisher (event: string, callback: (data: T, hook: HookContext<T>) => Channel): this;
1618
}
1719

1820
interface Application<ServiceTypes> {
@@ -21,11 +23,11 @@ declare module '@feathersjs/feathers' {
2123
channel (name: string[]): Channel;
2224
channel (...names: string[]): Channel;
2325

24-
// tslint:disable-next-line void-return
2526
publish<T> (callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
26-
27-
// tslint:disable-next-line void-return
2827
publish<T> (event: string, callback: (data: T, hook: HookContext<T>) => Channel | Channel[] | void): Application<ServiceTypes>;
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>;
2931
}
3032
}
3133

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,18 @@ export function channelMixin () {
6363
export interface PublishMixin {
6464
[PUBLISHERS]: { [key: string]: Channel };
6565
publish (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any;
66+
registerPublisher (event: string|symbol, callback: (data: any, hook: HookContext) => Channel): any;
6667
}
6768

6869
export function publishMixin () {
6970
const result: PublishMixin = {
7071
[PUBLISHERS]: {},
7172

72-
publish (event, callback) {
73+
publish (...args) {
74+
return this.registerPublisher(...args);
75+
},
76+
77+
registerPublisher (event, callback) {
7378
debug('Registering publisher', event);
7479

7580
if (!callback && typeof event === 'function') {

packages/transport-commons/test/channels/dispatch.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe('app.publish', () => {
1919
}
2020
});
2121

22-
app.service('test').publish('created', function () {});
23-
app.service('test').publish('bla', function () {});
22+
app.service('test').registerPublisher('created', function () {});
23+
app.service('test').registerPublisher('bla', function () {});
2424
assert.ok(false, 'Should never get here');
2525
} catch (e) {
2626
assert.strictEqual(e.message, `'bla' is not a valid service event`);
@@ -45,7 +45,7 @@ describe('app.publish', () => {
4545
it('simple event registration and dispatching', done => {
4646
app.channel('testing').join(c1);
4747

48-
app.service('test').publish('created', () => app.channel('testing'));
48+
app.service('test').registerPublisher('created', () => app.channel('testing'));
4949

5050
app.once('publish', (event: string, channel: Channel, hook: HookContext) => {
5151
assert.strictEqual(event, 'created');
@@ -65,8 +65,8 @@ describe('app.publish', () => {
6565
app.channel('testing').join(c1);
6666
app.channel('other').join(c2);
6767

68-
app.publish('created', () => app.channel('testing'));
69-
app.publish(() => app.channel('other'));
68+
app.registerPublisher('created', () => app.channel('testing'));
69+
app.registerPublisher(() => app.channel('other'));
7070

7171
app.once('publish', (_event: string, channel: Channel) => {
7272
assert.ok(channel.connections.indexOf(c1) !== -1);
@@ -82,12 +82,12 @@ describe('app.publish', () => {
8282
app.channel('testing').join(c1);
8383
app.channel('othertest').join(c2);
8484

85-
app.service('test').publish('created', () =>
85+
app.service('test').registerPublisher('created', () =>
8686
new Promise(resolve =>
8787
setTimeout(() => resolve(app.channel('testing')), 50)
8888
)
8989
);
90-
app.service('test').publish('created', () =>
90+
app.service('test').registerPublisher('created', () =>
9191
new Promise(resolve =>
9292
setTimeout(() => resolve(app.channel('testing', 'othertest')), 100)
9393
)
@@ -110,7 +110,7 @@ describe('app.publish', () => {
110110
app.channel('testing').join(c1);
111111
app.channel('othertest').join(c2);
112112

113-
app.service('test').publish('foo', () => app.channel('testing'));
113+
app.service('test').registerPublisher('foo', () => app.channel('testing'));
114114

115115
app.once('publish', (event: string, channel: Channel, hook: HookContext) => {
116116
assert.strictEqual(event, 'foo');
@@ -141,7 +141,7 @@ describe('app.publish', () => {
141141
});
142142

143143
it('does not send `dispatch` event if there are no connections', done => {
144-
app.service('test').publish('created', () =>
144+
app.service('test').registerPublisher('created', () =>
145145
app.channel('dummy')
146146
);
147147
app.once('publish', () =>
@@ -158,7 +158,7 @@ describe('app.publish', () => {
158158
app.channel('testing').join(c1);
159159
app.channel('othertest').join(c2);
160160

161-
app.service('test').publish('created', () => [
161+
app.service('test').registerPublisher('created', () => [
162162
app.channel('testing'),
163163
app.channel('othertest')
164164
]);
@@ -180,7 +180,7 @@ describe('app.publish', () => {
180180
app.channel('testing').join(c1);
181181
app.channel('othertest').join(c2);
182182

183-
app.service('test').publish('created', () => [
183+
app.service('test').registerPublisher('created', () => [
184184
app.channel('testing').send(c1data),
185185
app.channel('othertest')
186186
]);
@@ -199,8 +199,8 @@ describe('app.publish', () => {
199199
it('publisher precedence and preventing publishing', done => {
200200
app.channel('test').join(c1);
201201

202-
app.publish(() => app.channel('test'));
203-
app.service('test').publish('created', (): null => null);
202+
app.registerPublisher(() => app.channel('test'));
203+
app.service('test').registerPublisher('created', (): null => null);
204204

205205
app.once('publish', () => done(new Error('Should never get here')));
206206

@@ -213,7 +213,7 @@ describe('app.publish', () => {
213213
app.channel('testing').join(c1);
214214
app.channel('othertest').join(c1);
215215

216-
app.service('test').publish('created', () => {
216+
app.service('test').registerPublisher('created', () => {
217217
return [
218218
app.channel('testing'),
219219
app.channel('othertest').send(sendData)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ app.use(express.errorHandler());
136136
// Add any new real-time connection to the `everybody` channel
137137
app.on('connection', connection => app.channel('everybody').join(connection));
138138
// Publish all events to the `everybody` channel
139-
app.publish(data => app.channel('everybody'));
139+
app.registerPublisher(data => app.channel('everybody'));
140140

141141
// Start the server
142142
app.listen(3030).on('listening', () =>

0 commit comments

Comments
 (0)