Skip to content

Commit

Permalink
add back legacy findingPeers api
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 25, 2025
1 parent c044fba commit e8be6ab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
46 changes: 44 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ class CoreTracker extends EventEmitter {
}
}

class FindingPeers {
constructor () {
this.count = 0
this.pending = []
}

add (core) {
if (this.count === 0) return
this.pending.push(core.findingPeers())
}

inc (sessions) {
if (++this.count !== 1) return

for (const core of sessions) {
this.pending.push(core.findingPeers())
}
}

dec (sessions) {
if (--this.count !== 0) return
while (this.pending.length > 0) this.pending.pop()()
}
}

class Corestore extends ReadyResource {
constructor (storage, opts = {}) {
super()
Expand All @@ -147,6 +172,7 @@ class Corestore extends ReadyResource {

this.manifestVersion = 1 // just compat

this._findingPeers = null // here for legacy
this._ongcBound = this._ongc.bind(this)

if (this.root) this.corestores.add(this)
Expand Down Expand Up @@ -174,6 +200,17 @@ class Corestore extends ReadyResource {
}
}

findingPeers () {
if (this._findingPeers === null) this._findingPeers = new FindingPeers()
this._findingPeers.inc(this.sessions)
let done = false
return () => {
if (done) return
done = true
this._findingPeers.dec(this.sessions)
}
}

session (opts) {
this._maybeClosed()
const root = this.root || this
Expand Down Expand Up @@ -322,7 +359,7 @@ class Corestore extends ReadyResource {
// same goes if user has defined async preload obvs
if (opts.name || opts.preload) {
conf.preload = this._preload(opts)
return new Hypercore(null, null, conf)
return this._makeSession(conf)
}

// if not not we can sync create it, which just is easier for the
Expand All @@ -332,8 +369,13 @@ class Corestore extends ReadyResource {
conf.core = core
conf.sessions = this.sessions.get(core.id)
conf.ongc = this._ongcBound
return this._makeSession(conf)
}

return new Hypercore(null, null, conf)
_makeSession (conf) {
const session = new Hypercore(null, null, conf)
if (this._findingPeers !== null) this._findingPeers.add(session)
return session
}

async createKeyPair (name, ns = this.ns) {
Expand Down
17 changes: 17 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ test('if key is passed, its available immediately', async function (t) {
await store.close()
})

test('finding peers (compat)', async function (t) {
const store = new Corestore(await tmp(t))

const done = store.findingPeers()

const core = store.get({ key: b4a.alloc(32) })
let waited = false

setTimeout(() => {
waited = true
done()
}, 500)

await core.update()
t.ok(waited, 'waited')
})

async function create (t) {
const dir = await tmp(t)
const store = new Corestore(dir)
Expand Down

0 comments on commit e8be6ab

Please sign in to comment.