Skip to content

Commit

Permalink
fix(VtkWebMouseListener): Throttle events instead of waiting for the …
Browse files Browse the repository at this point in the history
…server to respond
  • Loading branch information
jourdain committed Feb 20, 2018
1 parent 437f00f commit da5c8ad
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Interaction/Core/VtkWebMouseListener/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const NoOp = () => {};
export default class VtkMouseListener {
constructor(vtkWebClient, width = 100, height = 100, viewId = -1) {
this.client = vtkWebClient;
this.ready = true;
this.lastEventTime = Date.now();
this.throttleTime = 16.6; // ms => 30 fps
this.width = width;
this.height = height;
this.viewId = viewId;
Expand Down Expand Up @@ -50,7 +51,11 @@ export default class VtkMouseListener {
this.emit(INTERATION_TOPIC, true);
}
if (this.client) {
if (this.ready || vtkEvent.action !== 'move') {
const tNow = Date.now();
if (
tNow > this.lastEventTime + this.throttleTime ||
vtkEvent.action !== 'move'
) {
// Make sure we only send the last down or first up before/after a move
if (vtkEvent.action !== 'move' && this.lastEvent) {
// eat first down action
Expand All @@ -71,10 +76,9 @@ export default class VtkMouseListener {
}
}
this.lastEvent = vtkEvent;
this.ready = false;
this.lastEventTime = tNow;
this.client.MouseHandler.interaction(vtkEvent).then(
(resp) => {
this.ready = true;
this.doneCallback(vtkEvent.action !== 'up');
},
(err) => {
Expand Down Expand Up @@ -115,15 +119,14 @@ export default class VtkMouseListener {
this.emit(INTERATION_TOPIC, true);
}
if (this.client) {
if (this.ready || vtkEvent.action !== 'move') {
this.ready = false;
const tNow = Date.now();
if (tNow > this.lastEventTime + this.throttleTime || vtkEvent.action !== 'move') {
this.lastEventTime = tNow;
this.client.MouseHandler.interaction(vtkEvent).then(
(resp) => {
this.ready = true;
this.doneCallback(vtkEvent.action !== 'up');
},
(err) => {
this.ready = true;
this.doneCallback(vtkEvent.action !== 'up');
}
);
Expand All @@ -144,6 +147,14 @@ export default class VtkMouseListener {
this.doneCallback = callback || NoOp;
}

setThrottleTime(tTime = 16.6) {
this.throttleTime = tTime;
}

getThrottleTime() {
return this.throttleTime;
}

updateSize(w, h) {
this.width = w;
this.height = h;
Expand Down

0 comments on commit da5c8ad

Please sign in to comment.