add semantic type MsgID in lib/index.js

This commit is contained in:
Andre Staltz 2023-10-18 14:43:24 +03:00
parent 31ec544522
commit 39d48ac416
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 17 additions and 16 deletions

View File

@ -22,6 +22,7 @@ const { decrypt } = require('./encryption')
* @typedef {import('ppppp-keypair').Keypair} Keypair * @typedef {import('ppppp-keypair').Keypair} Keypair
* @typedef {import('ppppp-keypair').KeypairPublicSlice} KeypairPublicSlice * @typedef {import('ppppp-keypair').KeypairPublicSlice} KeypairPublicSlice
* @typedef {import('ppppp-keypair').KeypairPrivateSlice} KeypairPrivateSlice * @typedef {import('ppppp-keypair').KeypairPrivateSlice} KeypairPrivateSlice
* @typedef {string} MsgID
* @typedef {import('./msg-v3').Msg} Msg * @typedef {import('./msg-v3').Msg} Msg
* @typedef {import('./msg-v3').AccountData} AccountData * @typedef {import('./msg-v3').AccountData} AccountData
* @typedef {import('./msg-v3').AccountPower} AccountPower * @typedef {import('./msg-v3').AccountPower} AccountPower
@ -44,7 +45,7 @@ const { decrypt } = require('./encryption')
* }} RecDeleted * }} RecDeleted
* *
* @typedef {{ * @typedef {{
* id: string; * id: MsgID;
* msg: Msg; * msg: Msg;
* received: number; * received: number;
* misc: { * misc: {
@ -70,7 +71,7 @@ const { decrypt } = require('./encryption')
class DBTangle extends MsgV3.Tangle { class DBTangle extends MsgV3.Tangle {
/** /**
* @param {string} rootID * @param {MsgID} rootID
* @param {Iterable<Rec>} recordsIter * @param {Iterable<Rec>} recordsIter
*/ */
constructor(rootID, recordsIter) { constructor(rootID, recordsIter) {
@ -90,8 +91,8 @@ class DBTangle extends MsgV3.Tangle {
* *
* *Erasables* are msgs that precede `msgsIDs` and can be erased without * *Erasables* are msgs that precede `msgsIDs` and can be erased without
* losing a validation path toward the root. * losing a validation path toward the root.
* @param {Array<string>} msgIDs * @param {Array<MsgID>} msgIDs
* @returns {{ deletables: Set<string>, erasables: Set<string> }} * @returns {{ deletables: Set<MsgID>, erasables: Set<MsgID> }}
*/ */
getDeletablesAndErasables(...msgIDs) { getDeletablesAndErasables(...msgIDs) {
// Determine erasables // Determine erasables
@ -204,7 +205,7 @@ function initDB(peer, config) {
}) })
/** /**
* @param {string} id * @param {MsgID} id
* @param {Msg} msg * @param {Msg} msg
* @param {CB<RecPresent>} cb * @param {CB<RecPresent>} cb
*/ */
@ -264,10 +265,10 @@ function initDB(peer, config) {
} }
/** /**
* @param {Array<string>} tangleIDs * @param {Array<MsgID>} tangleIDs
*/ */
function populateTangles(tangleIDs) { function populateTangles(tangleIDs) {
/** @type {Record<string, DBTangle>} */ /** @type {Record<MsgID, DBTangle>} */
const tangles = {} const tangles = {}
for (const tangleID of tangleIDs) { for (const tangleID of tangleIDs) {
tangles[tangleID] ??= new DBTangle(tangleID, records()) tangles[tangleID] ??= new DBTangle(tangleID, records())
@ -332,7 +333,7 @@ function initDB(peer, config) {
* locally known. * locally known.
* *
* @param {Pick<RecPresent, 'id' | 'msg'>} rec * @param {Pick<RecPresent, 'id' | 'msg'>} rec
* @param {string} tangleID * @param {MsgID} tangleID
* @returns {Error | null} * @returns {Error | null}
*/ */
function verifyRec(rec, tangleID) { function verifyRec(rec, tangleID) {
@ -387,7 +388,7 @@ function initDB(peer, config) {
/** /**
* @param {Msg} msg * @param {Msg} msg
* @param {string} tangleID * @param {MsgID} tangleID
* @param {CB<RecPresent>} cb * @param {CB<RecPresent>} cb
*/ */
function add(msg, tangleID, cb) { function add(msg, tangleID, cb) {
@ -438,7 +439,7 @@ function initDB(peer, config) {
/** /**
* @param {{ keypair?: Keypair; account: string; domain: string; }} opts * @param {{ keypair?: Keypair; account: string; domain: string; }} opts
* @param {CB<string>} cb * @param {CB<MsgID>} cb
*/ */
function initializeFeed(opts, cb) { function initializeFeed(opts, cb) {
const keypair = opts.keypair ?? config.keypair const keypair = opts.keypair ?? config.keypair
@ -777,7 +778,7 @@ function initDB(peer, config) {
* data: any; * data: any;
* domain: string; * domain: string;
* account: string; * account: string;
* tangles?: Array<string>; * tangles?: Array<MsgID>;
* }} opts * }} opts
* @param {CB<RecPresent>} cb * @param {CB<RecPresent>} cb
*/ */
@ -890,7 +891,7 @@ function initDB(peer, config) {
} }
/** /**
* @param {string} msgID * @param {MsgID} msgID
* @returns {RecPresent | null} * @returns {RecPresent | null}
*/ */
function getRecord(msgID) { function getRecord(msgID) {
@ -907,14 +908,14 @@ function initDB(peer, config) {
} }
/** /**
* @param {string} msgID * @param {MsgID} msgID
*/ */
function get(msgID) { function get(msgID) {
return getRecord(msgID)?.msg return getRecord(msgID)?.msg
} }
/** /**
* @param {string} msgID * @param {MsgID} msgID
* @param {CB<void>} cb * @param {CB<void>} cb
*/ */
function del(msgID, cb) { function del(msgID, cb) {
@ -929,7 +930,7 @@ function initDB(peer, config) {
} }
/** /**
* @param {string} msgID * @param {MsgID} msgID
* @param {CB<void>} cb * @param {CB<void>} cb
*/ */
function erase(msgID, cb) { function erase(msgID, cb) {
@ -943,7 +944,7 @@ function initDB(peer, config) {
} }
/** /**
* @param {string} tangleID * @param {MsgID} tangleID
* @returns {DBTangle} * @returns {DBTangle}
*/ */
function getTangle(tangleID) { function getTangle(tangleID) {