Bläddra i källkod

Introduce Source._is() for type-narrowing

customisations
alemart 2 månader sedan
förälder
incheckning
c4f84c3728

+ 6
- 4
src/core/session.ts Visa fil

@@ -514,12 +514,14 @@ export class Session extends AREventTarget<SessionEvent>
514 514
     {
515 515
         // prefer video sources
516 516
         for(let i = 0; i < sources.length; i++) {
517
-            if(sources[i]._type == 'video')
518
-                return sources[i] as VideoSource;
517
+            const source = sources[i];
518
+            if(source._is('video'))
519
+                return source;
519 520
         }
520 521
         for(let i = 0; i < sources.length; i++) {
521
-            if(sources[i]._type == 'canvas')
522
-                return sources[i] as CanvasSource;
522
+            const source = sources[i];
523
+            if(source._is('canvas'))
524
+                return source;
523 525
         }
524 526
 
525 527
         // emit warning

+ 11
- 1
src/sources/canvas-source.ts Visa fil

@@ -25,7 +25,7 @@ import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
25 25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26 26
 import { Utils, Nullable } from '../utils/utils';
27 27
 import { IllegalOperationError } from '../utils/errors';
28
-import { Source } from './source';
28
+import { Source, SourceType } from './source';
29 29
 
30 30
 /**
31 31
  * HTMLCanvasElement-based source of data
@@ -61,6 +61,16 @@ export class CanvasSource implements Source
61 61
     }
62 62
 
63 63
     /**
64
+     * Check if this source is of a certain type
65
+     * This is a convenient type-narrowing utility
66
+     * @internal
67
+     */
68
+    _is<T extends keyof SourceType>(type: T): this is SourceType[T]
69
+    {
70
+        return type === this._type;
71
+    }
72
+
73
+    /**
64 74
      * Get media
65 75
      * @internal
66 76
      */

+ 11
- 1
src/sources/pointer-source.ts Visa fil

@@ -22,7 +22,7 @@
22 22
 
23 23
 import Speedy from 'speedy-vision';
24 24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
-import { Source } from './source';
25
+import { Source, SourceType } from './source';
26 26
 import { Viewport } from '../core/viewport'
27 27
 import { Utils, Nullable } from '../utils/utils';
28 28
 
@@ -61,6 +61,16 @@ export class PointerSource implements Source
61 61
     }
62 62
 
63 63
     /**
64
+     * Check if this source is of a certain type
65
+     * This is a convenient type-narrowing utility
66
+     * @internal
67
+     */
68
+    _is<T extends keyof SourceType>(type: T): this is SourceType[T]
69
+    {
70
+        return type === this._type;
71
+    }
72
+
73
+    /**
64 74
      * Consume a pointer event
65 75
      * @returns the next pointer event to be consumed, or null if there are none
66 76
      * @internal

+ 17
- 1
src/sources/source.ts Visa fil

@@ -21,6 +21,9 @@
21 21
  */
22 22
 
23 23
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
24
+import { CanvasSource } from './canvas-source';
25
+import { PointerSource } from './pointer-source';
26
+import { VideoSource } from './video-source';
24 27
 
25 28
 /**
26 29
  * Abstract source of data
@@ -30,6 +33,9 @@ export interface Source
30 33
     /** @internal type-identifier of the source of data */
31 34
     readonly _type: string;
32 35
 
36
+    /** @internal check if this source is of a certain type - this is a convenient type-narrowing utility */
37
+    _is<T extends keyof SourceType>(type: T): this is SourceType[T];
38
+
33 39
     /** @internal method to initialize the source of data (gets the data ready) */
34 40
     _init(): SpeedyPromise<void>;
35 41
 
@@ -38,4 +44,14 @@ export interface Source
38 44
 
39 45
     /** @internal stats related to this source of data */
40 46
     readonly _stats: string;
41
-}
47
+}
48
+
49
+/**
50
+ * A helper for type-narrowing
51
+ * @internal
52
+ */
53
+export type SourceType = {
54
+    'video': VideoSource,
55
+    'canvas': CanvasSource,
56
+    'pointer-source': PointerSource
57
+};

+ 11
- 1
src/sources/video-source.ts Visa fil

@@ -25,7 +25,7 @@ import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
25 25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26 26
 import { Utils, Nullable } from '../utils/utils';
27 27
 import { IllegalOperationError, NotSupportedError, TimeoutError } from '../utils/errors';
28
-import { Source } from './source';
28
+import { Source, SourceType } from './source';
29 29
 
30 30
 /** A message to be displayed if a video can't autoplay and user interaction is required */
31 31
 const ALERT_MESSAGE = 'Tap on the screen to start';
@@ -76,6 +76,16 @@ export class VideoSource implements Source
76 76
     }
77 77
 
78 78
     /**
79
+     * Check if this source is of a certain type
80
+     * This is a convenient type-narrowing utility
81
+     * @internal
82
+     */
83
+    _is<T extends keyof SourceType>(type: T): this is SourceType[T]
84
+    {
85
+        return type === this._type;
86
+    }
87
+
88
+    /**
79 89
      * Get media
80 90
      * @internal
81 91
      */

+ 4
- 4
src/trackers/image-tracker/image-tracker.ts Visa fil

@@ -271,12 +271,12 @@ export class ImageTracker extends AREventTarget<ImageTrackerEvent> implements Tr
271 271
         // XXX also let the user specify a source manually?
272 272
         for(const source of session.sources) {
273 273
             // prefer video sources
274
-            if(source._type == 'video') {
275
-                this._source = source as VideoSource;
274
+            if(source._is('video')) {
275
+                this._source = source;
276 276
                 break;
277 277
             }
278
-            else if(source._type == 'canvas')
279
-                this._source = source as CanvasSource;
278
+            else if(source._is('canvas'))
279
+                this._source = source;
280 280
         }
281 281
         if(this._source === null)
282 282
             throw new IllegalOperationError('The image tracker requires a suitable source of data');

+ 2
- 2
src/trackers/pointer-tracker/pointer-tracker.ts Visa fil

@@ -218,8 +218,8 @@ export class PointerTracker implements Tracker
218 218
 
219 219
         // find the pointer source
220 220
         for(const source of session.sources) {
221
-            if(source._type == 'pointer-source') {
222
-                this._source = source as PointerSource;
221
+            if(source._is('pointer-source')) {
222
+                this._source = source;
223 223
                 break;
224 224
             }
225 225
         }

Laddar…
Avbryt
Spara