Fixed some promisify patterns and added p_stop.

This commit is contained in:
Mitra Ardron 2019-01-07 09:03:55 +11:00
parent fa865b94c3
commit dac9d1b7df
3 changed files with 33 additions and 4 deletions

View File

@ -53,6 +53,16 @@ class Transport {
return t.p_setup2(cb); // And connect
}
/* Disconnect from the transport service - there is no guarrantee that a restart will be successfull so this is usually only for when exiting */
p_stop(refreshstatus) {
return new Promise((resolve, reject) => {
this.status = Transport.STATUS_FAILED;
if (refreshstatus) refreshstatus(this);
//if (err) { reject(err) } else {
resolve(res);
//}
});
}
togglePaused(cb) {
/*
Switch the state of the transport between STATUS_CONNECTED and STATUS_PAUSED,
@ -370,4 +380,5 @@ Transport.STATUS_FAILED = 1; // Failed to connect
Transport.STATUS_STARTING = 2; // In the process of connecting
Transport.STATUS_LOADED = 3; // Code loaded, but haven't tried to connect. (this is typically hard coded in subclasses constructor)
Transport.STATUS_PAUSED = 4; // It was launched, probably connected, but now paused so will be ignored by validFor // Note this is copied to dweb-archive/Nav.js so check if change
Transport.STATUSTEXT = ["Connected", "Failed", "Starting", "Loaded", "Paused"];
exports = module.exports = Transport;

View File

@ -162,6 +162,14 @@ class TransportIPFS extends Transport {
return this;
}
p_stop(refreshstatus) {
return new Promise((resolve, reject) =>
this.ipfs.stop((err, res) => {
this.status = Transport.STATUS_FAILED;
if (refreshstatus) refreshstatus(this);
if (err) { reject(err); } else { resolve(res); }
}));
}
async p_status() {
/*
Return a numeric code for the status of a transport.

View File

@ -599,7 +599,7 @@ class Transports {
debugtransports("Connection stage 1 to %s", t.name);
return t.p_setup1(refreshstatus);
}))
if (cb) { prom.reject((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
}
static p_setup2(refreshstatus, cb) {
/* Second stage of setup, connect if possible */
@ -611,7 +611,17 @@ class Transports {
debugtransports("Connection stage 2 to %s", t.name);
return t.p_setup2(refreshstatus);
}));
if (cb) { prom.reject((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
}
static p_stop(refreshstatus, cb) {
/* Disconnect from all services, may not be able to reconnect */
const prom = Promise.all(this._connected()
.map((t) => {
debugtransports("Stopping %s", t.name);
return t.p_stop(refreshstatus);
}));
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
}
static async refreshstatus(t) {
@ -628,8 +638,8 @@ class Transports {
}
static connect(options, cb) {
const prom = p_connect(options);
if (cb) { prom.reject((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
const prom = this.p_connect(options);
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
}
static async p_connect(options) {
/*