UNPKG

@ledgerhq/hw-transport-http

Version:
74 lines 3.07 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import HttpTransport from "./HttpTransport"; import WebSocketTransport from "./WebSocketTransport"; import Transport from "@ledgerhq/hw-transport"; const getTransport = url => (!url.startsWith("ws") ? HttpTransport : WebSocketTransport); const inferURLs = (urls) => __awaiter(void 0, void 0, void 0, function* () { const r = yield (typeof urls === "function" ? urls() : urls); return typeof r === "string" ? [r] : r; }); export default (urls) => { class StaticTransport extends Transport { } StaticTransport.isSupported = HttpTransport.isSupported; StaticTransport.list = () => inferURLs(urls) .then(urls => Promise.all(urls.map(url => getTransport(url) .check(url) .then(() => [url]) .catch(() => [])))) .then(arrs => arrs.reduce((acc, a) => acc.concat(a), [])); StaticTransport.listen = (observer) => { let unsubscribed = false; const seen = {}; function checkLoop() { if (unsubscribed) return; inferURLs(urls) .then(urls => Promise.all(urls.map((url) => __awaiter(this, void 0, void 0, function* () { if (unsubscribed) return; try { yield getTransport(url).check(url); if (unsubscribed) return; if (!seen[url]) { seen[url] = 1; observer.next({ type: "add", descriptor: url, }); } } catch (e) { // nothing if (seen[url]) { delete seen[url]; observer.next({ type: "remove", descriptor: url, }); } } })))) .then(() => new Promise(success => setTimeout(success, 5000))) .then(checkLoop); } checkLoop(); return { unsubscribe: () => { unsubscribed = true; }, }; }; StaticTransport.open = url => getTransport(url).open(url); return StaticTransport; }; //# sourceMappingURL=withStaticURLs.js.map