improve indexdb promised. fix multiple spaces access. add workspace persist + restore for mesh

This commit is contained in:
Stewart Allen 2022-01-03 17:13:50 -05:00
commit 96145b370b
9 changed files with 192 additions and 64 deletions

View file

@ -191,8 +191,8 @@ let group = {
},
// @param group {MeshModel[]}
new(models) {
return group.add(new mesh.group(models));
new(models, id) {
return group.add(new mesh.group(models, id));
},
// @param group {MeshGroup}

View file

@ -26,8 +26,8 @@ let worker = moto.client.fn;
mesh.group = class MeshGroup extends mesh.object {
// @param group {MeshModel[]}
constructor(models = []) {
super();
constructor(models = [], id) {
super(id);
this.group = new THREE.Group();
this.models = [];
for (let model of models) {
@ -51,11 +51,14 @@ mesh.group = class MeshGroup extends mesh.object {
space.update();
call.model_add({model, group:this});
worker.group_add({id: this.id, model:model.id});
// update data store
mesh.db.space.put(this.id, this.models.map(m => m.id));
return this;
}
// @param model {MeshModel}
remove(model) {
// console.log('group',this,'remove',model);
// remove all models and group
if (arguments.length === 0) {
for (let m of this.models.slice()) {
@ -63,18 +66,24 @@ mesh.group = class MeshGroup extends mesh.object {
}
return;
}
model.group = undefined;
this.models.remove(model);
this.group.remove(model.mesh);
// create message for listeners
call.model_remove({model, group:this});
// trigger sync with worker
worker.group_remove({id: this.id, model:model.id});
worker.group_remove({id: this.id, model: model.id});
// ensure worker cleanup of model
model.remove(true);
// auto-remove group when empty
if (this.group.children.length === 0) {
mesh.api.group.remove(this);
// update worker state
worker.object_destroy({id: this.id});
// update object store
mesh.db.space.remove(this.id);
} else {
// update data store
mesh.db.space.put(this.id, this.models.map(m => m.id));
}
space.update();
return this;

View file

@ -16,6 +16,7 @@ gapp.register("mesh.model", [
let mesh = self.mesh = self.mesh || {};
if (mesh.model) return;
let mapp = mesh;
let space = moto.Space;
let worker = moto.client.fn;
@ -41,8 +42,8 @@ let materials = mesh.material = {
/** 3D model rendered on plaform **/
mesh.model = class MeshModel extends mesh.object {
constructor(data) {
super();
constructor(data, id) {
super(id);
let { file, mesh } = data;
if (!mesh) {
@ -57,6 +58,9 @@ mesh.model = class MeshModel extends mesh.object {
this.file = file || 'unnamed';
this.mesh = this.load(mesh);
// persist in db so it can be restored on page load
mapp.db.space.put(this.id, { file, mesh });
}
type() {
@ -179,9 +183,16 @@ mesh.model = class MeshModel extends mesh.object {
remove() {
if (arguments.length === 0) {
// direct call requires pass through group
this.group.remove(this);
this.group = undefined;
this.removed = 'pending';
} else {
// called from group
// update worker state
worker.object_destroy({id: this.id});
// update object store
mesh.db.space.remove(this.id);
// tag removed for debugging
this.removed = 'complete';
}
}
};

View file

@ -18,8 +18,8 @@ let worker = moto.client.fn;
mesh.object = class MeshObject {
constructor() {
this.id = mesh.util.uuid();
constructor(id) {
this.id = id || mesh.util.uuid();
worker.object_create({id: this.id, type: this.type()});
}

View file

@ -27,11 +27,6 @@ function cacheUpdate(id, data) {
Object.assign(cache[id], data);
}
worker.bind("debug", (data, send) => {
console.log({work_cache: cache});
send.done();
});
let model = {
load(data) {
let { vertices, name, id } = data;
@ -93,10 +88,16 @@ let object = {
}
};
worker.bind("debug", (data, send) => {
console.log({work_cache: cache});
});
worker.bindObject({
model,
group,
object
});
worker.ready();
})();