Browse Source

ensure ready is only called once on each group

Kieran Gibb 1 year ago
parent
commit
82ac066d5a
No known key found for this signature in database
2 changed files with 22 additions and 35 deletions
  1. 2
    3
      index.js
  2. 20
    32
      test/index.test.js

+ 2
- 3
index.js View File

@@ -10,7 +10,7 @@ module.exports = (storage, factory, opts) => new GroupStore(storage, factory, op
10 10
 
11 11
 class GroupStore {
12 12
   constructor (storage, factory, opts = {}) {
13
-    this._id = crypto.randomBytes(16).toString('hex')
13
+    this._id = crypto.randomBytes(2).toString('hex')
14 14
     this.storage = storage
15 15
     this.opts = opts
16 16
     this.factory = factory
@@ -77,8 +77,7 @@ class GroupStore {
77 77
 
78 78
   _ready (callback) {
79 79
     var pending = 1
80
-    var self = this
81
-    var collection = Object.values(self.collection)
80
+    var collection = uniq(Object.values(this.collection))
82 81
 
83 82
     function next (n) {
84 83
       var entry = collection[n]

+ 20
- 32
test/index.test.js View File

@@ -1,11 +1,11 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const Config = require('cobox-config')
3 3
 const crypto = require('cobox-crypto')
4
-const Store = require('../')
4
+const Group = require('cobox-group')
5
+const Replicator = require('cobox-group-base')
5 6
 
7
+const Store = require('../')
6 8
 const { tmp, cleanup } = require('./util')
7
-const Group = require('cobox-group/test/mock')
8
-const BlindReplicator = require('cobox-blind-replicator/test/mock')
9 9
 
10 10
 describe('group store: basic', (context) => {
11 11
   context('ready()', async function (assert, next) {
@@ -19,13 +19,9 @@ describe('group store: basic', (context) => {
19 19
     const store = Store(storage, Group, { config })
20 20
     store.seed(collection)
21 21
 
22
-    try {
23
-      await store.ready()
24
-      assert.same(Object.values(store.collection).length, 1, 'builds then caches groups from the config')
25
-      assert.ok(store.collection[params.address.toString('hex')], 'accessible using address hex string')
26
-    } catch (err) {
27
-      assert.error(err, 'no error')
28
-    }
22
+    await store.ready()
23
+    assert.same(Object.values(store.collection).length, 1, 'builds then caches groups from the config')
24
+    assert.ok(store.collection[params.address.toString('hex')], 'accessible using address hex string')
29 25
 
30 26
     cleanup(storage, next)
31 27
   })
@@ -41,14 +37,10 @@ describe('group store: basic', (context) => {
41 37
     const store = Store(storage, Group, { config })
42 38
     store.seed(collection)
43 39
 
44
-    try {
45
-      await store.ready()
46
-      var response = await store.all(params)
47
-      assert.ok(Array.isArray(response), 'returns an array')
48
-      assert.same(response.length, 1, 'returns the correct number')
49
-    } catch (err) {
50
-      assert.error(err, 'no error')
51
-    }
40
+    await store.ready()
41
+    var response = await store.all(params)
42
+    assert.ok(Array.isArray(response), 'returns an array')
43
+    assert.same(response.length, 1, 'returns the correct number')
52 44
 
53 45
     cleanup(storage, next)
54 46
   })
@@ -65,20 +57,16 @@ describe('group store: basic', (context) => {
65 57
     const store = Store(storage, Group, { config })
66 58
     store.seed(collection)
67 59
 
68
-    try {
69
-      await store.ready()
70
-      var response = await store.where({
71
-        name: 'magma',
72
-        address: params.address
73
-      })
60
+    await store.ready()
61
+    var response = await store.where({
62
+      name: 'magma',
63
+      address: params.address
64
+    })
74 65
 
75
-      assert.ok(Array.isArray(response), 'returns an array')
76
-      var entry = response[0]
77
-      assert.ok(entry, 'finds a entry')
78
-      assert.same(entry && entry.address, params.address, 'finds the correct entry')
79
-    } catch (err) {
80
-      assert.error(err, 'no error')
81
-    }
66
+    assert.ok(Array.isArray(response), 'returns an array')
67
+    var entry = response[0]
68
+    assert.ok(entry, 'finds a entry')
69
+    assert.same(entry && entry.address, params.address, 'finds the correct entry')
82 70
 
83 71
     cleanup(storage, next)
84 72
 
@@ -330,7 +318,7 @@ describe('multiple', (context) => {
330 318
         store: Store(storage, Group, { config })
331 319
       },
332 320
       replicators: {
333
-        store: Store(storage, BlindReplicator, { config })
321
+        store: Store(storage, Replicator, { config })
334 322
       }
335 323
     }
336 324