Skip to content

Commit

Permalink
Remove empty channels (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Aug 21, 2018
1 parent d27d177 commit f986cba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
10 changes: 9 additions & 1 deletion packages/transport-commons/lib/channels/channel/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Channel {
const { EventEmitter } = require('events');

class Channel extends EventEmitter {
constructor (connections = [], data = null) {
super();

this.connections = connections;
this.data = data;
}
Expand All @@ -21,6 +25,10 @@ class Channel {
}
});

if (this.length === 0) {
this.emit('empty');
}

return this;
}

Expand Down
16 changes: 13 additions & 3 deletions packages/transport-commons/lib/channels/mixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ exports.channelMixin = function channelMixin () {
}

if (names.length === 1) {
const name = names[0];
const [ name ] = names;

if (Array.isArray(name)) {
return this.channel(...name);
}

return this[CHANNELS][name] ||
(this[CHANNELS][name] = new Channel());
if (!this[CHANNELS][name]) {
const channel = new Channel();

channel.once('empty', () => {
channel.removeAllListeners();
delete this[CHANNELS][name];
});

this[CHANNELS][name] = channel;
}

return this[CHANNELS][name];
}

const channels = names.map(name => this.channel(name));
Expand Down
62 changes: 61 additions & 1 deletion packages/transport-commons/test/channels/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ const channels = require('../../lib/channels');
const Channel = require('../../lib/channels/channel/base');
const CombinedChannel = require('../../lib/channels/channel/combined');

const { CHANNELS } = channels.keys;

describe('app.channel', () => {
let app;

beforeEach(() => {
app = feathers().configure(channels());
});

describe('leaf channels', () => {
describe('base channels', () => {
it('creates a new channel, app.channels has names', () => {
assert.ok(app.channel('test') instanceof Channel);
assert.deepEqual(app.channels, ['test']);
Expand Down Expand Up @@ -97,6 +99,64 @@ describe('app.channel', () => {
assert.ok(test !== withData);
assert.deepEqual(withData.data, data);
});

describe('empty channels', () => {
it('is an EventEmitter', () => {
const channel = app.channel('emitchannel');

return new Promise((resolve) => {
channel.once('message', data => {
assert.equal(data, 'hello');
resolve();
});

channel.emit('message', 'hello');
});
});

it('empty', done => {
const channel = app.channel('test');
const c1 = { id: 1 };
const c2 = { id: 2 };

channel.once('empty', done);

channel.join(c1, c2);
channel.leave(c1);
channel.leave(c2);
});

it('removes an empty channel', () => {
const channel = app.channel('test');
const appChannels = app[CHANNELS];
const c1 = { id: 1 };

channel.join(c1);

assert.ok(appChannels.test);
assert.equal(Object.keys(appChannels).length, 1);
channel.leave(c1);

assert.ok(app[CHANNELS].test === undefined);
assert.equal(Object.keys(appChannels).length, 0);
});

it('removes all event listeners from an empty channel', () => {
const channel = app.channel('testing');
const connection = { id: 1 };

channel.on('something', () => {});
assert.equal(channel.listenerCount('something'), 1);
assert.equal(channel.listenerCount('empty'), 1);

channel.join(connection).leave(connection);

assert.ok(app[CHANNELS].testing === undefined);

assert.equal(channel.listenerCount('something'), 0);
assert.equal(channel.listenerCount('empty'), 0);
});
});
});

describe('combined channels', () => {
Expand Down

0 comments on commit f986cba

Please sign in to comment.