add experimental promises + concurrent shell offsetting

This commit is contained in:
Stewart Allen 2021-06-20 00:25:35 -04:00
commit a8973dbef8
7 changed files with 97 additions and 35 deletions

View file

@ -231,14 +231,27 @@
});
KIRI.Top.prototype.encode = function(state) {
return {
let obj = {
type: 'top',
poly: encode(this.poly, state),
poly: encode(this.poly, state)
};
if (state.full) {
obj.last = encode(this.last, state);
obj.shells = encode(this.shells, state);
obj.fill_off = encode(this.fill_off, state);
obj.fill_lines = encode(this.fill_lines, state);
}
return obj;
};
registerDecoder('top', function(v, state) {
let top = KIRI.newTop(decode(v.poly, state));
if (state.full) {
top.last = decode(v.last, state);
top.shells = decode(v.shells, state);
top.fill_off = decode(v.fill_off, state);
top.fill_lines = decode(v.fill_lines, state);
}
return top;
});

View file

@ -13,20 +13,30 @@ self.alert = function(o) {
console.log(o);
};
function reply(msg) {
self.postMessage(msg);
}
self.onmessage = function(msg) {
let data = msg.data;
switch (data.cmd) {
case "union":
if (!(data.polys && data.polys.length)) {
self.postMessage({union: CODEC.encode([])});
reply({union: CODEC.encode([])});
return;
}
let polys = CODEC.decode(data.polys);
let union = POLY.union(polys, data.minarea || 0, true);
self.postMessage({union: CODEC.encode(union)});
reply({union: CODEC.encode(union)});
break;
case "top.shells":
let top = CODEC.decode(data.top, {full: true});
let {z, count, offset1, offsetN, fillOffset, opt} = data;
KIRI.driver.FDM.share.doTopShells(z, top, count, offset1, offsetN, fillOffset, opt);
reply({top: CODEC.encode(top, {full: true})});
break;
default:
self.postMessage({error: "invalid command"});
reply({error: "invalid command"});
break;
}
};

View file

@ -32,6 +32,7 @@ if (concurrent) {
}
// for concurrent operations
const minwork =
KIRI.minions = {
union: function(polys, minarea) {
return new Promise((resolve, reject) => {
@ -41,30 +42,27 @@ KIRI.minions = {
let polyper = Math.ceil(polys.length / concurrent);
let running = 0;
let union = [];
let receiver = function(msg) {
let polys = KIRI.codec.decode(msg.data.union);
let receiver = function(data) {
let polys = KIRI.codec.decode(data.union);
union.appendAll(polys);
if (--running === 0) {
resolve(POLY.union(union, 0, true));
}
};
for (let i=0; i<polys.length; i += polyper) {
minions[running].onmessage = receiver;
minions[running].postMessage({
running++;
minwork.queue({
cmd: "union",
minarea,
polys: KIRI.codec.encode(polys.slice(i, i + polyper))
});
running++;
}, receiver);
}
});
},
queue: function(work) {
return new Promise((resolve, reject) => {
minionq.push({work, resolve, reject});
kick();
});
queue: function(work, ondone) {
minionq.push({work, ondone});
minwork.kick();
},
kick: function() {
@ -72,10 +70,11 @@ KIRI.minions = {
let qrec = minionq.shift();
let minion = minions.shift();
minion.onmessage = (msg) => {
qrec.resolve(msg.data);
minions.push(minion);
qrec.ondone(msg.data);
minwork.kick();
};
minion.postMessage(work);
minion.postMessage(qrec.work);
}
}
};