Skip to content

Commit

Permalink
add weak session support (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Nov 27, 2024
1 parent 147478e commit 68a24d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Hypercore extends EventEmitter {
this.exclusive = false
this.opened = false
this.closed = false
this.weak = !!opts.weak
this.snapshotted = !!opts.snapshot
this.draft = !!opts.draft
this.onwait = opts.onwait || null
Expand Down Expand Up @@ -206,6 +207,7 @@ class Hypercore extends EventEmitter {
const writable = opts.writable === false ? false : !this._readonly
const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
const timeout = opts.timeout === undefined ? this.timeout : opts.timeout
const weak = opts.weak === undefined ? this.weak : opts.weak
const Clz = opts.class || Hypercore
const s = new Clz(null, this.key, {
...opts,
Expand All @@ -214,6 +216,7 @@ class Hypercore extends EventEmitter {
onwait,
timeout,
writable,
weak,
parent: this
})

Expand Down
12 changes: 10 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = class Core {
this.replicator = new Replicator(this, opts)
this.sessionStates = []
this.monitors = []
this.activeSessions = 0

this.id = opts.key ? z32.encode(opts.key) : null
this.key = opts.key || null
Expand Down Expand Up @@ -86,12 +87,14 @@ module.exports = class Core {

allSessions () {
const sessions = []
for (const state of this.sessionStates) sessions.push(...state.sessions)
for (const state of this.sessionStates) {
if (state.sessions.length) sessions.push(...state.sessions)
}
return sessions
}

hasSession () {
return this.sessionStates.length > 1 || (this.state !== null && this.state.sessions.length !== 0)
return this.activeSessions !== 0
}

checkIfIdle () {
Expand Down Expand Up @@ -719,8 +722,13 @@ module.exports = class Core {
}

destroy () {
if (this.hasSession() === true) throw new Error('Cannot destroy while sessions are open')
if (this.replicator) this.replicator.destroy()
if (this.state) this.state.destroy()

// close all pending weak sessions...
const weakSessions = this.allSessions()
for (const s of weakSessions) s.close().catch(noop)
}

async _close () {
Expand Down
3 changes: 3 additions & 0 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ module.exports = class SessionState {
}

addSession (s) {
if (s._stateIndex !== -1) return
s._stateIndex = this.sessions.push(s) - 1
if (s.weak === false) this.core.activeSessions++
}

removeSession (s) {
if (s._stateIndex === -1) return
const head = this.sessions.pop()
if (head !== s) this.sessions[(head._stateIndex = s._stateIndex)] = head
s._stateIndex = -1
if (s.weak === false) this.core.activeSessions--
this.core.checkIfIdle()
}

Expand Down

0 comments on commit 68a24d5

Please sign in to comment.