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,20 +62,14 @@ function ui_build() {
let { grouplist } = bound; let { grouplist } = bound;
let api = mesh.api; let api = mesh.api;
let maker = { function build() {
update() {
clearTimeout(maker.timer);
maker.timer = setTimeout(maker.do, 10);
},
do() {
let groups = api.group.list()
// map groups to divs // map groups to divs
let groups = api.group.list()
.map(g => h.div([ .map(g => h.div([
h.button({ h.button({
_: `group`, _: `group`,
title: g.id, title: g.id,
class: [ class: [ "group",
"group",
api.selection.contains(g) ? 'selected' : undefined api.selection.contains(g) ? 'selected' : undefined
], ],
onclick() { api.selection.toggle(g); } onclick() { api.selection.toggle(g); }
@ -92,20 +86,20 @@ function ui_build() {
])); ]));
h.bind(grouplist, groups); 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) {