diff Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp @ 1618:9a52bac0c2a7

Added code to clear pending calls to RequestAnimationFrame
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 02 Nov 2020 17:54:18 +0100
parents 5887a4f8594b
children 575f512cdf48
line wrap: on
line diff
--- a/Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp	Fri Oct 30 17:43:29 2020 +0100
+++ b/Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp	Mon Nov 02 17:54:18 2020 +0100
@@ -129,6 +129,7 @@
     if (that->compositor_.get() != NULL &&
         that->controller_ /* should always be true */)
     {
+      that->animationFrameCallbackIds_.clear();
       that->Paint(*that->compositor_, *that->controller_);
     }
       
@@ -213,7 +214,9 @@
 
   void WebAssemblyViewport::Invalidate()
   {
-    emscripten_request_animation_frame(OnRequestAnimationFrame, reinterpret_cast<void*>(this));
+    long id = emscripten_request_animation_frame(OnRequestAnimationFrame, 
+                                                 reinterpret_cast<void*>(this));
+    animationFrameCallbackIds_.push_back(id);
   }
 
   void WebAssemblyViewport::FitForPrint()
@@ -320,6 +323,13 @@
 
   WebAssemblyViewport::~WebAssemblyViewport()
   {
+    // cancel the pending RequestAnimationFrame callbacks
+    for(size_t i = 0; i < animationFrameCallbackIds_.size(); ++i)
+    {
+      long id = animationFrameCallbackIds_[i];
+      emscripten_cancel_animation_frame(id);
+    }
+
     emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
                                    reinterpret_cast<void*>(this),
                                    false,