hypercore 11.40.5 → 11.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +6 -1
- package/lib/core.js +13 -0
- package/lib/session-state.js +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -88,6 +88,7 @@ class Hypercore extends EventEmitter {
|
|
|
88
88
|
this.preload = null
|
|
89
89
|
this.closing = null
|
|
90
90
|
this.opening = null
|
|
91
|
+
this.orphaned = false
|
|
91
92
|
|
|
92
93
|
this._readonly = opts.writable === false
|
|
93
94
|
this._preappend = preappend.bind(this)
|
|
@@ -244,6 +245,10 @@ class Hypercore extends EventEmitter {
|
|
|
244
245
|
if (opts.checkout !== undefined && !opts.name && !opts.atom) {
|
|
245
246
|
throw ASSERTION('Checkouts are only supported on atoms or named sessions', this.discoveryKey)
|
|
246
247
|
}
|
|
248
|
+
if (this.weak === true && opts.weak !== undefined && !opts.weak) {
|
|
249
|
+
// a weak session does not keep the core open, so it cannot hand out one that does
|
|
250
|
+
throw ASSERTION('Cannot make a strong session on a weak session', this.discoveryKey)
|
|
251
|
+
}
|
|
247
252
|
|
|
248
253
|
const wait = opts.wait === false ? false : this.wait
|
|
249
254
|
const writable = opts.writable === undefined ? !this._readonly : opts.writable === true
|
|
@@ -735,7 +740,7 @@ class Hypercore extends EventEmitter {
|
|
|
735
740
|
|
|
736
741
|
if (this.weak === false) {
|
|
737
742
|
this.core.activeSessions--
|
|
738
|
-
core.activeSessions++
|
|
743
|
+
if (core.activeSessions++ === 0) core.orphaned = false
|
|
739
744
|
}
|
|
740
745
|
|
|
741
746
|
if (this._monitorIndex >= 0) {
|
package/lib/core.js
CHANGED
|
@@ -28,6 +28,7 @@ module.exports = class Core {
|
|
|
28
28
|
this.sessionStates = []
|
|
29
29
|
this.monitors = []
|
|
30
30
|
this.activeSessions = 0
|
|
31
|
+
this.orphaned = false
|
|
31
32
|
this.gc = 0 // corestore uses this to main a gc strike pool
|
|
32
33
|
|
|
33
34
|
this.id = opts.key ? z32.encode(opts.key) : null
|
|
@@ -121,6 +122,16 @@ module.exports = class Core {
|
|
|
121
122
|
return this.activeSessions !== 0
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
emitOrphaned() {
|
|
126
|
+
if (this.orphaned === true) return
|
|
127
|
+
this.orphaned = true
|
|
128
|
+
|
|
129
|
+
for (const s of this.allSessions()) {
|
|
130
|
+
if (s._stateIndex === -1 || s.closing !== null) continue
|
|
131
|
+
s.emit('orphaned')
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
compact() {
|
|
125
136
|
const compacting = []
|
|
126
137
|
for (const s of this.sessionStates) {
|
|
@@ -131,6 +142,8 @@ module.exports = class Core {
|
|
|
131
142
|
|
|
132
143
|
checkIfIdle() {
|
|
133
144
|
if (!this.opened || this.destroyed === true || this.hasSession() === true) return
|
|
145
|
+
this.emitOrphaned()
|
|
146
|
+
if (this.hasSession() === true) return // a listener may have re-opened the core
|
|
134
147
|
if (this.replicator.idle() === false) return
|
|
135
148
|
if (this.state === null || this.state.mutex.idle() === false) return
|
|
136
149
|
this.onidle()
|
package/lib/session-state.js
CHANGED
|
@@ -104,7 +104,7 @@ class SessionState {
|
|
|
104
104
|
addSession(s) {
|
|
105
105
|
if (s._stateIndex !== -1) return
|
|
106
106
|
s._stateIndex = this.sessions.push(s) - 1
|
|
107
|
-
if (s.weak === false
|
|
107
|
+
if (s.weak === false && this.core.activeSessions++ === 0) this.core.orphaned = false
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
removeSession(s) {
|