refactor to move files around

This commit is contained in:
Andre Staltz 2023-10-25 18:39:57 +03:00
parent 21c1adbd2a
commit 778dbda588
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 42 additions and 39 deletions

View File

@ -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<string, number>} */
@ -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

View File

@ -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<string>}
*/
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

28
lib/utils/ready-gate.js Normal file
View File

@ -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