Skip to content

Commit

Permalink
allow for only a single raf message, until callbacks execute
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Jul 30, 2019
1 parent 9043f24 commit 358b822
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions components/script/dom/xrsession.rs
Expand Up @@ -285,6 +285,9 @@ impl XRSessionMethods for XRSession {

/// https://immersive-web.github.io/webxr/#dom-xrsession-requestanimationframe
fn RequestAnimationFrame(&self, callback: Rc<XRFrameRequestCallback>) -> i32 {
// We only need to send a message once, until a raf callback executes.
let should_send = self.raf_callback_list.borrow().is_empty();

// queue up RAF callback, obtain ID
let raf_id = self.next_raf_id.get();
self.next_raf_id.set(raf_id + 1);
Expand Down Expand Up @@ -315,10 +318,21 @@ impl XRSessionMethods for XRSession {
}),
);
}
let sender = self.raf_sender.borrow().clone().unwrap();

// request animation frame
self.session.borrow_mut().request_animation_frame(sender);
if should_send {
// If our callback list is empty, it either means this is the first request,
// or raf_callback executed, in which case we should
// send a message to request an animation frame.
//
// This prevents multiple messages being sent for a single call to raf_callback,
// and multiple message are unnecessary,
// since one call will already deal with multiple potentially enqueued callbacks.
//
// Allowing multiple messages could keep the main-thread,
// where the session thread might be running, looping on incoming messages.
let sender = self.raf_sender.borrow().clone().unwrap();
self.session.borrow_mut().request_animation_frame(sender);
}

raf_id
}
Expand Down

0 comments on commit 358b822

Please sign in to comment.