diff --git a/dist/index.d.ts b/dist/index.d.ts index ac53781..d5afd59 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -3,18 +3,18 @@ import Hyperswarm from "hyperswarm"; import EventEmitter from "eventemitter2"; export default class HyperswarmWeb extends EventEmitter { private _options; - private _relays; - private _activeRelay; private _discovery; private _queuedEmActions; private _connectionMutex; constructor(opts?: any); - get activeRelay(): Hyperswarm; - init(): Promise; - private ensureConnection; - private isServerAvailable; - connect(pubkey: string, options?: {}): Promise; + private _relays; get relays(): string[]; + private _activeRelay; + get activeRelay(): Hyperswarm; + private _ready; + get ready(): boolean; + init(): Promise; + connect(pubkey: string, options?: {}): Promise; addRelay(pubkey: string): Promise; removeRelay(pubkey: string): boolean; clearRelays(): void; @@ -24,8 +24,6 @@ export default class HyperswarmWeb extends EventEmitter { removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this; emit(eventName: string | symbol, ...args: any[]): boolean; once(eventName: string | symbol, listener: (...args: any[]) => void): this; - private _processOrQueueAction; - private _processQueuedActions; join(topic: Uint8Array, opts?: {}): void; joinPeer(publicKey: Uint8Array): void; leave(topic: Uint8Array): void; @@ -34,5 +32,9 @@ export default class HyperswarmWeb extends EventEmitter { topics(): string[]; flush(): Promise; clear(): Promise; + private ensureConnection; + private isServerAvailable; + private _processOrQueueAction; + private _processQueuedActions; } //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index b37cce2..bc35fc7 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAW5C,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,YAAY,MAAM,eAAe,CAAC;AAGzC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAM;IACt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AO,CAAC,GAAG,CAAC;YAIpB,gBAAgB;YAqEhB,iBAAiB;IAa/B,OAAO,CAAC,qBAAqB;IAS7B,OAAO,CAAC,qBAAqB;CAO9B"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 1dd39f1..24ae171 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11,8 +11,6 @@ import EventEmitter from "eventemitter2"; import { Mutex } from "async-mutex"; export default class HyperswarmWeb extends EventEmitter { _options; - _relays = new Set(); - _activeRelay; _discovery; _queuedEmActions = []; _connectionMutex = new Mutex(); @@ -22,12 +20,82 @@ export default class HyperswarmWeb extends EventEmitter { this._options = opts; this._discovery = createClient(); } + _relays = new Set(); + get relays() { + return [...this._relays.values()]; + } + _activeRelay; get activeRelay() { return this._activeRelay; } + _ready = false; + get ready() { + return this._ready; + } init() { return this.ensureConnection(); } + async connect(pubkey, options = {}) { + if (!this._activeRelay) { + await this.ensureConnection(); + } + return this._activeRelay.connect(pubkey, options); + } + async addRelay(pubkey) { + this._relays.add(pubkey); + } + removeRelay(pubkey) { + if (!this._relays.has(pubkey)) { + return false; + } + this._relays.delete(pubkey); + return true; + } + clearRelays() { + this._relays.clear(); + } + on(eventName, listener) { + return this._processOrQueueAction("on", ...arguments); + } + addListener(eventName, listener) { + return this.on(eventName, listener); + } + off(eventName, listener) { + return this._processOrQueueAction("off", ...arguments); + } + removeListener(eventName, listener) { + return this.off(eventName, listener); + } + emit(eventName, ...args) { + return this._processOrQueueAction("emit", ...arguments); + } + once(eventName, listener) { + return this._processOrQueueAction("once", ...arguments); + } + join(topic, opts = {}) { + return this._processOrQueueAction("join", ...arguments); + } + joinPeer(publicKey) { + return this._processOrQueueAction("joinPeer", ...arguments); + } + leave(topic) { + return this._processOrQueueAction("leave", ...arguments); + } + leavePeer(publicKey) { + return this._processOrQueueAction("leavePeer", ...arguments); + } + status(publicKey) { + return this._activeRelay?.status(publicKey); + } + topics() { + return this._activeRelay?.topics(); + } + async flush() { + return this._activeRelay?.flush(); + } + async clear() { + return this._activeRelay?.clear(); + } async ensureConnection() { const logErr = (await load()).logErr; await this._connectionMutex.waitForUnlock(); @@ -65,6 +133,7 @@ export default class HyperswarmWeb extends EventEmitter { }); this._activeRelay.on("close", () => { this._activeRelay = undefined; + this._ready = false; }); } while (relays.length > 0 && !this._activeRelay); } @@ -75,6 +144,7 @@ export default class HyperswarmWeb extends EventEmitter { this._processQueuedActions(); await this._activeRelay.dht.ready(); this._connectionMutex.release(); + this._ready = true; this.emit("ready"); } async isServerAvailable(connection) { @@ -89,46 +159,6 @@ export default class HyperswarmWeb extends EventEmitter { }); }); } - async connect(pubkey, options = {}) { - if (!this._activeRelay) { - await this.ensureConnection(); - } - return this._activeRelay.connect(pubkey, options); - } - get relays() { - return [...this._relays.values()]; - } - async addRelay(pubkey) { - this._relays.add(pubkey); - } - removeRelay(pubkey) { - if (!this._relays.has(pubkey)) { - return false; - } - this._relays.delete(pubkey); - return true; - } - clearRelays() { - this._relays.clear(); - } - on(eventName, listener) { - return this._processOrQueueAction("on", ...arguments); - } - addListener(eventName, listener) { - return this.on(eventName, listener); - } - off(eventName, listener) { - return this._processOrQueueAction("off", ...arguments); - } - removeListener(eventName, listener) { - return this.off(eventName, listener); - } - emit(eventName, ...args) { - return this._processOrQueueAction("emit", ...arguments); - } - once(eventName, listener) { - return this._processOrQueueAction("once", ...arguments); - } _processOrQueueAction(method, ...args) { if (this._activeRelay) { return this._activeRelay[method](...args); @@ -142,28 +172,4 @@ export default class HyperswarmWeb extends EventEmitter { } this._queuedEmActions = []; } - join(topic, opts = {}) { - return this._processOrQueueAction("join", ...arguments); - } - joinPeer(publicKey) { - return this._processOrQueueAction("joinPeer", ...arguments); - } - leave(topic) { - return this._processOrQueueAction("leave", ...arguments); - } - leavePeer(publicKey) { - return this._processOrQueueAction("leavePeer", ...arguments); - } - status(publicKey) { - return this._activeRelay?.status(publicKey); - } - topics() { - return this._activeRelay?.topics(); - } - async flush() { - return this._activeRelay?.flush(); - } - async clear() { - return this._activeRelay?.clear(); - } }