refactor ui builder to use util.defer

This commit is contained in:
Stewart Allen 2022-01-03 10:50:59 -05:00
commit a5cf4a7a87
2 changed files with 38 additions and 37 deletions

View file

@ -62,50 +62,44 @@ function ui_build() {
let { grouplist } = bound; let { grouplist } = bound;
let api = mesh.api; let api = mesh.api;
let maker = { function build() {
update() { // map groups to divs
clearTimeout(maker.timer); let groups = api.group.list()
maker.timer = setTimeout(maker.do, 10); .map(g => h.div([
}, h.button({
do() { _: `group`,
let groups = api.group.list() title: g.id,
// map groups to divs class: [ "group",
.map(g => h.div([ api.selection.contains(g) ? 'selected' : undefined
h.button({ ],
_: `group`, onclick() { api.selection.toggle(g); }
title: g.id, }),
class: [ h.div({ class: "vsep" }),
"group", h.div({ class: "models"},
api.selection.contains(g) ? 'selected' : undefined // map models to buttons
], g.models.map(m => h.button({
onclick() { api.selection.toggle(g); } _: m.file || m.id,
}), class: api.selection.contains(m) ? [ 'selected' ] : [],
h.div({ class: "vsep" }), onclick() { api.selection.toggle(m); }
h.div({ class: "models"}, }))
// map models to buttons )
g.models.map(m => h.button({ ]));
_: m.file || m.id, h.bind(grouplist, groups);
class: api.selection.contains(m) ? [ 'selected' ] : [], }
onclick() { api.selection.toggle(m); }
}))
)
]));
h.bind(grouplist, groups);
}
};
function update(object, topic) { function update(object, topic) {
console.log({update: object, topic}); console.log({update: object, topic});
} }
// listen for api calls // listen for api calls
// todo: switch to util.defer(fn) // create a deferred wrapper to merge multiple rapid events
let deferred = mesh.util.deferWrap(build);
broker.listeners({ broker.listeners({
model_add: maker.update, model_add: deferred,
group_add: maker.update, group_add: deferred,
model_remove: maker.update, model_remove: deferred,
group_remove: maker.update, group_remove: deferred,
selection_update: maker.update, selection_update: deferred,
}) })
} }

View file

@ -282,6 +282,13 @@ let util = mesh.util = {
deferFn.push(rec); deferFn.push(rec);
}, },
// return a function wrapper which can be re-used
deferWrap(fn, time) {
return function() {
util.defer(fn, time);
}
},
// @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]} // @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]}
// @returns bounds modified for moto.Space // @returns bounds modified for moto.Space
bounds(object) { bounds(object) {