Sending change signal on delete

This commit is contained in:
Fabian Stamm 2019-10-30 16:06:28 +01:00
parent 15df0351a4
commit a78e98a0c8
2 changed files with 36 additions and 37 deletions

View File

@ -139,48 +139,49 @@ export default class Query {
} }
async set(value: any) { async set(value: any) {
if (value === null || value === undefined)
return this.delete(value);
const lock = await this.database.locks.lock(this.path); const lock = await this.database.locks.lock(this.path);
let batch = this.database.level.batch(); let batch = this.database.level.batch();
try { try {
let field = await this.getField(this.path); if (value === null || value === undefined) {
if (field) { this.delete(value);
await this.delete(batch);
} else { } else {
for (let i = 0; i < this.path.length; i++) { let field = await this.getField(this.path);
let subpath = this.path.slice(0, i); if (field) {
let field = await this.getField(subpath); await this.delete(batch);
if (!field) {
batch.put(this.pathToKey(subpath), FieldEncoder.encode({
type: FieldTypes.OBJECT
}));
} else if (field.type !== FieldTypes.OBJECT) {
throw new Error("Parent elements not all Object. Cannot set value!");
}
}
}
const saveValue = (path: string[], value: any) => {
if (typeof value === "object") {
//TODO: Handle case array!
// Field type array?
batch.put(this.pathToKey(path), FieldEncoder.encode({
type: FieldTypes.OBJECT
}))
for (let field in value) {
saveValue([...path, field], value[field]);
}
} else { } else {
batch.put(this.pathToKey(path), FieldEncoder.encode({ for (let i = 0; i < this.path.length; i++) {
type: FieldTypes.VALUE, let subpath = this.path.slice(0, i);
value let field = await this.getField(subpath);
})); if (!field) {
batch.put(this.pathToKey(subpath), FieldEncoder.encode({
type: FieldTypes.OBJECT
}));
} else if (field.type !== FieldTypes.OBJECT) {
throw new Error("Parent elements not all Object. Cannot set value!");
}
}
} }
}
saveValue(this.path, value); const saveValue = (path: string[], value: any) => {
if (typeof value === "object") {
//TODO: Handle case array!
// Field type array?
batch.put(this.pathToKey(path), FieldEncoder.encode({
type: FieldTypes.OBJECT
}))
for (let field in value) {
saveValue([...path, field], value[field]);
}
} else {
batch.put(this.pathToKey(path), FieldEncoder.encode({
type: FieldTypes.VALUE,
value
}));
}
}
saveValue(this.path, value);
}
await batch.write(); await batch.write();
this.database.changeObservable.send({ this.database.changeObservable.send({

View File

@ -36,8 +36,6 @@ Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
}); });
const formsTemplate = Handlebars.compile(readFileSync("./views/forms.hbs").toString());
const cache = new Map<string, Handlebars.TemplateDelegate>(); const cache = new Map<string, Handlebars.TemplateDelegate>();
export default function getTemplate(name: string) { export default function getTemplate(name: string) {