Skip to content

Commit

Permalink
fixed issue #1 - resubscribe to events
Browse files Browse the repository at this point in the history
  • Loading branch information
jhenson29 committed Nov 25, 2018
1 parent 7bfbebc commit 206f2a7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soct",
"version": "1.0.2",
"version": "1.0.3",
"description": "Proxy classes over socket.io.",
"main": "./src/index.js",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,8 @@
},
"homepage": "https://github.com/jhenson29/node-soct#readme",
"dependencies": {
"socket.io": "^2.1.1"
"socket.io": "^2.1.1",
"uuid": "^3.3.2"
},
"devDependencies": {
"coveralls": "^3.0.2",
Expand Down
11 changes: 9 additions & 2 deletions src/soct-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ class SocktService{
this.state = {
port,
io: require('socket.io')(),
listeners: {},
};

const { listeners } = this.state;
this.state.io.on('connection', socket => {
socket.on('__sockt_register_event_listener__', prop => {
service.on(prop, (...args) => socket.emit(prop, [...args]));
listeners[socket.id] = [];
socket.emit('__soct_new_connection__');
socket.on('__soct_register_event_listener__', ({ name, id }) => {
if(!listeners[socket.id].includes(id)){
listeners[socket.id].push(id);
service.on(name, (...args) => socket.emit(name, [...args]));
}
});
Object.getOwnPropertyNames(Object.getPrototypeOf(service)).forEach( prop => {
socket.on(prop, async (args, cb) => {
Expand Down
74 changes: 61 additions & 13 deletions src/soct/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const _socket = Symbol('socket');
const uuid = require('uuid/v4');

const _state = Symbol('state');
const _mapClass = Symbol('mapClass');
const _registerListenOnConnection = Symbol('registerListenOnConnection');
const _emit = Symbol('emit');
const _registerListener = Symbol('registerListener');

const OPTIONDEFAULT = {
host: 'http://localhost',
Expand All @@ -11,9 +16,9 @@ const OPTIONDEFAULT = {
class Soct{
/**
*
* @param {object function} service // class or function to wrap
* @param {object} service // class to wrap
* @param {number} port // port number for service
* @param {string} host // host connection string for service
* @param {object} options // options
*/
constructor(
service,
Expand All @@ -22,7 +27,39 @@ class Soct{
host = OPTIONDEFAULT.host,
} = OPTIONDEFAULT
){
this[_socket] = require('socket.io-client')(`${host}:${port}`);
this[_state] = {
socket: require('socket.io-client')(`${host}:${port}`),
eventListeners: []
};

this[_mapClass](service);
this[_registerListenOnConnection]();
}

/**
* add an event listener
* @public
* @param {string} name
* @param {function} func
*/
on(name, func){
const { socket, eventListeners } = this[_state];
const listener = {
name,
id: uuid()
};
eventListeners.push(listener);
this[_registerListener](listener);
socket.on(name, args => func(args));
}

/**
* map the class to this instance of soct
* @param {object} service
*/
[_mapClass](
service
){
const testObj = new service();
Object.getOwnPropertyNames(service.prototype).forEach( prop => {

Expand All @@ -45,14 +82,13 @@ class Soct{
}

/**
* add an event listener
* @public
* @param {string} name
* @param {function} func
*/
on(name, func){
this[_socket].emit('__sockt_register_event_listener__',name);
this[_socket].on(name, args => func(args));
* register 'listen on connection'
*/
[_registerListenOnConnection](){
const { socket } = this[_state];
socket.on('__soct_new_connection__', () => {
this[_state].eventListeners.forEach( listener => this[_registerListener](listener));
});
}

/**
Expand All @@ -65,7 +101,19 @@ class Soct{
prop,
args
){
return new Promise( resolve => this[_socket].emit(prop, args, cb => resolve(cb)));
const { socket } = this[_state];
return new Promise( resolve => socket.emit(prop, args, cb => resolve(cb)));
}

/**
* register listener with socket
* @param {string} name
*/
[_registerListener](
listener
){
const { socket } = this[_state];
socket.emit('__soct_register_event_listener__', listener);
}
}

Expand Down

0 comments on commit 206f2a7

Please sign in to comment.