mirror of https://codeberg.org/pzp/pzp-set.git
refactor misc
This commit is contained in:
parent
69f5ce52c3
commit
6980d41207
28
lib/index.js
28
lib/index.js
|
@ -21,7 +21,7 @@ const PREFIX = 'set_v1__'
|
|||
* add: Array<string>,
|
||||
* del: Array<string>,
|
||||
* supersedes: Array<MsgID>,
|
||||
* }} SetData
|
||||
* }} SetMsgData
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -60,8 +60,8 @@ function fromSubdomain(subdomain) {
|
|||
* }} peer
|
||||
* @returns {asserts peer is { db: PPPPPDB, close: ClosableHook }}
|
||||
*/
|
||||
function assertDBExists(peer) {
|
||||
if (!peer.db) throw new Error('record plugin requires ppppp-db plugin')
|
||||
function assertDBPlugin(peer) {
|
||||
if (!peer.db) throw new Error('set plugin requires ppppp-db plugin')
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +82,7 @@ module.exports = {
|
|||
* @param {any} config
|
||||
*/
|
||||
init(peer, config) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
|
||||
//#region state
|
||||
let accountID = /** @type {string | null} */ (null)
|
||||
|
@ -102,10 +102,10 @@ module.exports = {
|
|||
},
|
||||
/**
|
||||
* @param {string} subdomain
|
||||
* @returns {Record<string, Array<MsgID>>}
|
||||
* @returns {{[item in string]: Array<MsgID>}}
|
||||
*/
|
||||
getAll(subdomain) {
|
||||
const out = /** @type {Record<string, Array<MsgID>>} */ ({})
|
||||
const out = /** @type {{[item in string]: Array<MsgID>}} */ ({})
|
||||
for (const [key, value] of this._map.entries()) {
|
||||
if (key.startsWith(subdomain + '/')) {
|
||||
const item = key.slice(subdomain.length + 1)
|
||||
|
@ -177,7 +177,7 @@ module.exports = {
|
|||
/**
|
||||
* @private
|
||||
* @param {Msg | null | undefined} msg
|
||||
* @returns {msg is Msg<SetData>}
|
||||
* @returns {msg is Msg<SetMsgData>}
|
||||
*/
|
||||
function isValidSetMsg(msg) {
|
||||
if (!msg) return false
|
||||
|
@ -195,7 +195,7 @@ module.exports = {
|
|||
* @param {string} subdomain
|
||||
*/
|
||||
function readSet(id, subdomain) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
const domain = fromSubdomain(subdomain)
|
||||
const mootID = MsgV3.getMootID(id, domain)
|
||||
const tangle = peer.db.getTangle(mootID)
|
||||
|
@ -227,7 +227,7 @@ module.exports = {
|
|||
|
||||
/**
|
||||
* @param {string} msgID
|
||||
* @param {Msg<SetData>} msg
|
||||
* @param {Msg<SetMsgData>} msg
|
||||
*/
|
||||
function learnSetUpdate(msgID, msg) {
|
||||
const { account, domain } = msg.metadata
|
||||
|
@ -283,7 +283,7 @@ module.exports = {
|
|||
* @param {string} subdomain
|
||||
*/
|
||||
function _squeezePotential(subdomain) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
if (!accountID) throw new Error('Cannot squeeze potential before loading')
|
||||
// TODO: improve this so that the squeezePotential is the size of the
|
||||
// tangle suffix built as a slice from the fieldRoots
|
||||
|
@ -308,7 +308,7 @@ module.exports = {
|
|||
* @param {CB<void>} cb
|
||||
*/
|
||||
function load(id, cb) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
accountID = id
|
||||
loadPromise = new Promise((resolve, reject) => {
|
||||
for (const rec of peer.db.records()) {
|
||||
|
@ -336,7 +336,7 @@ module.exports = {
|
|||
* @param {CB<boolean>} cb
|
||||
*/
|
||||
function add(id, subdomain, value, cb) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
assert(!!accountID, 'Cannot add to Set before loading')
|
||||
// prettier-ignore
|
||||
if (id !== accountID) return cb(new Error(`Cannot add to another user's Set (${id}/${subdomain})`))
|
||||
|
@ -385,7 +385,7 @@ module.exports = {
|
|||
* @param {CB<boolean>} cb
|
||||
*/
|
||||
function del(id, subdomain, value, cb) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
assert(!!accountID, 'Cannot add to Set before loading')
|
||||
// prettier-ignore
|
||||
if (id !== accountID) return cb(new Error(`Cannot delete from another user's Set (${id}/${subdomain})`))
|
||||
|
@ -453,7 +453,7 @@ module.exports = {
|
|||
* @param {CB<boolean>} cb
|
||||
*/
|
||||
function squeeze(id, subdomain, cb) {
|
||||
assertDBExists(peer)
|
||||
assertDBPlugin(peer)
|
||||
assert(!!accountID, 'Cannot squeeze Set before loading')
|
||||
// prettier-ignore
|
||||
if (id !== accountID) return cb(new Error(`Cannot squeeze another user's Set (${id}/${subdomain})`))
|
||||
|
|
Loading…
Reference in New Issue