autobase-test-helpers
Advanced tools
Comparing version
31
index.js
@@ -29,2 +29,5 @@ const b4a = require('b4a') | ||
return new Promise((resolve, reject) => { | ||
let done = false | ||
let checks = 0 | ||
for (const base of bases) { | ||
@@ -38,11 +41,25 @@ base.on('update', check) | ||
async function check () { | ||
if (!synced(bases)) return | ||
checks++ | ||
if (!(await same())) return mbShutdown() | ||
for (const base of bases) { | ||
await base.update() | ||
} | ||
if (!synced(bases)) return | ||
if (!(await same())) return mbShutdown() | ||
shutdown() | ||
} | ||
function mbShutdown () { | ||
if (done) return shutdown() | ||
checks-- | ||
} | ||
async function same () { | ||
return synced(bases) && await sameHash(bases) | ||
} | ||
function shutdown (err) { | ||
checks-- | ||
done = true | ||
if (checks !== 0 && !err) return | ||
for (const base of bases) { | ||
@@ -59,2 +76,12 @@ base.off('update', check) | ||
async function sameHash (bases) { | ||
const first = bases[0] | ||
const h1 = await first.hash() | ||
for (let i = 1; i < bases.length; i++) { | ||
const h2 = await bases[i].hash() | ||
if (!b4a.equals(h1, h2)) return false | ||
} | ||
return true | ||
} | ||
function synced (bases) { | ||
@@ -61,0 +88,0 @@ const first = bases[0] |
{ | ||
"name": "autobase-test-helpers", | ||
"version": "2.1.3", | ||
"version": "3.0.0", | ||
"description": "Helpers when writing tests for an Autobased application", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
15580
3.76%115
23.66%