mirror of https://codeberg.org/pzp/pzp-dict.git
make getFieldRoots() public
This commit is contained in:
parent
eb902078f1
commit
edb31af0b6
15
lib/index.js
15
lib/index.js
|
@ -168,15 +168,11 @@ module.exports = {
|
||||||
else loadPromise.then(() => cb(null), cb)
|
else loadPromise.then(() => cb(null), cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getFieldRoots(subtype) {
|
|
||||||
return fieldRoots.get(subtype)
|
|
||||||
}
|
|
||||||
|
|
||||||
function _squeezePotential(subtype) {
|
function _squeezePotential(subtype) {
|
||||||
const rootHash = FeedV1.getFeedRootHash(myWho, fromSubtype(subtype))
|
const rootHash = FeedV1.getFeedRootHash(myWho, fromSubtype(subtype))
|
||||||
const tangle = peer.db.getTangle(rootHash)
|
const tangle = peer.db.getTangle(rootHash)
|
||||||
const maxDepth = tangle.getMaxDepth()
|
const maxDepth = tangle.getMaxDepth()
|
||||||
const fieldRoots = _getFieldRoots(subtype)
|
const fieldRoots = getFieldRoots(myWho, subtype)
|
||||||
let minDepth = Infinity
|
let minDepth = Infinity
|
||||||
for (const field in fieldRoots) {
|
for (const field in fieldRoots) {
|
||||||
for (const msgHash of fieldRoots[field]) {
|
for (const msgHash of fieldRoots[field]) {
|
||||||
|
@ -206,6 +202,13 @@ module.exports = {
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
//#region public methods
|
//#region public methods
|
||||||
|
function getFieldRoots(authorId, subtype) {
|
||||||
|
const who = FeedV1.stripAuthor(authorId)
|
||||||
|
// prettier-ignore
|
||||||
|
if (who !== myWho) return cb(new Error('Cannot getFieldRoots for another user\'s record. Given "authorId" was ' + authorId))
|
||||||
|
return fieldRoots.get(subtype)
|
||||||
|
}
|
||||||
|
|
||||||
function get(authorId, subtype) {
|
function get(authorId, subtype) {
|
||||||
const type = fromSubtype(subtype)
|
const type = fromSubtype(subtype)
|
||||||
const rootHash = FeedV1.getFeedRootHash(authorId, type)
|
const rootHash = FeedV1.getFeedRootHash(authorId, type)
|
||||||
|
@ -266,7 +269,7 @@ module.exports = {
|
||||||
get,
|
get,
|
||||||
squeeze,
|
squeeze,
|
||||||
|
|
||||||
_getFieldRoots,
|
getFieldRoots,
|
||||||
_squeezePotential,
|
_squeezePotential,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -35,7 +35,7 @@ test('Record update() and get()', async (t) => {
|
||||||
)
|
)
|
||||||
t.deepEqual(peer.record.get(who, 'profile'), { name: 'alice' }, 'get')
|
t.deepEqual(peer.record.get(who, 'profile'), { name: 'alice' }, 'get')
|
||||||
|
|
||||||
const fieldRoots1 = peer.record._getFieldRoots('profile')
|
const fieldRoots1 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(fieldRoots1, { name: ['Pt4YwxksvCLir45Tmw3hXK'] }, 'fieldRoots')
|
t.deepEquals(fieldRoots1, { name: ['Pt4YwxksvCLir45Tmw3hXK'] }, 'fieldRoots')
|
||||||
|
|
||||||
t.ok(await p(peer.record.update)(who, 'profile', { age: 20 }), 'update .age')
|
t.ok(await p(peer.record.update)(who, 'profile', { age: 20 }), 'update .age')
|
||||||
|
@ -45,7 +45,7 @@ test('Record update() and get()', async (t) => {
|
||||||
'get'
|
'get'
|
||||||
)
|
)
|
||||||
|
|
||||||
const fieldRoots2 = peer.record._getFieldRoots('profile')
|
const fieldRoots2 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
fieldRoots2,
|
fieldRoots2,
|
||||||
{ name: ['Pt4YwxksvCLir45Tmw3hXK'], age: ['XqkG9Uz1eQcxv9R1f3jgKS'] },
|
{ name: ['Pt4YwxksvCLir45Tmw3hXK'], age: ['XqkG9Uz1eQcxv9R1f3jgKS'] },
|
||||||
|
@ -72,7 +72,7 @@ test('Record update() and get()', async (t) => {
|
||||||
'get'
|
'get'
|
||||||
)
|
)
|
||||||
|
|
||||||
const fieldRoots3 = peer.record._getFieldRoots('profile')
|
const fieldRoots3 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
fieldRoots3,
|
fieldRoots3,
|
||||||
{ name: ['WGDGt1UEGPpRyutfDyC2we'], age: ['XqkG9Uz1eQcxv9R1f3jgKS'] },
|
{ name: ['WGDGt1UEGPpRyutfDyC2we'], age: ['XqkG9Uz1eQcxv9R1f3jgKS'] },
|
||||||
|
@ -85,7 +85,7 @@ test('Record squeeze', async (t) => {
|
||||||
t.ok(await p(peer.record.update)(who, 'profile', { age: 22 }), 'update .age')
|
t.ok(await p(peer.record.update)(who, 'profile', { age: 22 }), 'update .age')
|
||||||
t.ok(await p(peer.record.update)(who, 'profile', { age: 23 }), 'update .age')
|
t.ok(await p(peer.record.update)(who, 'profile', { age: 23 }), 'update .age')
|
||||||
|
|
||||||
const fieldRoots4 = peer.record._getFieldRoots('profile')
|
const fieldRoots4 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
fieldRoots4,
|
fieldRoots4,
|
||||||
{ name: ['WGDGt1UEGPpRyutfDyC2we'], age: ['6qu5mbLbFPJHCFge7QtU48'] },
|
{ name: ['WGDGt1UEGPpRyutfDyC2we'], age: ['6qu5mbLbFPJHCFge7QtU48'] },
|
||||||
|
@ -95,7 +95,7 @@ test('Record squeeze', async (t) => {
|
||||||
t.equals(peer.record._squeezePotential('profile'), 3, 'squeezePotential=3')
|
t.equals(peer.record._squeezePotential('profile'), 3, 'squeezePotential=3')
|
||||||
t.true(await p(peer.record.squeeze)(who, 'profile'), 'squeezed')
|
t.true(await p(peer.record.squeeze)(who, 'profile'), 'squeezed')
|
||||||
|
|
||||||
const fieldRoots5 = peer.record._getFieldRoots('profile')
|
const fieldRoots5 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
fieldRoots5,
|
fieldRoots5,
|
||||||
{ name: ['Ba96TjutuuPbdMMvNS4BbL'], age: ['Ba96TjutuuPbdMMvNS4BbL'] },
|
{ name: ['Ba96TjutuuPbdMMvNS4BbL'], age: ['Ba96TjutuuPbdMMvNS4BbL'] },
|
||||||
|
@ -105,7 +105,7 @@ test('Record squeeze', async (t) => {
|
||||||
t.equals(peer.record._squeezePotential('profile'), 0, 'squeezePotential=0')
|
t.equals(peer.record._squeezePotential('profile'), 0, 'squeezePotential=0')
|
||||||
t.false(await p(peer.record.squeeze)(who, 'profile'), 'squeeze idempotent')
|
t.false(await p(peer.record.squeeze)(who, 'profile'), 'squeeze idempotent')
|
||||||
|
|
||||||
const fieldRoots6 = peer.record._getFieldRoots('profile')
|
const fieldRoots6 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(fieldRoots6, fieldRoots5, 'fieldRoots')
|
t.deepEquals(fieldRoots6, fieldRoots5, 'fieldRoots')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ test('Record receives old branched update', async (t) => {
|
||||||
const rec = await p(peer.db.add)(msg, rootHash)
|
const rec = await p(peer.db.add)(msg, rootHash)
|
||||||
t.equals(rec.hash, 'JXvFSXE9s1DF77cSu5XUm', 'msg hash')
|
t.equals(rec.hash, 'JXvFSXE9s1DF77cSu5XUm', 'msg hash')
|
||||||
|
|
||||||
const fieldRoots7 = peer.record._getFieldRoots('profile')
|
const fieldRoots7 = peer.record.getFieldRoots(who, 'profile')
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
fieldRoots7,
|
fieldRoots7,
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue