See if this works: on('ready',()=>{
const toFrontFixed = (()=>{
let queue=[];
let last=0;
const DELAY = 1000;
const burndownQueue = ()=>{
let o = queue.shift();
toFront(o);
last = Date.now();
if(queue.length){
setTimeout(burndownQueue,DELAY);
}
};
return (obj=>{
if(queue.length){
queue.push(obj);
} else {
let t = Date.now();
if(last+DELAY > t){
queue.push(obj);
setTimeout(burndownQueue,(last+DELAY-t));
} else {
toFront(obj);
last = t;
}
}
});
})();
on('change:page:_zorder',(page)=>{
findObjs({type:'graphic',pageid:page.id})
.filter(g=>g.get('represents').length)
.filter(t=>parseInt((findObjs({type:'attribute',characterid:t.get('represents'),name:'priority_yes'})[0]||{get:()=>'0'}).get('current'))>0)
.forEach(toFrontFixed)
;
});
});
There are a few problems with doing this: 1) What is at the front is not completely under the control of the API, or the client in general. If a player controls a token, that graphic will always appear for them above anything else. 2) ToFront() has a horrible race condition where calling it on multiple objects will only work on some of them as they each overwrite the same field on the page object. To avoid this, you have to defer each call with 1000ms between (which is what the toFrontFixed() function in the above does). 3) The order is actually controlled in the page's _zorder field, so observing a graphic you won't see an event for an ordering change. I've not tested the above, but it should work... (crosses fingers), but will take a minimum of 1 second per priority token.