Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit e357f8d

Browse files
committed
Better handling of sub-apps and sockets (#18)
* adding some commments and debug statements * Make socket provider property configurable
1 parent dbb038d commit e357f8d

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/events.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export function getDispatcher(service, ev, data, hook) {
4141
return function(connection) {
4242
let promise = promisify(originalDispatcher, service, data, connection);
4343

44-
if(eventFilters.length) {
44+
if (eventFilters.length) {
4545
debug(`Dispatching ${eventFilters.length} event filters for '${ev}' event`);
4646
eventFilters.forEach(filterFn => {
47-
if(filterFn.length === 4) { // function(data, connection, hook, callback)
47+
if (filterFn.length === 4) { // function(data, connection, hook, callback)
4848
promise = promise.then(data =>
4949
promisify(filterFn, service, data, connection, hook)
5050
);
@@ -82,6 +82,7 @@ export function setupEventHandlers(info, path, service) {
8282
dispatcher(info.params(socket))
8383
.then(data => {
8484
if(data) {
85+
debug(`Dispatching ${eventName} with data`, data);
8586
send(eventName, data);
8687
} else {
8788
debug(`Not sending any data for ${eventName}`);

src/index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function socketMixin(service) {
1111

1212
service.mixin({
1313
setup(app, path) {
14-
if(!this._socketSetup) {
14+
if (!this._socketSetup) {
1515
const info = app._socketInfo;
16-
const mountpath = (app.mountpath !== '/' && typeof app.mountpath === 'string') ?
17-
app.mountpath : '';
16+
const isSubApp = app.mountpath !== '/' && typeof app.mountpath === 'string';
17+
const mountpath = isSubApp ? app.mountpath : '';
1818
const fullPath = stripSlashes(`${mountpath}/${path}`);
1919
const setupSocket = socket => {
2020
setupMethodHandlers.call(app, info, socket, fullPath, this);
@@ -41,22 +41,26 @@ function socketMixin(service) {
4141
});
4242
}
4343

44-
export default function mixin() {
45-
const app = this;
44+
export default function createMixin(property) {
45+
return function mixin() {
46+
const app = this;
4647

47-
app.mixins.push(socketMixin);
48-
app.mixins.push(filterMixin);
48+
app.mixins.push(socketMixin);
49+
app.mixins.push(filterMixin);
4950

50-
// When mounted as a sub-app, override the parent setup so you don't have to call it
51-
app.on('mount', parent => {
52-
const oldSetup = parent.setup;
51+
// When mounted as a sub-app, override the parent setup to call our
52+
// own setup so the developer doesn't need to call it explicitly.
53+
app.on('mount', parent => {
54+
const oldSetup = parent.setup;
5355

54-
parent.setup = function(... args) {
55-
const result = oldSetup.apply(this, args);
56-
app.setup(... args);
57-
return result;
58-
};
59-
});
56+
parent.setup = function(... args) {
57+
const result = oldSetup.apply(this, args);
58+
app[property] = parent[property];
59+
app.setup(... args);
60+
return result;
61+
};
62+
});
63+
};
6064
}
6165

62-
mixin.socketMixin = socketMixin;
66+
createMixin.socketMixin = socketMixin;

src/methods.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function setupMethodHandlers(info, socket, path, service) {
2323
let position = typeof paramsPositions[method] !== 'undefined' ?
2424
paramsPositions[method] : 1;
2525

26+
debug(`Setting up socket listener for event '${name}'`);
27+
2628
socket.on(name, function () {
2729
debug(`Got '${name}' event with connection`, connection);
2830

0 commit comments

Comments
 (0)