fix subscriptions (#196)
This commit is contained in:
parent
885acf9575
commit
a6b525b09e
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue