From 4f8d2f1355c469ae3f79e536e4be2e6f16101689 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Sat, 15 Apr 2023 16:31:16 +0300 Subject: [PATCH] shuffle some things around --- lib/plugin.js | 76 +++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 69cf7d5..126fdbb 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -147,6 +147,19 @@ exports.init = function initDB(peer, config) { return encryptionFormat } + function populateTangles(tangleIds) { + const tangles = {} + for (const tangleId of tangleIds) { + tangles[tangleId] ??= new Tangle(tangleId, records()) + } + return tangles + } + + function loaded(cb) { + if (cb === void 0) return promisify(loaded)() + scannedLog.onReady(cb) + } + function add(msg, tangleRootHash, cb) { // TODO: optimize this. This may be slow if you're adding many msgs in a // row, because it creates a new Map() each time. @@ -168,21 +181,6 @@ exports.init = function initDB(peer, config) { } } - function getFeedRoot(findWho, findType) { - for (const rec of records()) { - if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash - } - return null - } - - function populateTangles(tangleIds) { - const tangles = {} - for (const tangleId of tangleIds) { - tangles[tangleId] ??= new Tangle(tangleId, records()) - } - return tangles - } - function create(opts, cb) { const keys = opts.keys ?? config.keys @@ -251,6 +249,28 @@ exports.init = function initDB(peer, config) { }) } + function getFeedRoot(findWho, findType) { + for (const rec of records()) { + if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash + } + return null + } + + function getRecord(msgId) { + const isUri = msgId.startsWith('ppppp:') + for (let i = 0; i < recs.length; i++) { + const rec = recs[i] + if (!rec) continue + if (isUri && msgId.endsWith(rec.hash)) return rec + else if (!isUri && rec.hash === msgId) return rec + } + return null + } + + function get(msgId) { + return getRecord(msgId)?.msg + } + function del(msgId, cb) { const rec = getRecord(msgId) const { offset, size, seq } = rec.misc @@ -274,39 +294,19 @@ exports.init = function initDB(peer, config) { } } - function getRecord(msgId) { - const isUri = msgId.startsWith('ppppp:') - for (let i = 0; i < recs.length; i++) { - const rec = recs[i] - if (!rec) continue - if (isUri && msgId.endsWith(rec.hash)) return rec - else if (!isUri && rec.hash === msgId) return rec - } - return null - } - - function get(msgId) { - return getRecord(msgId)?.msg - } - - function loaded(cb) { - if (cb === void 0) return promisify(loaded)() - scannedLog.onReady(cb) - } - return { // public installEncryptionFormat, loaded, add, create, + getFeedRoot, + getRecord, + get, del, onRecordAdded, msgs, records, - getRecord, - get, - getFeedRoot, // internal findEncryptionFormatFor,