1 import assert from 'assert'
2 import LazyValue from './LazyValue.mjs'
4 class LazyObject extends LazyValue {
5 constructor(transaction, ptr_len, object) {
6 super(transaction, ptr_len)
7 this.object = object || {}
11 assert(typeof key === 'string')
12 return Object.prototype.hasOwnProperty.call(this.object, key)
15 async get(key, default_value) {
16 assert(typeof key === 'string')
17 if (!Object.prototype.hasOwnProperty.call(this.object, key)) {
18 if (default_value === undefined)
20 this.set(key, this.transaction.json_to_logjson(default_value))
23 let value = this.object[key]
24 if (value instanceof Array) {
25 value = await this.transaction.read(value)
26 this.object[key] = value
32 assert(typeof key === 'string')
34 typeof value !== 'object' ||
36 (value instanceof LazyValue && value.transaction === this.transaction)
38 this.ptr_len = null // mark dirty
39 this.object[key] = value
44 this.ptr_len = null // mark dirty
45 delete this.object[key]
51 for (let i in this.object)
57 for (let i in this.object) {
58 let value = this.object[i]
59 if (value instanceof LazyValue) {
60 if (await value.commit())
61 this.ptr_len = null // mark dirty
62 this.object[i] = value.ptr_len
66 if (this.ptr_len === null) {
67 this.ptr_len = await this.transaction.database.write(this.object)
74 export default LazyObject