File size: 2,902 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**

 * HeadingService

 *

 * Thin singleton over the native compass stream exposed by `crust`.

 * Works on both Android and iOS via CLLocationManager (iOS) / SensorManager (Android).

 *

 * Reference-counted: the underlying SensorManager subscription starts on

 * the first listener and stops when the last one unsubscribes. Late

 * subscribers get the most recently emitted heading replayed

 * synchronously so the UI doesn't have to wait for the next sensor tick.

 */

import CrustModule from "@mentra/crust"

const LOG_TAG = "HEADING_SERVICE"

export type HeadingListener = (degrees: number) => void

class HeadingService {
  private static instance: HeadingService | null = null
  private listeners = new Set<HeadingListener>()
  private nativeSub: {remove: () => void} | null = null
  private lastDegrees: number | null = null
  private nativeStarted = false

  private constructor() {}

  public static getInstance(): HeadingService {
    if (!HeadingService.instance) {
      HeadingService.instance = new HeadingService()
    }
    return HeadingService.instance
  }

  public addListener(listener: HeadingListener): () => void {
    this.listeners.add(listener)
    if (!this.nativeStarted) {
      this.startNative()
    }
    if (this.lastDegrees != null) {
      try {
        listener(this.lastDegrees)
      } catch (err) {
        console.error(`${LOG_TAG}: listener threw on replay`, err)
      }
    }
    return () => {
      this.listeners.delete(listener)
      if (this.listeners.size === 0) {
        this.stopNative()
      }
    }
  }

  private async startNative(): Promise<void> {
    if (this.nativeStarted) return
    this.nativeStarted = true
    this.nativeSub = CrustModule.addListener("onHeading", (data) => {
      const deg = Number(data.degrees)
      if (!Number.isFinite(deg)) return
      this.lastDegrees = deg
      this.listeners.forEach((l) => {
        try {
          l(deg)
        } catch (err) {
          console.error(`${LOG_TAG}: listener threw`, err)
        }
      })
    })
    try {
      const result = await CrustModule.startHeading()
      if (!result.ok) {
        console.warn(`${LOG_TAG}: startHeading failed — ${result.error}`)
      } else {
        console.log(`${LOG_TAG}: started`)
      }
    } catch (err) {
      console.error(`${LOG_TAG}: startHeading threw`, err)
    }
  }

  private async stopNative(): Promise<void> {
    if (!this.nativeStarted) return
    this.nativeStarted = false
    this.nativeSub?.remove()
    this.nativeSub = null
    this.lastDegrees = null
    try {
      await CrustModule.stopHeading()
      console.log(`${LOG_TAG}: stopped`)
    } catch (err) {
      console.error(`${LOG_TAG}: stopHeading threw`, err)
    }
  }
}

const headingService = HeadingService.getInstance()
export default headingService