Procházet zdrojové kódy

Ignore calls to Session.requestAnimationFrame() if the session has been ended

customisations
alemart před 11 měsíci
rodič
revize
59d8524414
2 změnil soubory, kde provedl 16 přidání a 6 odebrání
  1. 10
    3
      docs/api/session.md
  2. 6
    3
      src/core/session.ts

+ 10
- 3
docs/api/session.md Zobrazit soubor

@@ -66,10 +66,12 @@ A reference to the [Gizmos](gizmos.md) object.
66 66
 
67 67
 Schedules a call to the `callback` function, which is intended to update and render the virtual scene. Your `callback` function must itself call `session.requestAnimationFrame()` again in order to continue to update and render the virtual scene.
68 68
 
69
-!!! info "Note"
69
+!!! info "Notes"
70 70
 
71 71
     `session.requestAnimationFrame()` is analogous to `window.requestAnimationFrame()`, but they are not the same! The former is a call to the WebAR engine, whereas the latter is a standard call to the web browser.
72 72
 
73
+    This call will be ignored and an invalid handle will be returned if the session has been [ended](#ended) (*since 0.3.0*). Previously, it would raise an exception.
74
+
73 75
 **Arguments**
74 76
 
75 77
 * `callback: function`. A function that receives two parameters:
@@ -83,15 +85,20 @@ A handle.
83 85
 **Example**
84 86
 
85 87
 ```js
88
+//
89
+// This is the animation loop:
90
+//
91
+
86 92
 function animate(time, frame)
87 93
 {
88 94
     // update and render the virtual scene
89 95
     // ...
90 96
 
91
-    // repeat the call
97
+    // repeat
92 98
     session.requestAnimationFrame(animate);
93 99
 }
94 100
 
101
+// start the animation loop
95 102
 session.requestAnimationFrame(animate);
96 103
 ```
97 104
 
@@ -103,7 +110,7 @@ Cancels an animation frame request.
103 110
 
104 111
 **Arguments**
105 112
 
106
-* `handle: SessionRequestAnimationFrameHandle`. A handle returned by `session.requestAnimationFrame()`.
113
+* `handle: SessionRequestAnimationFrameHandle`. A handle returned by `session.requestAnimationFrame()`. If the handle is invalid, this method does nothing.
107 114
 
108 115
 ### end
109 116
 

+ 6
- 3
src/core/session.ts Zobrazit soubor

@@ -418,10 +418,13 @@ export class Session extends AREventTarget<SessionEventType>
418 418
     {
419 419
         const handle: SessionRequestAnimationFrameHandle = Symbol('raf-handle');
420 420
 
421
-        if(this._active)
421
+        if(this._active) {
422 422
             this._rafQueue.push([ handle, callback ]);
423
-        else
424
-            throw new IllegalOperationError(`Can't requestAnimationFrame(): session ended.`);
423
+        }
424
+        else {
425
+            // if the session is inactive, we simply ignore this call
426
+            // this is friendly behavior, since RAF is used in animation loops
427
+        }
425 428
 
426 429
         return handle;
427 430
     }

Načítá se…
Zrušit
Uložit