mirror of https://codeberg.org/pzp/pzp-goals.git
parse() and serialize()
This commit is contained in:
parent
49c4e1b280
commit
e8e88a8bf3
36
lib/index.js
36
lib/index.js
|
@ -187,6 +187,36 @@ function initGoals(peer, config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
* @param {GoalDSL} goalDSL
|
||||||
|
* @returns {Goal}
|
||||||
|
*/
|
||||||
|
function parse(goalDSL) {
|
||||||
|
return new GoalImpl('?', goalDSL)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Pick<Goal, 'type' | 'count'>} goal
|
||||||
|
* @returns {GoalDSL}
|
||||||
|
*/
|
||||||
|
function serialize(goal) {
|
||||||
|
switch (goal.type) {
|
||||||
|
case 'newest':
|
||||||
|
return `newest-${goal.count}`
|
||||||
|
case 'all':
|
||||||
|
return 'all'
|
||||||
|
case 'set':
|
||||||
|
return 'set'
|
||||||
|
case 'dict':
|
||||||
|
return 'dict'
|
||||||
|
case 'none':
|
||||||
|
return 'none'
|
||||||
|
default:
|
||||||
|
throw new Error(`Unrecognized goal type: ${goal.type}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
* @param {string} tangleID
|
* @param {string} tangleID
|
||||||
|
@ -268,14 +298,14 @@ function initGoals(peer, config) {
|
||||||
assertDictPlugin(peer)
|
assertDictPlugin(peer)
|
||||||
const span = peer.dict.getGhostSpan()
|
const span = peer.dict.getGhostSpan()
|
||||||
if (peer.dict.isGhostable(msgID, tangle.id)) {
|
if (peer.dict.isGhostable(msgID, tangle.id)) {
|
||||||
return ['ghost', {tangleID: tangle.id, span }]
|
return ['ghost', { tangleID: tangle.id, span }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (goalType === 'set') {
|
if (goalType === 'set') {
|
||||||
assertSetPlugin(peer)
|
assertSetPlugin(peer)
|
||||||
const span = peer.set.getGhostSpan()
|
const span = peer.set.getGhostSpan()
|
||||||
if (peer.set.isGhostable(msgID, tangle.id)) {
|
if (peer.set.isGhostable(msgID, tangle.id)) {
|
||||||
return ['ghost', {tangleID: tangle.id, span }]
|
return ['ghost', { tangleID: tangle.id, span }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -292,6 +322,8 @@ function initGoals(peer, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
parse,
|
||||||
|
serialize,
|
||||||
set,
|
set,
|
||||||
get,
|
get,
|
||||||
getMsgPurpose,
|
getMsgPurpose,
|
||||||
|
|
|
@ -4,6 +4,25 @@ const { isMapIterator } = require('node:util/types')
|
||||||
const p = require('node:util').promisify
|
const p = require('node:util').promisify
|
||||||
const { createPeer } = require('./util')
|
const { createPeer } = require('./util')
|
||||||
|
|
||||||
|
test('parse() and serialize()', async (t) => {
|
||||||
|
const peer = createPeer({ name: 'alice' })
|
||||||
|
await peer.db.loaded()
|
||||||
|
|
||||||
|
const all = peer.goals.parse('all')
|
||||||
|
assert.equal(all.type, 'all')
|
||||||
|
assert.equal(all.count, Infinity)
|
||||||
|
|
||||||
|
const newest = peer.goals.parse('newest-123')
|
||||||
|
assert.equal(newest.type, 'newest')
|
||||||
|
assert.equal(newest.count, 123)
|
||||||
|
|
||||||
|
assert.equal(peer.goals.serialize(all), 'all')
|
||||||
|
assert.equal(peer.goals.serialize(newest), 'newest-123')
|
||||||
|
|
||||||
|
await p(setTimeout)(200) // necessary wait, otherwise peer.close fails
|
||||||
|
await p(peer.close)(true)
|
||||||
|
})
|
||||||
|
|
||||||
test('set, getByID, list, watch', async (t) => {
|
test('set, getByID, list, watch', async (t) => {
|
||||||
const alice = createPeer({ name: 'alice' })
|
const alice = createPeer({ name: 'alice' })
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue