fix subscriptions (#196)

This commit is contained in:
Guilherme Gazzo 2018-01-16 16:32:30 -02:00 committed by GitHub
parent 885acf9575
commit a6b525b09e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -46,7 +46,7 @@ export default class Socket extends EventEmitter {
this._connect(); this._connect();
this.ddp = new EventEmitter(); this.ddp = new EventEmitter();
this.on('ping', () => this.send({ msg: 'pong' })); this.on('ping', () => this.send({ msg: 'pong' }));
this.on('result', data => this.ddp.emit(data.id, { result: data.result, error: data.error })); this.on('result', data => this.ddp.emit(data.id, { id: data.id, result: data.result, error: data.error }));
this.on('ready', data => this.ddp.emit(data.subs[0], data)); this.on('ready', data => this.ddp.emit(data.subs[0], data));
} }
send(obj) { send(obj) {
@ -54,7 +54,7 @@ export default class Socket extends EventEmitter {
this.id += 1; this.id += 1;
const id = obj.id || `${ this.id }`; const id = obj.id || `${ this.id }`;
this.connection.send(EJSON.stringify({ ...obj, id })); this.connection.send(EJSON.stringify({ ...obj, id }));
this.ddp.once(id, data => (data.error ? reject(data.error) : resolve(data.result || data.subs))); this.ddp.once(id, data => (data.error ? reject(data.error) : resolve({ id, ...data })));
}); });
} }
_connect() { _connect() {
@ -99,22 +99,22 @@ export default class Socket extends EventEmitter {
call(method, ...params) { call(method, ...params) {
return this.send({ return this.send({
msg: 'method', method, params msg: 'method', method, params
}); }).then(data => data.result || data.subs);
} }
unsubscribe(id) { unsubscribe(id) {
if (!this.subscriptions[id]) { if (!this.subscriptions[id]) {
return Promise.reject(); return Promise.reject(id);
} }
delete this.subscriptions[id]; delete this.subscriptions[id];
return this.send({ return this.send({
msg: 'unsub', msg: 'unsub',
id id
}); }).then(data => data.result || data.subs);
} }
subscribe(name, ...params) { subscribe(name, ...params) {
return this.send({ return this.send({
msg: 'sub', name, params msg: 'sub', name, params
}).then((id) => { }).then(({ id }) => {
const args = { const args = {
name, name,
params, params,