From e14fd87658c5f6e201c3b286e8d1fc8b5710593e Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Wed, 25 Oct 2023 19:40:35 +0300 Subject: [PATCH] rename getRecordPurpose to getMsgPurpose --- lib/index.js | 26 +++++++++++++------------- package.json | 3 ++- test/goals.test.js | 20 ++++++++++---------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7f0bf6b..1823af6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,12 @@ // @ts-ignore const Obz = require('obz') -// @ts-ignore -const multicb = require('multicb') /** * @typedef {ReturnType} PPPPPDB * @typedef {ReturnType} PPPPPRecord * @typedef {import('ppppp-db').RecPresent} RecPresent * @typedef {import('ppppp-db').Tangle} Tangle + * @typedef {import('ppppp-db').Msg} Msg * @typedef {ReturnType} 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, } diff --git a/package.json b/package.json index d6cef50..f7d8542 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "main": "index.js", "files": [ "*.js", - "lib/*.js" + "lib/*.js", + "lib/*.d.ts" ], "types": "types/index.d.ts", "exports": { diff --git a/test/goals.test.js b/test/goals.test.js index e3d9f83..e57833e 100644 --- a/test/goals.test.js +++ b/test/goals.test.js @@ -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) })