Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add weak session support #603

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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