Browse Source

initialize separately with a collection

Kieran Gibb 2 years ago
parent
commit
7e84d6694d
No known key found for this signature in database
3 changed files with 32 additions and 2741 deletions
  1. 5
    4
      index.js
  2. 27
    23
      test/index.test.js
  3. 0
    2714
      yarn.lock

+ 5
- 4
index.js View File

@@ -11,18 +11,19 @@ const { uniq, isString } = require('./util')
11 11
 module.exports = (storage, factory, collection, opts) => new GroupStore(storage, factory, collection, opts)
12 12
 
13 13
 class GroupStore {
14
-  constructor (storage, factory, collection, opts = {}) {
14
+  constructor (storage, factory, opts = {}) {
15 15
     this._id = crypto.randomBytes(16).toString('hex')
16 16
     this.storage = storage
17 17
     this.opts = opts
18 18
     this.factory = factory
19
-
20 19
     this.collection = {}
20
+    this._readyCallback = thunky(this._ready.bind(this))
21
+  }
22
+
23
+  seed (collection) {
21 24
     collection.forEach((_opts, i) => {
22 25
       this._cache(this._build(_opts))
23 26
     })
24
-
25
-    this._readyCallback = thunky(this._ready.bind(this))
26 27
   }
27 28
 
28 29
   ready (callback) {

+ 27
- 23
test/index.test.js View File

@@ -16,7 +16,8 @@ describe('group store: basic', (context) => {
16 16
     config.groups.set(params.address, params)
17 17
 
18 18
     const collection = config.groups.list()
19
-    const store = Store(storage, Group, collection, { config })
19
+    const store = Store(storage, Group, { config })
20
+    store.seed(collection)
20 21
 
21 22
     try {
22 23
       await store.ready()
@@ -37,7 +38,8 @@ describe('group store: basic', (context) => {
37 38
     config.groups.set(params.address, params)
38 39
 
39 40
     const collection = config.groups.list()
40
-    const store = Store(storage, Group, collection, { config })
41
+    const store = Store(storage, Group, { config })
42
+    store.seed(collection)
41 43
 
42 44
     try {
43 45
       await store.ready()
@@ -60,7 +62,8 @@ describe('group store: basic', (context) => {
60 62
     config.groups.set(params.address, params)
61 63
 
62 64
     const collection = config.groups.list()
63
-    const store = Store(storage, Group, collection, { config })
65
+    const store = Store(storage, Group, { config })
66
+    store.seed(collection)
64 67
 
65 68
     try {
66 69
       await store.ready()
@@ -98,7 +101,8 @@ describe('group store: basic', (context) => {
98 101
     config.groups.set(params.address, params)
99 102
 
100 103
     const collection = config.groups.list()
101
-    const store = Store(storage, Group, collection, { config })
104
+    const store = Store(storage, Group, { config })
105
+    store.seed(collection)
102 106
 
103 107
     try {
104 108
       await store.ready()
@@ -124,7 +128,8 @@ describe('group store: basic', (context) => {
124 128
     config.groups.set(params.address, params)
125 129
 
126 130
     const collection = config.groups.list()
127
-    const store = Store(storage, Group, collection, { config })
131
+    const store = Store(storage, Group, { config })
132
+    store.seed(collection)
128 133
 
129 134
     try {
130 135
       await store.ready()
@@ -150,7 +155,8 @@ describe('group store: basic', (context) => {
150 155
     config.groups.set(params.address, params)
151 156
 
152 157
     const collection = config.groups.list()
153
-    const store = Store(storage, Group, collection, { config })
158
+    const store = Store(storage, Group, { config })
159
+    store.seed(collection)
154 160
 
155 161
     try {
156 162
       await store.ready()
@@ -176,7 +182,8 @@ describe('group store: basic', (context) => {
176 182
     config.groups.set(params.address, params)
177 183
 
178 184
     const collection = config.groups.list()
179
-    const store = Store(storage, Group, collection, { config })
185
+    const store = Store(storage, Group, { config })
186
+    store.seed(collection)
180 187
 
181 188
     try {
182 189
       await store.ready()
@@ -202,7 +209,8 @@ describe('group store: basic', (context) => {
202 209
     config.groups.set(params.address, params)
203 210
 
204 211
     const collection = config.groups.list()
205
-    const store = Store(storage, Group, collection, { config })
212
+    const store = Store(storage, Group, { config })
213
+    store.seed(collection)
206 214
 
207 215
     try {
208 216
       await store.ready()
@@ -227,7 +235,8 @@ describe('group store: basic', (context) => {
227 235
     config.groups.set(params.address, params)
228 236
 
229 237
     const collection = config.groups.list()
230
-    const store = Store(storage, Group, collection, { config })
238
+    const store = Store(storage, Group, { config })
239
+    store.seed(collection)
231 240
 
232 241
     try {
233 242
       await store.ready()
@@ -252,7 +261,8 @@ describe('group store: basic', (context) => {
252 261
     config.groups.set(params.address, params)
253 262
 
254 263
     const collection = config.groups.list()
255
-    const store = Store(storage, Group, collection, { config })
264
+    const store = Store(storage, Group, { config })
265
+    store.seed(collection)
256 266
 
257 267
     try {
258 268
       await store.ready()
@@ -273,7 +283,8 @@ describe('group store: basic', (context) => {
273 283
       params = Object.assign(crypto.keySet(), { name: 'magma' })
274 284
 
275 285
     const collection = config.groups.list()
276
-    const store = Store(storage, Group, collection, { config })
286
+    const store = Store(storage, Group, { config })
287
+    store.seed(collection)
277 288
 
278 289
     try {
279 290
       var group = await store.create(params)
@@ -316,23 +327,16 @@ describe('multiple', (context) => {
316 327
 
317 328
     const api = {
318 329
       groups: {
319
-        store: Store(
320
-          storage,
321
-          Group,
322
-          groups,
323
-          { config }
324
-        )
330
+        store: Store(storage, Group, { config })
325 331
       },
326 332
       replicators: {
327
-        store: Store(
328
-          storage,
329
-          BlindReplicator,
330
-          replicators,
331
-          { config }
332
-        )
333
+        store: Store(storage, BlindReplicator, { config })
333 334
       }
334 335
     }
335 336
 
337
+    api.groups.store.seed(groups)
338
+    api.replicators.store.seed(replicators)
339
+
336 340
     await api.groups.store.ready()
337 341
     await api.replicators.store.ready()
338 342
 

+ 0
- 2714
yarn.lock
File diff suppressed because it is too large
View File