UNPKG

1.71 kBJavaScriptView Raw
1import { EventEmitter, Platform } from '@unimodules/core';
2/**
3 * A base class for subscribable sensors. The events emitted by this class are measurements
4 * specified by the parameter type `M`.
5 */
6export default class DeviceSensor {
7 constructor(nativeSensorModule, nativeEventName) {
8 this._nativeModule = nativeSensorModule;
9 this._nativeEmitter = new EventEmitter(nativeSensorModule);
10 this._nativeEventName = nativeEventName;
11 this._listenerCount = 0;
12 }
13 addListener(listener) {
14 const subscription = this._nativeEmitter.addListener(this._nativeEventName, listener);
15 subscription.remove = () => this.removeSubscription(subscription);
16 this._listenerCount++;
17 return subscription;
18 }
19 hasListeners() {
20 return this._listenerCount > 0;
21 }
22 getListenerCount() {
23 return this._listenerCount;
24 }
25 removeAllListeners() {
26 this._listenerCount = 0;
27 this._nativeEmitter.removeAllListeners(this._nativeEventName);
28 }
29 removeSubscription(subscription) {
30 this._listenerCount--;
31 this._nativeEmitter.removeSubscription(subscription);
32 }
33 setUpdateInterval(intervalMs) {
34 if (!this._nativeModule.setUpdateInterval) {
35 console.warn(`expo-sensors: setUpdateInterval() is not supported on ${Platform.OS}`);
36 }
37 else {
38 this._nativeModule.setUpdateInterval(intervalMs);
39 }
40 }
41 async isAvailableAsync() {
42 if (!this._nativeModule.isAvailableAsync) {
43 return false;
44 }
45 else {
46 return await this._nativeModule.isAvailableAsync();
47 }
48 }
49}
50//# sourceMappingURL=DeviceSensor.js.map
\No newline at end of file