rename getRecordPurpose to getMsgPurpose

This commit is contained in:
Andre Staltz 2023-10-25 19:40:35 +03:00
parent f99d906161
commit e14fd87658
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 25 additions and 24 deletions

View File

@ -1,13 +1,12 @@
// @ts-ignore
const Obz = require('obz')
// @ts-ignore
const multicb = require('multicb')
/**
* @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB
* @typedef {ReturnType<import('ppppp-record').init>} PPPPPRecord
* @typedef {import('ppppp-db').RecPresent} RecPresent
* @typedef {import('ppppp-db').Tangle} Tangle
* @typedef {import('ppppp-db').Msg} Msg
* @typedef {ReturnType<PPPPPDB['getTangle']>} DBTangle
* @typedef {string} MsgID
* @typedef {'none'|'all'|`newest-${number}`|'record'|'set'} GoalDSL
@ -159,7 +158,7 @@ function initGoals(peer, config) {
case 'record':
assertRecordPlugin(peer)
const minDepth = peer.record.getMinRequiredDepth(goal.id)
const minDepth = peer.record.minRequiredDepth(goal.id)
return [minDepth, maxDepth]
case 'none':
@ -193,18 +192,19 @@ function initGoals(peer, config) {
/**
* @public
* @param {RecPresent} rec
* @param {MsgID} msgID
* @param {Msg} msg
* @returns {Purpose}
*/
function getRecordPurpose(rec) {
function getMsgPurpose(msgID, msg) {
assertDBPlugin(peer)
let servesAsTrail = false
// Check whether this record is a goalful root of some tangle:
asRoot: if (goals.has(rec.id)) {
const goal = /** @type {GoalImpl} */ (goals.get(rec.id))
asRoot: if (goals.has(msgID)) {
const goal = /** @type {GoalImpl} */ (goals.get(msgID))
if (goal.type === 'none') break asRoot
const tangle = peer.db.getTangle(rec.id)
const tangle = peer.db.getTangle(msgID)
if (!tangle) break asRoot
const [min, max] = crossGoalWithTangle(goal, tangle)
if (min > max) break asRoot
@ -215,7 +215,7 @@ function initGoals(peer, config) {
// Check whether this record is a goalful affix of some tangle:
const validTangles =
/** @type {Array<[DBTangle, number, number, number, GoalType]>} */ ([])
asAffix: for (const tangleID in rec.msg.metadata.tangles) {
asAffix: for (const tangleID in msg.metadata.tangles) {
if (!goals.has(tangleID)) continue asAffix
const goal = /** @type {GoalImpl} */ (goals.get(tangleID))
if (goal.type === 'none') continue asAffix
@ -223,7 +223,7 @@ function initGoals(peer, config) {
if (!tangle) continue asAffix
const [min, max] = crossGoalWithTangle(goal, tangle)
if (min > max) continue asAffix
const recDepth = tangle.getDepth(rec.id)
const recDepth = tangle.getDepth(msgID)
if (recDepth < 0) continue asAffix
validTangles.push([tangle, min, max, recDepth, goal.type])
}
@ -241,14 +241,14 @@ function initGoals(peer, config) {
.topoSort()
.filter((msgID) => tangle.getDepth(msgID) === min)
const { erasables } = tangle.getDeletablesAndErasables(...minMsgIDs)
if (erasables.has(rec.id)) return 'trail'
if (erasables.has(msgID)) return 'trail'
}
// Check whether this record is a ghost affix of some tangle:
for (const [tangle, , , , goalType] of validTangles) {
if (goalType === 'record') {
assertRecordPlugin(peer)
if (peer.record.isGhostable(rec.id, tangle.id)) {
if (peer.record.isGhostable(msgID, tangle.id)) {
return 'ghost'
}
}
@ -268,7 +268,7 @@ function initGoals(peer, config) {
return {
set,
get,
getRecordPurpose,
getMsgPurpose,
list,
listen,
}

View File

@ -12,7 +12,8 @@
"main": "index.js",
"files": [
"*.js",
"lib/*.js"
"lib/*.js",
"lib/*.d.ts"
],
"types": "types/index.d.ts",
"exports": {

View File

@ -34,7 +34,7 @@ test('set, getByID, list, listen', async (t) => {
}
{
const purpose = alice.goals.getRecordPurpose(aliceAccountRoot)
const purpose = alice.goals.getMsgPurpose(aliceAccountRoot.id, aliceAccountRoot.msg)
assert.equal(purpose, 'goal', 'rec purpose is "goal"')
}
@ -60,7 +60,7 @@ test('set, getByID, list, listen', async (t) => {
await p(alice.close)(true)
})
test('getRecordPurpose', async (t) => {
test('getMsgPurpose', async (t) => {
const alice = createPeer({ name: 'alice' })
await alice.db.loaded()
@ -91,18 +91,18 @@ test('getRecordPurpose', async (t) => {
const gottenGoal = alice.goals.get(feedID)
assert.strictEqual(gottenGoal.id, feedID, 'gotten goal id is correct')
const purpose = alice.goals.getRecordPurpose(post2)
const purpose = alice.goals.getMsgPurpose(post2.id, post2.msg)
assert.equal(purpose, 'goal', 'purpose is "goal"')
alice.goals.set(feedID, 'newest-1')
assert('set goal to newest-1')
const purpose2 = alice.goals.getRecordPurpose(post2)
const purpose2 = alice.goals.getMsgPurpose(post2.id, post2.msg)
assert.equal(purpose2, 'none', 'purpose2 is "none"')
await p(alice.close)(true)
})
test('getRecordPurpose ghost', async (t) => {
test('getMsgPurpose ghost', async (t) => {
const alice = createPeer({ name: 'alice', record: {ghostSpan: 3} })
await alice.db.loaded()
@ -126,11 +126,11 @@ test('getRecordPurpose ghost', async (t) => {
const recs = msgIDs.map(id => alice.db.getRecord(id))
alice.goals.set(feedID, 'record')
assert.equal(alice.goals.getRecordPurpose(recs[1]), 'none')
assert.equal(alice.goals.getRecordPurpose(recs[2]), 'ghost')
assert.equal(alice.goals.getRecordPurpose(recs[3]), 'trail')
assert.equal(alice.goals.getRecordPurpose(recs[4]), 'trail')
assert.equal(alice.goals.getRecordPurpose(recs[5]), 'goal')
assert.equal(alice.goals.getMsgPurpose(recs[1].id, recs[1].msg), 'none')
assert.equal(alice.goals.getMsgPurpose(recs[2].id, recs[2].msg), 'ghost')
assert.equal(alice.goals.getMsgPurpose(recs[3].id, recs[3].msg), 'trail')
assert.equal(alice.goals.getMsgPurpose(recs[4].id, recs[4].msg), 'trail')
assert.equal(alice.goals.getMsgPurpose(recs[5].id, recs[5].msg), 'goal')
await p(alice.close)(true)
})