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

clean up replication requests on session close #183

Merged
merged 6 commits into from
Sep 26, 2024
Merged
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
13 changes: 11 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { ViewRecord } = require('./messages')

const {
SESSION_CLOSED,
REQUEST_CANCELLED,
BLOCK_NOT_AVAILABLE
} = require('hypercore/errors')

Expand Down Expand Up @@ -246,7 +247,9 @@ class AutocoreSession extends EventEmitter {
if (this.opened === false) await this.ready()
if (this.closing === true) throw SESSION_CLOSED()

opts = { activeRequests: this.activeRequests, valueEncoding: this.valueEncoding, ...opts }
const onwait = autocoreOnWait.bind(this, opts && opts.onwait)

opts = { activeRequests: this.activeRequests, valueEncoding: this.valueEncoding, ...opts, onwait }

// check if we indexed this already
if (index < this.indexedLength) {
Expand Down Expand Up @@ -311,7 +314,8 @@ class AutocoreSession extends EventEmitter {
const top = this._source.sessions.pop()
if (top !== this) this._source.sessions[top._index = this._index] = top

// TODO: clear active requests also!
const core = this.getBackingCore().session
if (core.replicator) core.replicator.clearRequests(this.activeRequests)

this.emit('close', false)
}
Expand Down Expand Up @@ -899,3 +903,8 @@ module.exports = class Autocore extends ReadyResource {
function debugWarn (...msg) { // calls to this are ONLY allowed for soft assertions
console.log('[autobase]', ...msg)
}

function autocoreOnWait (fn, index, core) {
if (this.closing) throw REQUEST_CANCELLED()
if (fn) return fn(index, this)
}
Loading