improve read/write APIs

This commit is contained in:
Andre Staltz 2023-10-26 17:23:46 +03:00
parent 3e32267b74
commit e20928e0d5
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 24 additions and 18 deletions

View File

@ -341,6 +341,8 @@ function initSet(peer, config) {
function add(subdomain, value, cb) { function add(subdomain, value, cb) {
assertDBPlugin(peer) assertDBPlugin(peer)
assert(!!accountID, 'Cannot add to Set before loading') assert(!!accountID, 'Cannot add to Set before loading')
// prettier-ignore
assert(typeof cb === 'function', 'add() does not accept an accountID in the 3rd argument, must be callback instead')
loaded(() => { loaded(() => {
assert(!!accountID, 'Cannot add to Set before loading') assert(!!accountID, 'Cannot add to Set before loading')
@ -384,6 +386,8 @@ function initSet(peer, config) {
function del(subdomain, value, cb) { function del(subdomain, value, cb) {
assertDBPlugin(peer) assertDBPlugin(peer)
assert(!!accountID, 'Cannot add to Set before loading') assert(!!accountID, 'Cannot add to Set before loading')
// prettier-ignore
assert(typeof cb === 'function', 'del() does not accept an accountID in the 3rd argument, must be callback instead')
loaded(() => { loaded(() => {
assert(!!accountID, 'Cannot add to Set before loading') assert(!!accountID, 'Cannot add to Set before loading')
@ -411,21 +415,23 @@ function initSet(peer, config) {
} }
/** /**
* @param {string} id
* @param {string} subdomain * @param {string} subdomain
* @param {any} value * @param {any} value
* @param {string=} id
*/ */
function has(id, subdomain, value) { function has(subdomain, value, id) {
const set = readSet(id, subdomain) assert(!!accountID, 'Cannot call has() before loading')
const set = readSet(id ?? accountID, subdomain)
return set.has(value) return set.has(value)
} }
/** /**
* @param {string} id
* @param {string} subdomain * @param {string} subdomain
* @param {string=} id
*/ */
function values(id, subdomain) { function values(subdomain, id) {
const set = readSet(id, subdomain) assert(!!accountID, 'Cannot call has() before loading')
const set = readSet(id ?? accountID, subdomain)
return [...set] return [...set]
} }

View File

@ -45,12 +45,12 @@ let add1, add2, del1, add3, del2
test('Set add(), del(), has()', async (t) => { test('Set add(), del(), has()', async (t) => {
// Add 1st // Add 1st
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '1st'), peer.set.has('follows', '1st'),
false, false,
'doesnt have 1st' 'doesnt have 1st'
) )
assert(await p(peer.set.add)('follows', '1st'), 'add 1st') assert(await p(peer.set.add)('follows', '1st'), 'add 1st')
assert.equal(peer.set.has(aliceID, 'follows', '1st'), true, 'has 1st') assert.equal(peer.set.has('follows', '1st'), true, 'has 1st')
add1 = lastMsgID() add1 = lastMsgID()
assert.deepEqual( assert.deepEqual(
peer.set._getItemRoots('follows'), peer.set._getItemRoots('follows'),
@ -60,12 +60,12 @@ test('Set add(), del(), has()', async (t) => {
// Add 2nd // Add 2nd
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '2nd'), peer.set.has('follows', '2nd'),
false, false,
'doesnt have 2nd' 'doesnt have 2nd'
) )
assert(await p(peer.set.add)('follows', '2nd'), 'add 2nd') assert(await p(peer.set.add)('follows', '2nd'), 'add 2nd')
assert.equal(peer.set.has(aliceID, 'follows', '2nd'), true, 'has 2nd') assert.equal(peer.set.has('follows', '2nd'), true, 'has 2nd')
add2 = lastMsgID() add2 = lastMsgID()
assert.deepEqual( assert.deepEqual(
peer.set._getItemRoots('follows'), peer.set._getItemRoots('follows'),
@ -74,10 +74,10 @@ test('Set add(), del(), has()', async (t) => {
) )
// Del 1st // Del 1st
assert.equal(peer.set.has(aliceID, 'follows', '1st'), true, 'has 1st') assert.equal(peer.set.has('follows', '1st'), true, 'has 1st')
assert(await p(peer.set.del)('follows', '1st'), 'del 1st') assert(await p(peer.set.del)('follows', '1st'), 'del 1st')
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '1st'), peer.set.has( 'follows', '1st'),
false, false,
'doesnt have 1st' 'doesnt have 1st'
) )
@ -90,12 +90,12 @@ test('Set add(), del(), has()', async (t) => {
// Add 3rd // Add 3rd
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '3rd'), peer.set.has( 'follows', '3rd'),
false, false,
'doesnt have 3rd' 'doesnt have 3rd'
) )
assert(await p(peer.set.add)('follows', '3rd'), 'add 3rd') assert(await p(peer.set.add)('follows', '3rd'), 'add 3rd')
assert.equal(peer.set.has(aliceID, 'follows', '3rd'), true, 'has 3rd') assert.equal(peer.set.has( 'follows', '3rd'), true, 'has 3rd')
add3 = lastMsgID() add3 = lastMsgID()
assert.deepEqual( assert.deepEqual(
peer.set._getItemRoots('follows'), peer.set._getItemRoots('follows'),
@ -104,10 +104,10 @@ test('Set add(), del(), has()', async (t) => {
) )
// Del 2nd // Del 2nd
assert.equal(peer.set.has(aliceID, 'follows', '2nd'), true, 'has 2nd') assert.equal(peer.set.has( 'follows', '2nd'), true, 'has 2nd')
assert(await p(peer.set.del)('follows', '2nd'), 'del 2nd') // msg seq 4 assert(await p(peer.set.del)('follows', '2nd'), 'del 2nd') // msg seq 4
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '2nd'), peer.set.has( 'follows', '2nd'),
false, false,
'doesnt have 2nd' 'doesnt have 2nd'
) )
@ -125,7 +125,7 @@ test('Set add(), del(), has()', async (t) => {
'del 2nd idempotent' 'del 2nd idempotent'
) )
assert.equal( assert.equal(
peer.set.has(aliceID, 'follows', '2nd'), peer.set.has( 'follows', '2nd'),
false, false,
'doesnt have 2nd' 'doesnt have 2nd'
) )
@ -144,7 +144,7 @@ test('Set values()', async (t) => {
add5 = lastMsgID() add5 = lastMsgID()
const expected = new Set(['3rd', '4th', '5th']) const expected = new Set(['3rd', '4th', '5th'])
for (const item of peer.set.values(aliceID, 'follows')) { for (const item of peer.set.values( 'follows')) {
assert.equal(expected.has(item), true, 'values() item') assert.equal(expected.has(item), true, 'values() item')
expected.delete(item) expected.delete(item)
} }