mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-27 16:32:30 +00:00
Refactor Pubsub
This commit is contained in:
parent
19edecc25c
commit
1a0b93d133
@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const io = require('socket.io-client');
|
const io = require('socket.io-client');
|
||||||
const List = require('./list/OrbitList');
|
|
||||||
|
|
||||||
class Pubsub {
|
class Pubsub {
|
||||||
constructor(ipfs) {
|
constructor(ipfs) {
|
||||||
@ -16,22 +15,12 @@ class Pubsub {
|
|||||||
this._socket = io.connect(`http://${host}:${port}`, { 'forceNew': true });
|
this._socket = io.connect(`http://${host}:${port}`, { 'forceNew': true });
|
||||||
this._socket.on('connect', resolve);
|
this._socket.on('connect', resolve);
|
||||||
this._socket.on('connect_error', (err) => reject(new Error(`Connection refused to ${host}:${port}`)));
|
this._socket.on('connect_error', (err) => reject(new Error(`Connection refused to ${host}:${port}`)));
|
||||||
|
this._socket.on('disconnect', (socket) => console.log(`Disconnected from http://${host}:${port}`));
|
||||||
// TODO: cleanup
|
this._socket.on('error', (e) => console.log('Pubsub socket error:', e));
|
||||||
this._socket.on('disconnect', (socket) => {
|
|
||||||
// console.log(`Disconnected from http://${host}:${port}`)
|
|
||||||
});
|
|
||||||
|
|
||||||
this._socket.on('error', (e) => console.log('error:', e));
|
|
||||||
this._socket.on('message', this._handleMessage.bind(this));
|
this._socket.on('message', this._handleMessage.bind(this));
|
||||||
this._socket.on('latest', (hash, message) => {
|
this._socket.on('latest', (hash, message) => {
|
||||||
console.log(">", hash, message);
|
console.log(">", hash, message);
|
||||||
if(this._subscriptions[hash]) {
|
this._handleMessage(hash, message);
|
||||||
this._subscriptions[hash].head = message;
|
|
||||||
|
|
||||||
if(this._subscriptions[hash].onLatest)
|
|
||||||
this._subscriptions[hash].onLatest(hash, message);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -41,9 +30,9 @@ class Pubsub {
|
|||||||
this._socket.disconnect();
|
this._socket.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(hash, password, callback, onLatest) {
|
subscribe(hash, password, callback) {
|
||||||
if(!this._subscriptions[hash]) {
|
if(!this._subscriptions[hash]) {
|
||||||
this._subscriptions[hash] = { head: null, callback: callback, onLatest: onLatest };
|
this._subscriptions[hash] = { head: null, callback: callback };
|
||||||
this._socket.emit('subscribe', { channel: hash });
|
this._socket.emit('subscribe', { channel: hash });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user