Browse Source

Introduce Source._is() for type-narrowing

customisations
alemart 2 months ago
parent
commit
c4f84c3728

+ 6
- 4
src/core/session.ts View File

514
     {
514
     {
515
         // prefer video sources
515
         // prefer video sources
516
         for(let i = 0; i < sources.length; i++) {
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
         for(let i = 0; i < sources.length; i++) {
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
         // emit warning
527
         // emit warning

+ 11
- 1
src/sources/canvas-source.ts View File

25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26
 import { Utils, Nullable } from '../utils/utils';
26
 import { Utils, Nullable } from '../utils/utils';
27
 import { IllegalOperationError } from '../utils/errors';
27
 import { IllegalOperationError } from '../utils/errors';
28
-import { Source } from './source';
28
+import { Source, SourceType } from './source';
29
 
29
 
30
 /**
30
 /**
31
  * HTMLCanvasElement-based source of data
31
  * HTMLCanvasElement-based source of data
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
      * Get media
74
      * Get media
65
      * @internal
75
      * @internal
66
      */
76
      */

+ 11
- 1
src/sources/pointer-source.ts View File

22
 
22
 
23
 import Speedy from 'speedy-vision';
23
 import Speedy from 'speedy-vision';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
-import { Source } from './source';
25
+import { Source, SourceType } from './source';
26
 import { Viewport } from '../core/viewport'
26
 import { Viewport } from '../core/viewport'
27
 import { Utils, Nullable } from '../utils/utils';
27
 import { Utils, Nullable } from '../utils/utils';
28
 
28
 
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
      * Consume a pointer event
74
      * Consume a pointer event
65
      * @returns the next pointer event to be consumed, or null if there are none
75
      * @returns the next pointer event to be consumed, or null if there are none
66
      * @internal
76
      * @internal

+ 17
- 1
src/sources/source.ts View File

21
  */
21
  */
22
 
22
 
23
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
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
  * Abstract source of data
29
  * Abstract source of data
30
     /** @internal type-identifier of the source of data */
33
     /** @internal type-identifier of the source of data */
31
     readonly _type: string;
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
     /** @internal method to initialize the source of data (gets the data ready) */
39
     /** @internal method to initialize the source of data (gets the data ready) */
34
     _init(): SpeedyPromise<void>;
40
     _init(): SpeedyPromise<void>;
35
 
41
 
38
 
44
 
39
     /** @internal stats related to this source of data */
45
     /** @internal stats related to this source of data */
40
     readonly _stats: string;
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 View File

25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26
 import { Utils, Nullable } from '../utils/utils';
26
 import { Utils, Nullable } from '../utils/utils';
27
 import { IllegalOperationError, NotSupportedError, TimeoutError } from '../utils/errors';
27
 import { IllegalOperationError, NotSupportedError, TimeoutError } from '../utils/errors';
28
-import { Source } from './source';
28
+import { Source, SourceType } from './source';
29
 
29
 
30
 /** A message to be displayed if a video can't autoplay and user interaction is required */
30
 /** A message to be displayed if a video can't autoplay and user interaction is required */
31
 const ALERT_MESSAGE = 'Tap on the screen to start';
31
 const ALERT_MESSAGE = 'Tap on the screen to start';
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
      * Get media
89
      * Get media
80
      * @internal
90
      * @internal
81
      */
91
      */

+ 4
- 4
src/trackers/image-tracker/image-tracker.ts View File

271
         // XXX also let the user specify a source manually?
271
         // XXX also let the user specify a source manually?
272
         for(const source of session.sources) {
272
         for(const source of session.sources) {
273
             // prefer video sources
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
                 break;
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
         if(this._source === null)
281
         if(this._source === null)
282
             throw new IllegalOperationError('The image tracker requires a suitable source of data');
282
             throw new IllegalOperationError('The image tracker requires a suitable source of data');

+ 2
- 2
src/trackers/pointer-tracker/pointer-tracker.ts View File

218
 
218
 
219
         // find the pointer source
219
         // find the pointer source
220
         for(const source of session.sources) {
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
                 break;
223
                 break;
224
             }
224
             }
225
         }
225
         }

Loading…
Cancel
Save