Manage expired channels

This commit is contained in:
yflory
2018-02-13 18:20:13 +01:00
parent 8f7489576f
commit 728a6a868d
13 changed files with 120 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ define([], function () {
var onLeave = conf.onLeave;
var onReady = conf.onReady;
var onDisconnect = conf.onDisconnect;
var onError = conf.onError;
var owners = conf.owners;
var password = conf.password;
var expire = conf.expire;
@@ -44,6 +45,17 @@ define([], function () {
var messageFromOuter = function () {};
var error = function (err, wc) {
if (onError) {
onError({
type: err,
loaded: !initializing
});
if (wc && (err === "EEXPIRED" || err === "EDELETED")) { wc.leave(); }
}
else { console.error(err); }
};
var onRdy = function (padData) {
// Trigger onReady only if not ready yet. This is important because the history keeper sends a direct
// message through "network" when it is synced, and it triggers onReady for each channel joined.
@@ -96,11 +108,17 @@ define([], function () {
if (peer === hk) {
// if the peer is the 'history keeper', extract their message
var parsed1 = JSON.parse(msg);
// First check if it is an error message (EXPIRED/DELETED)
if (parsed1.channel === wc.id && parsed1.error) {
return void error(parsed1.error, wc);
}
msg = parsed1[4];
// Check that this is a message for our channel
if (parsed1[3] !== wc.id) { return; }
}
lastKnownHash = msg.slice(0,64);
var message = msgIn(peer, msg);
@@ -177,7 +195,12 @@ define([], function () {
};
var msg = ['GET_HISTORY', wc.id, cfg];
// Add the validateKey if we are the channel creator and we have a validateKey
if (hk) { network.sendto(hk, JSON.stringify(msg)); }
if (hk) {
network.sendto(hk, JSON.stringify(msg)).then(function () {
}, function (err) {
console.error(err);
});
}
} else {
onRdy();
}
@@ -204,8 +227,8 @@ define([], function () {
// join the netflux network, promise to handle opening of the channel
network.join(channel || null).then(function(wc) {
onOpen(wc, network, firstConnection);
}, function(error) {
console.error(error);
}, function(err) {
console.error(err);
});
};