corestore 6.0.1-alpha.13 → 6.0.1-alpha.14
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 +44 -0
- package/package.json +1 -1
- package/test/all.js +46 -0
package/index.js
CHANGED
|
@@ -25,12 +25,43 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
25
25
|
this._namespace = opts._namespace || DEFAULT_NAMESPACE
|
|
26
26
|
this._replicationStreams = opts._streams || []
|
|
27
27
|
this._streamSessions = opts._streamSessions || new Map()
|
|
28
|
+
this._sessions = new Set() // sessions for THIS namespace
|
|
29
|
+
|
|
30
|
+
this._findingPeersCount = 0
|
|
31
|
+
this._findingPeers = []
|
|
28
32
|
|
|
29
33
|
this._opening = opts._opening ? opts._opening.then(() => this._open()) : this._open()
|
|
30
34
|
this._opening.catch(safetyCatch)
|
|
31
35
|
this.ready = () => this._opening
|
|
32
36
|
}
|
|
33
37
|
|
|
38
|
+
findingPeers () {
|
|
39
|
+
let done = false
|
|
40
|
+
this._incFindingPeers()
|
|
41
|
+
|
|
42
|
+
return () => {
|
|
43
|
+
if (done) return
|
|
44
|
+
done = true
|
|
45
|
+
this._decFindingPeers()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_incFindingPeers () {
|
|
50
|
+
if (++this._findingPeersCount !== 1) return
|
|
51
|
+
|
|
52
|
+
for (const core of this._sessions) {
|
|
53
|
+
this._findingPeers.push(core.findingPeers())
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_decFindingPeers () {
|
|
58
|
+
if (--this._findingPeersCount !== 0) return
|
|
59
|
+
|
|
60
|
+
while (this._findingPeers.length > 0) {
|
|
61
|
+
this._findingPeers.pop()()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
34
65
|
async _open () {
|
|
35
66
|
if (this.keys) {
|
|
36
67
|
this.keys = await this.keys // opts.keys can be a Promise that resolves to a KeyManager
|
|
@@ -155,6 +186,19 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
155
186
|
name: null,
|
|
156
187
|
preload: () => this._preload(opts)
|
|
157
188
|
})
|
|
189
|
+
|
|
190
|
+
this._sessions.add(core)
|
|
191
|
+
if (this._findingPeersCount > 0) {
|
|
192
|
+
this._findingPeers.push(core.findingPeers())
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
core.once('close', () => {
|
|
196
|
+
// technically better to also clear _findingPeers if we added it,
|
|
197
|
+
// but the lifecycle for those are pretty short so prob not worth the complexity
|
|
198
|
+
// as _decFindingPeers clear them all.
|
|
199
|
+
this._sessions.delete(core)
|
|
200
|
+
})
|
|
201
|
+
|
|
158
202
|
return core
|
|
159
203
|
}
|
|
160
204
|
|
package/package.json
CHANGED
package/test/all.js
CHANGED
|
@@ -201,6 +201,52 @@ test('closing a namespace does not close cores', async function (t) {
|
|
|
201
201
|
t.ok(core2.closed)
|
|
202
202
|
})
|
|
203
203
|
|
|
204
|
+
test('findingPeers', async function (t) {
|
|
205
|
+
t.plan(6)
|
|
206
|
+
|
|
207
|
+
const store = new Corestore(ram)
|
|
208
|
+
|
|
209
|
+
const ns1 = store.namespace('ns1')
|
|
210
|
+
const ns2 = store.namespace('ns2')
|
|
211
|
+
|
|
212
|
+
const a = ns1.get(Buffer.alloc(32).fill('a'))
|
|
213
|
+
const b = ns2.get(Buffer.alloc(32).fill('b'))
|
|
214
|
+
|
|
215
|
+
const done = ns1.findingPeers()
|
|
216
|
+
|
|
217
|
+
let aUpdated = false
|
|
218
|
+
let bUpdated = false
|
|
219
|
+
let cUpdated = false
|
|
220
|
+
|
|
221
|
+
const c = ns1.get(Buffer.alloc(32).fill('c'))
|
|
222
|
+
|
|
223
|
+
a.update().then(function (bool) {
|
|
224
|
+
aUpdated = true
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
b.update().then(function (bool) {
|
|
228
|
+
bUpdated = true
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
c.update().then(function (bool) {
|
|
232
|
+
cUpdated = true
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
await new Promise(resolve => setImmediate(resolve))
|
|
236
|
+
|
|
237
|
+
t.is(aUpdated, false)
|
|
238
|
+
t.is(bUpdated, true)
|
|
239
|
+
t.is(cUpdated, false)
|
|
240
|
+
|
|
241
|
+
done()
|
|
242
|
+
|
|
243
|
+
await new Promise(resolve => setImmediate(resolve))
|
|
244
|
+
|
|
245
|
+
t.is(aUpdated, true)
|
|
246
|
+
t.is(bUpdated, true)
|
|
247
|
+
t.is(cUpdated, true)
|
|
248
|
+
})
|
|
249
|
+
|
|
204
250
|
function tmpdir () {
|
|
205
251
|
return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
|
|
206
252
|
}
|