diff --git a/lib/utils.js b/lib/ghosts.js similarity index 88% rename from lib/utils.js rename to lib/ghosts.js index 6e2f4b0..e82f816 100644 --- a/lib/utils.js +++ b/lib/ghosts.js @@ -4,6 +4,7 @@ const Path = require('path') const atomic = require('atomic-file-rw') // @ts-ignore const multicb = require('multicb') +const ReadyGate = require('./utils/ready-gate') // TODO: fs is only supported in node.js. We should support browser by replacing // fs.readdir with a browser "file" that just lists all ghost files. @@ -16,34 +17,7 @@ const multicb = require('multicb') * } CB */ -class ReadyGate { - #waiting - #ready - constructor() { - this.#waiting = new Set() - this.#ready = false - } - - /** - * @param {() => void} cb - */ - onReady(cb) { - if (this.#ready) cb() - else this.#waiting.add(cb) - } - - setReady() { - this.#ready = true - for (const cb of this.#waiting) cb() - this.#waiting.clear() - } - - get isReady() { - return this.#ready - } -} - -class GhostDB { +class Ghosts { /** @type {string} */ #basePath @@ -118,7 +92,7 @@ class GhostDB { #read(tangleID, cb) { atomic.readFile( this.#path(tangleID), - GhostDB.encodingOpts, + Ghosts.encodingOpts, (/** @type {any} */ err, /** @type {any} */ str) => { // Load Map /** @type {Map} */ @@ -166,7 +140,7 @@ class GhostDB { atomic.writeFile( this.#path(tangleID), this.#serialize(newMap), - GhostDB.encodingOpts, + Ghosts.encodingOpts, (/** @type {any} */ err) => { // prettier-ignore if (err) return cb(new Error('GhostDB.save() failed to write ghost file', { cause: err })) @@ -189,4 +163,4 @@ class GhostDB { } } -module.exports = { ReadyGate, GhostDB } +module.exports = Ghosts diff --git a/lib/index.js b/lib/index.js index fac4558..fbd65f0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -15,7 +15,8 @@ const { ACCOUNT_SELF, ACCOUNT_ANY, } = require('./msg-v3/constants') -const { ReadyGate, GhostDB } = require('./utils') +const ReadyGate = require('./utils/ready-gate') +const Ghosts = require('./ghosts') const { decrypt } = require('./encryption') /** @@ -162,7 +163,7 @@ function initDB(peer, config) { }, }) - const ghostDB = new GhostDB(Path.join(config.path, 'ghosts')) + const ghosts = new Ghosts(Path.join(config.path, 'ghosts')) peer.close.hook(function (/** @type {any} */ fn, /** @type {any} */ args) { log.close(() => { @@ -327,7 +328,7 @@ function initDB(peer, config) { function loaded(cb) { if (cb === void 0) return promisify(loaded)() scannedLog.onReady(() => { - ghostDB.onReady(cb) + ghosts.onReady(cb) }) } @@ -954,7 +955,7 @@ function initDB(peer, config) { if (!tangleData) return cb(new Error(`ghosts.add() opts.msg "${opts.msg}" does not belong to opts.tangle "${opts.tangle}"`)) const depth = tangleData.depth - ghostDB.save(tangleID, msgID, depth, max, (err) => { + ghosts.save(tangleID, msgID, depth, max, (err) => { // prettier-ignore if (err) cb(new Error('ghosts.add() failed to save to disk', { cause: err })) else cb() @@ -966,8 +967,8 @@ function initDB(peer, config) { * @returns {Array} */ function getGhosts(tangleID) { - const ghosts = ghostDB.read(tangleID) - return [...ghosts.keys()] + const map = ghosts.read(tangleID) + return [...map.keys()] } /** @@ -975,9 +976,9 @@ function initDB(peer, config) { * @returns {number} */ function getMinGhostDepth(tangleID) { - const ghosts = ghostDB.read(tangleID) + const map = ghosts.read(tangleID) let minDepth = Infinity - for (const depth of ghosts.values()) { + for (const depth of map.values()) { if (depth < minDepth) minDepth = depth } return minDepth diff --git a/lib/utils/ready-gate.js b/lib/utils/ready-gate.js new file mode 100644 index 0000000..ece500f --- /dev/null +++ b/lib/utils/ready-gate.js @@ -0,0 +1,28 @@ +class ReadyGate { + #waiting + #ready + constructor() { + this.#waiting = new Set() + this.#ready = false + } + + /** + * @param {() => void} cb + */ + onReady(cb) { + if (this.#ready) cb() + else this.#waiting.add(cb) + } + + setReady() { + this.#ready = true + for (const cb of this.#waiting) cb() + this.#waiting.clear() + } + + get isReady() { + return this.#ready + } +} + +module.exports = ReadyGate