@live-change/db-store-rbtree 0.6.1 → 0.6.2

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.
Files changed (2) hide show
  1. package/lib/Store.js +14 -8
  2. package/package.json +2 -2
package/lib/Store.js CHANGED
@@ -293,8 +293,14 @@ class CountObservable extends ReactiveDao.ObservableValue {
293
293
  }
294
294
 
295
295
  class Store {
296
- constructor() {
296
+ constructor(options = {}) {
297
297
  this.tree = createTree()
298
+
299
+ const {
300
+ serialization = JSON
301
+ } = options
302
+ this.serialization = serialization
303
+
298
304
  this.objectObservables = new Map()
299
305
  this.rangeObservables = new Map()
300
306
  this.countObservables = new Map()
@@ -306,7 +312,7 @@ class Store {
306
312
  const json = this.tree.get(key)
307
313
  if(!json) return Promise.resolve(null)
308
314
  try {
309
- const obj = JSON.parse(json)
315
+ const obj = this.serialization.parse(json)
310
316
  return Promise.resolve(obj)
311
317
  } catch(e) {
312
318
  return Promise.reject(e)
@@ -338,7 +344,7 @@ class Store {
338
344
  if(range.gte && cursor.key < range.gte) break;
339
345
  if((!range.lt || cursor.key < range.lt) && (!range.lte || cursor.key <= range.lte)) {
340
346
  // key in range, skip keys outside range
341
- data.push(JSON.parse(cursor.value))
347
+ data.push(this.serialization.parse(cursor.value))
342
348
  }
343
349
  cursor.prev()
344
350
  }
@@ -353,7 +359,7 @@ class Store {
353
359
  if(range.lte && cursor.key > range.lte) break;
354
360
  if((!range.gt || cursor.key > range.gt) && (!range.gte || cursor.key >= range.gte)) {
355
361
  // key in range, skip keys outside range
356
- data.push(JSON.parse(cursor.value))
362
+ data.push(this.serialization.parse(cursor.value))
357
363
  }
358
364
  cursor.next()
359
365
  }
@@ -470,7 +476,7 @@ class Store {
470
476
  const key = keys[i]
471
477
  const json = tree.get(key)
472
478
  try {
473
- const obj = JSON.parse(json)
479
+ const obj = this.serialization.parse(json)
474
480
  this.tree = this.tree.remove(key)
475
481
  const rangeObservables = this.rangeObservablesTree.search([key, key])
476
482
  for (const rangeObservable of rangeObservables) {
@@ -488,11 +494,11 @@ class Store {
488
494
  const id = object.id
489
495
  if(typeof id != 'string') throw new Error(`ID is not string: ${JSON.stringify(id)}`)
490
496
  const oldObjectJson = this.tree.get(id)
491
- const oldObject = oldObjectJson && JSON.parse(oldObjectJson)
497
+ const oldObject = oldObjectJson && this.serialization.parse(oldObjectJson)
492
498
  if(oldObjectJson) {
493
499
  this.tree = this.tree.remove(id)
494
500
  }
495
- this.tree = this.tree.insert(id, JSON.stringify(object))
501
+ this.tree = this.tree.insert(id, this.serialization.stringify(object))
496
502
  const objectObservable = this.objectObservables.get(id)
497
503
  if (objectObservable) objectObservable.set(object, oldObject)
498
504
  const rangeObservables = this.rangeObservablesTree.search([id, id])
@@ -518,7 +524,7 @@ class Store {
518
524
  let object = null
519
525
  try {
520
526
  const json = this.tree.get(id)
521
- object = json ? JSON.parse(json) : null
527
+ object = json ? this.serialization.parse(json) : null
522
528
  this.tree = this.tree.remove(id)
523
529
  } catch(e) {
524
530
  console.error("FAILED REMOVE OF", id, e)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/db-store-rbtree",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "Database with observable data for live queries",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,5 +28,5 @@
28
28
  "@live-change/interval-tree": "^1.0.12",
29
29
  "functional-red-black-tree": "^1.0.1"
30
30
  },
31
- "gitHead": "f458425c609deeb9d1f38c15a3aa37759629a67e"
31
+ "gitHead": "9a1b104864c08f3e35b009f191889e3308e3eeb0"
32
32
  }