Skip to content

Commit

Permalink
move throttle_function to Bonito
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Jun 20, 2024
1 parent 225d0ae commit 6efba2a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 61 deletions.
22 changes: 2 additions & 20 deletions WGLMakie/src/wglmakie.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -22773,24 +22773,6 @@ function start_renderloop(three_scene) {
render_scene(three_scene);
renderloop();
}
function throttle_function(func, delay) {
let prev = 0;
let future_id = undefined;
function inner_throttle(...args) {
const now = new Date().getTime();
if (future_id !== undefined) {
clearTimeout(future_id);
future_id = undefined;
}
if (now - prev > delay) {
prev = now;
return func(...args);
} else {
future_id = setTimeout(()=>inner_throttle(...args), now - prev + 1);
}
}
return inner_throttle;
}
function get_body_size() {
const bodyStyle = window.getComputedStyle(document.body);
const width_padding = parseInt(bodyStyle.paddingLeft, 10) + parseInt(bodyStyle.paddingRight, 10) + parseInt(bodyStyle.marginLeft, 10) + parseInt(bodyStyle.marginRight, 10);
Expand Down Expand Up @@ -22870,7 +22852,7 @@ function add_canvas_events(screen, comm, resize_to) {
]
});
}
const notify_mouse_throttled = throttle_function(mouse_callback, 40);
const notify_mouse_throttled = Bonito.throttle_function(mouse_callback, 40);
function mousemove(event) {
notify_mouse_throttled(event);
return false;
Expand Down Expand Up @@ -22941,7 +22923,7 @@ function add_canvas_events(screen, comm, resize_to) {
});
}
if (resize_to) {
const resize_callback_throttled = throttle_function(resize_callback, 100);
const resize_callback_throttled = Bonito.throttle_function(resize_callback, 100);
window.addEventListener("resize", (event)=>resize_callback_throttled());
resize_callback_throttled();
}
Expand Down
43 changes: 2 additions & 41 deletions WGLMakie/src/wglmakie.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,6 @@ function start_renderloop(three_scene) {
renderloop();
}

// from: https://www.geeksforgeeks.org/javascript-throttling/
function throttle_function(func, delay) {
// Previously called time of the function
let prev = 0;
// ID of queued future update
let future_id = undefined;
function inner_throttle(...args) {
// Current called time of the function
const now = new Date().getTime();

// If we had a queued run, clear it now, we're
// either going to execute now, or queue a new run.
if (future_id !== undefined) {
clearTimeout(future_id);
future_id = undefined;
}

// If difference is greater than delay call
// the function again.
if (now - prev > delay) {
prev = now;
// "..." is the spread operator here
// returning the function with the
// array of arguments
return func(...args);
} else {
// Otherwise, we want to queue this function call
// to occur at some later later time, so that it
// does not get lost; we'll schedule it so that it
// fires just a bit after our choke ends.
future_id = setTimeout(
() => inner_throttle(...args),
now - prev + 1
);
}
}
return inner_throttle;
}

function get_body_size() {
const bodyStyle = window.getComputedStyle(document.body);
// Subtract padding that is added by VSCode
Expand Down Expand Up @@ -239,7 +200,7 @@ function add_canvas_events(screen, comm, resize_to) {
comm.notify({ mouseposition: [x, y] });
}

const notify_mouse_throttled = throttle_function(mouse_callback, 40);
const notify_mouse_throttled = Bonito.throttle_function(mouse_callback, 40);

function mousemove(event) {
notify_mouse_throttled(event);
Expand Down Expand Up @@ -318,7 +279,7 @@ function add_canvas_events(screen, comm, resize_to) {
comm.notify({ resize: [width / winscale, height / winscale] });
}
if (resize_to) {
const resize_callback_throttled = throttle_function(
const resize_callback_throttled = Bonito.throttle_function(
resize_callback,
100
);
Expand Down

0 comments on commit 6efba2a

Please sign in to comment.