'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var nanoid = require('nanoid');
var delay = require('delay');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var delay__default = /*#__PURE__*/_interopDefaultLegacy(delay);

async function waitForPeers(ipfs, topic, peersToWait, waitForMs) {
  const start = Date.now();
  while (true) {
    const peers = await ipfs.pubsub.peers(topic);
    const everyPeerFound = peersToWait.every(p => peers.includes(p));
    if (everyPeerFound) {
      return;
    }
    if (Date.now() > start + waitForMs) {
      throw new Error(`Timed out waiting for peers to be subscribed to "${ topic }"`);
    }
    await delay__default["default"](10);
  }
}
function getTopic() {
  return 'pubsub-tests-' + nanoid.nanoid();
}

exports.getTopic = getTopic;
exports.waitForPeers = waitForPeers;
