This commit is contained in:
Raven Scott
2026-05-21 02:00:00 -04:00
parent 8971182b3f
commit 3b75e74dbd
107 changed files with 1060 additions and 211 deletions
@@ -1,6 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertBee } = require('../../_shared/storage-gossip-base.js')
const { beeStats, inLexRange } = require('../../_shared/hyperbee-base.js')
const PROTOCOL = 'bee-range-watch/v1'
class HyperP2PBeeRangeWatch extends EventEmitter {
@@ -36,11 +37,26 @@ class HyperP2PBeeRangeWatch extends EventEmitter {
return this.emitChange(key, value, op)
}
coversKey (key) {
if (key == null) return false
for (const w of this._watches.values()) {
if (inLexRange(key, w.gte, w.lte)) return true
}
return false
}
unwatchAll () {
const n = this._watches.size
this._watches.clear()
this._stats.unwatch += n
return n
}
emitChange (key, value, op = 'put') {
if (key == null) throw new Error('key required')
let n = 0
for (const w of this._watches.values()) {
if (key >= w.gte && key <= w.lte) {
if (inLexRange(key, w.gte, w.lte)) {
w.cb({ key, value, op, at: Date.now() })
n++
}
@@ -76,11 +92,7 @@ class HyperP2PBeeRangeWatch extends EventEmitter {
}
getStats () {
return {
...this._stats,
active: this._watches.size,
protocol: PROTOCOL
}
return beeStats(this._stats, PROTOCOL, { active: this._watches.size })
}
async ready () { return this }
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-bee-range-watch",
"version": "0.3.1",
"version": "0.3.2",
"description": "Range watch notifications on Hyperbee.",
"main": "index.js",
"type": "commonjs",
@@ -34,6 +34,7 @@ test('getStats', async (t) => {
const m = new HyperP2PBeeRangeWatch()
m.watchRange('a', 'c', () => {})
t.is(m.getStats().active, 1)
t.is(m.getStats().mode, 'hyperbee')
await m.close()
})