mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-25 06:42:17 +00:00
Fixed some promisify patterns and added p_stop.
This commit is contained in:
parent
fa865b94c3
commit
dac9d1b7df
11
Transport.js
11
Transport.js
@ -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;
|
||||
|
@ -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.
|
||||
|
@ -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) {
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user