From 317079a87d3743768784e9cf6916a5b8d68350f2 Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Sat, 5 May 2018 21:04:15 -0700 Subject: [PATCH] Move from transports= to paused = in URLS (transport= is still supported) --- API.md | 2 ++ Transport.js | 2 +- Transports.js | 28 +++++++++++++++++++++------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index bce629d..76cfcbd 100644 --- a/API.md +++ b/API.md @@ -271,6 +271,8 @@ The Transports Class manages multiple transports _transports List of transports loaded (internal) namingcb If set will be called cb(urls) => urls to convert to urls from names. _transportclasses All classes whose code is loaded e.g. {HTTP: TransportHTTP, IPFS: TransportIPFS} +_optionspaused Saves paused option for setup + ``` #####static _connected() diff --git a/Transport.js b/Transport.js index 8db86c3..11b298c 100644 --- a/Transport.js +++ b/Transport.js @@ -347,5 +347,5 @@ Transport.STATUS_CONNECTED = 0; // Connected - all other numbers are some versio 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 +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 exports = module.exports = Transport; diff --git a/Transports.js b/Transports.js index fe66d01..922e6e1 100644 --- a/Transports.js +++ b/Transports.js @@ -2,12 +2,19 @@ const Url = require('url'); const errors = require('./Errors'); const utils = require('./utils'); -/* -Handles multiple transports, API should be (almost) the same as for an individual transport) - */ class Transports { + /* + Handles multiple transports, API should be (almost) the same as for an individual transport) + + Fields: + _transports List of transports loaded (internal) + namingcb If set will be called cb(urls) => urls to convert to urls from names. + _transportclasses All classes whose code is loaded e.g. {HTTP: TransportHTTP, IPFS: TransportIPFS} + _optionspaused Saves paused option for setup + */ + constructor(options, verbose) { if (verbose) console.log("Transports(%o)",options); } @@ -483,12 +490,16 @@ class Transports { static async p_setup1(verbose, cb) { /* Second stage of setup, connect if possible */ // Does all setup1a before setup1b since 1b can rely on ones with 1a, e.g. YJS relies on IPFS - await Promise.all(this._transports.map((t) => t.p_setup1(verbose, cb))) + await Promise.all(this._transports + .filter((t) => (! this._optionspaused.includes(t.name))) + .map((t) => t.p_setup1(verbose, cb))) } static async p_setup2(verbose, cb) { /* Second stage of setup, connect if possible */ // Does all setup1a before setup1b since 1b can rely on ones with 1a, e.g. YJS relies on IPFS - await Promise.all(this._transports.map((t) => t.p_setup2(verbose, cb))) + await Promise.all(this._transports + .filter((t) => (! this._optionspaused.includes(t.name))) + .map((t) => t.p_setup2(verbose, cb))) } static async refreshstatus(t) { @@ -504,21 +515,24 @@ class Transports { } } - static async p_connect(options, verbose) { + static async p_connect(options, verbose) { //TODO-API paused /* This is a standardish starting process, feel free to subclass or replace ! It will connect to a set of standard transports and is intended to work inside a browser. - options = { defaulttransports: ["IPFS"], statuselement: el, http: {}, ipfs: {} } + options = { defaulttransports: ["IPFS"], statuselement: el, http: {}, ipfs: {}; paused: ["IPFS"] } */ if (verbose) console.group("p_connect ---"); try { options = options || {}; let setupoptions = {}; let tabbrevs = options.transports; // Array of transport abbreviations + let tpaused = options.paused; // Array of transports paused if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] } if (! tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT"]; } tabbrevs = tabbrevs.map(n => n.toUpperCase()); + tpaused = tpaused.map(n => n.toUpperCase()); // In case its input by user instead of saved state in history let transports = this.setup0(tabbrevs, options, verbose); + this._optionspaused = options.paused; if (options.statuscb) { this.statuscb = options.statuscb; }