@live-change/db
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -76,3 +76,4 @@ function getProperty(of, propertyName) { | ||
class WriteQueue { | ||
constructor(store, id) { | ||
constructor(atomicWriter, store, id) { | ||
this.atomicWriter = atomicWriter | ||
this.store = store | ||
@@ -87,2 +88,9 @@ this.id = id | ||
tryDeleteQueue() { | ||
if(this.operations.length == 0) { | ||
//console.log("DELETE QUEUE", this.id) | ||
this.atomicWriter.writes.delete(this.id) | ||
} | ||
} | ||
async put(object) { | ||
@@ -93,2 +101,3 @@ this.operations = [] | ||
this.writeValue = object | ||
this.writePromise.then(ok => this.tryDeleteQueue()) | ||
return this.writePromise | ||
@@ -102,24 +111,35 @@ } | ||
this.writeValue = null | ||
this.writePromise.then(ok => this.tryDeleteQueue()) | ||
return this.writePromise | ||
} | ||
update(operations) { | ||
async update(operations) { | ||
const first = this.operations.length == 0 | ||
this.operations = this.operations.concat(operations) | ||
//console.log("QUEUE UPDATE", this.operations) | ||
if(first) { | ||
//console.log("QUEUE UPDATE DO!") | ||
if(this.writePromise) { | ||
this.updatePromise = this.writePromise.then(written => { | ||
//console.log("GOT WRITE PROMISE") | ||
this.updatePromise = this.writePromise.then(async written => { | ||
if(this.operations == []) return | ||
const value = mutate(this.writeValue || {}, this.operations) | ||
this.operations = [] | ||
this.put(value) | ||
await this.put(value) | ||
return [value, written] | ||
}) | ||
} else if(!this.readPromise) { | ||
//console.log("READING STARTED") | ||
this.readPromise = this.store.objectGet(this.id) | ||
this.updatePromise = this.readPromise.then(readed => { | ||
this.updatePromise = this.readPromise.then(async readed => { | ||
this.readPromise = null | ||
//console.log("READED DOING", this.operations) | ||
if(this.operations == []) return | ||
const value = mutate(readed || {}, this.operations) | ||
this.operations = [] | ||
this.put(value) | ||
await this.put(value) | ||
return [value, readed] | ||
}) | ||
} else { | ||
//console.log("WAITING FOR READ") | ||
} | ||
@@ -164,3 +184,3 @@ } | ||
if(!queue) { | ||
queue = new WriteQueue(this.store, id) | ||
queue = new WriteQueue(this, this.store, id) | ||
this.writes.set(id, queue) | ||
@@ -174,3 +194,3 @@ } | ||
if(!queue) { | ||
queue = new WriteQueue(this.store, id) | ||
queue = new WriteQueue(this, this.store, id) | ||
this.writes.set(id, queue) | ||
@@ -182,5 +202,7 @@ } | ||
update(id, operations) { | ||
//console.log("AW Update", id, operations) | ||
let queue = this.writes.get(id) | ||
if(!queue) { | ||
queue = new WriteQueue(this.store, id) | ||
//console.log("NEW QUEUE") | ||
queue = new WriteQueue(this, this.store, id) | ||
this.writes.set(id, queue) | ||
@@ -187,0 +209,0 @@ } |
@@ -66,2 +66,3 @@ const IntervalTree = require('node-interval-tree').default | ||
change(obj, oldObj) { | ||
if(!(obj || oldObj)) return | ||
const id = obj ? obj.id : oldObj.id | ||
@@ -68,0 +69,0 @@ const objectReader = this.objectReaders.get(id) |
@@ -39,3 +39,5 @@ | ||
let object = await this.store.delete(id) | ||
for(let output of this.outputs) output({ type: 'delete', object }) | ||
if(object) { | ||
for(let output of this.outputs) output({ type: 'delete', object }) | ||
} | ||
return object | ||
@@ -42,0 +44,0 @@ } |
{ | ||
"name": "@live-change/db", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Database with observable data for live queries", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
67042
2062