@neurosity/sdk 6.2.2 → 6.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,7 +25,7 @@ import { createOAuthURL } from "./api/https/createOAuthURL";
25
25
  import { getOAuthToken } from "./api/https/getOAuthToken";
26
26
  import { isNode } from "./utils/is-node";
27
27
  import { getCloudMetric } from "./utils/metrics";
28
- import { BluetoothClient } from "./api/bluetooth";
28
+ import { BluetoothClient, osHasBluetoothSupport } from "./api/bluetooth";
29
29
  import { BLUETOOTH_CONNECTION } from "./api/bluetooth/types";
30
30
  const defaultOptions = {
31
31
  timesync: false,
@@ -115,16 +115,20 @@ export class Neurosity {
115
115
  streamingState() {
116
116
  const isWifiOnline = (state) => [STATUS.ONLINE, STATUS.UPDATING].includes(state);
117
117
  return this.streamingMode$.pipe(switchMap((streamingMode) => {
118
- if (this.isMissingBluetoothTransport) {
119
- return this.cloudClient.status().pipe(map(({ state }) => ({
120
- connected: isWifiOnline(state),
121
- streamingMode,
122
- activeMode: STREAMING_TYPE.WIFI
123
- })));
124
- }
125
- return this.onDeviceChange().pipe(switchMap((selectDevice) => !selectDevice
126
- ? EMPTY
127
- : combineLatest({
118
+ return this.onDeviceChange().pipe(switchMap((selectDevice) => {
119
+ if (!selectDevice) {
120
+ return EMPTY;
121
+ }
122
+ const isUnableToUseBluetooth = this.isMissingBluetoothTransport ||
123
+ !osHasBluetoothSupport(selectDevice);
124
+ if (isUnableToUseBluetooth) {
125
+ return this.cloudClient.status().pipe(map(({ state }) => ({
126
+ connected: isWifiOnline(state),
127
+ streamingMode,
128
+ activeMode: STREAMING_TYPE.WIFI
129
+ })));
130
+ }
131
+ return combineLatest({
128
132
  wifiStatus: this.cloudClient.status(),
129
133
  bluetoothConnection: !!(this === null || this === void 0 ? void 0 : this.bluetoothClient)
130
134
  ? this.bluetoothClient.connection()
@@ -141,13 +145,11 @@ export class Neurosity {
141
145
  };
142
146
  case STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:
143
147
  return {
144
- connected: isWifiOnline(wifiStatus.state) ||
145
- !isBluetoothConnected
148
+ connected: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
146
149
  ? isWifiOnline(wifiStatus.state)
147
150
  : isBluetoothConnected,
148
151
  streamingMode,
149
- activeMode: isWifiOnline(wifiStatus.state) ||
150
- !isBluetoothConnected
152
+ activeMode: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
151
153
  ? STREAMING_TYPE.WIFI
152
154
  : STREAMING_TYPE.BLUETOOTH
153
155
  };
@@ -162,7 +164,8 @@ export class Neurosity {
162
164
  : STREAMING_TYPE.WIFI
163
165
  };
164
166
  }
165
- }), distinctUntilChanged((a, b) => isEqual(a, b)))));
167
+ }), distinctUntilChanged((a, b) => isEqual(a, b)));
168
+ }));
166
169
  }));
167
170
  }
168
171
  /**
@@ -55,6 +55,7 @@ export declare class ReactNativeTransport implements BluetoothTransport {
55
55
  scan(options?: {
56
56
  seconds?: number;
57
57
  once?: boolean;
58
+ skipConnectionUpdate?: boolean;
58
59
  }): Observable<Peripheral[]>;
59
60
  connect(peripheral: Peripheral): Promise<void>;
60
61
  disconnect(): Promise<void>;
@@ -131,10 +131,14 @@ export class ReactNativeTransport {
131
131
  share());
132
132
  }
133
133
  scan(options) {
134
- var _a, _b;
134
+ var _a, _b, _c;
135
135
  const RESCAN_INTERVAL = 10000; // 10 seconds
136
136
  const seconds = (_a = options === null || options === void 0 ? void 0 : options.seconds) !== null && _a !== void 0 ? _a : RESCAN_INTERVAL / 1000;
137
137
  const once = (_b = options === null || options === void 0 ? void 0 : options.once) !== null && _b !== void 0 ? _b : false;
138
+ // If we are already connected to a peripheral and start scanning,
139
+ // be default, it will set the connection status to SCANNING and not
140
+ // update it back if no device is connected to
141
+ const skipConnectionUpdate = (_c = options === null || options === void 0 ? void 0 : options.skipConnectionUpdate) !== null && _c !== void 0 ? _c : false;
138
142
  const serviceUUIDs = [BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
139
143
  const allowDuplicates = true;
140
144
  const scanOptions = {};
@@ -158,7 +162,9 @@ export class ReactNativeTransport {
158
162
  ? scanOnce$
159
163
  : timer(0, RESCAN_INTERVAL).pipe(switchMap(() => scanOnce$));
160
164
  const peripherals$ = scan$.pipe(tap(() => {
161
- this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
165
+ if (!skipConnectionUpdate) {
166
+ this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
167
+ }
162
168
  }), takeUntil(this.onDisconnected$), switchMap(() => this.bleEvents.discoverPeripheral$),
163
169
  // Filter out devices that are not Neurosity devices
164
170
  filter((peripheral) => {
@@ -46842,10 +46842,14 @@ class ReactNativeTransport {
46842
46842
  share());
46843
46843
  }
46844
46844
  scan(options) {
46845
- var _a, _b;
46845
+ var _a, _b, _c;
46846
46846
  const RESCAN_INTERVAL = 10000; // 10 seconds
46847
46847
  const seconds = (_a = options === null || options === void 0 ? void 0 : options.seconds) !== null && _a !== void 0 ? _a : RESCAN_INTERVAL / 1000;
46848
46848
  const once = (_b = options === null || options === void 0 ? void 0 : options.once) !== null && _b !== void 0 ? _b : false;
46849
+ // If we are already connected to a peripheral and start scanning,
46850
+ // be default, it will set the connection status to SCANNING and not
46851
+ // update it back if no device is connected to
46852
+ const skipConnectionUpdate = (_c = options === null || options === void 0 ? void 0 : options.skipConnectionUpdate) !== null && _c !== void 0 ? _c : false;
46849
46853
  const serviceUUIDs = [BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
46850
46854
  const allowDuplicates = true;
46851
46855
  const scanOptions = {};
@@ -46869,7 +46873,9 @@ class ReactNativeTransport {
46869
46873
  ? scanOnce$
46870
46874
  : timer(0, RESCAN_INTERVAL).pipe(switchMap(() => scanOnce$));
46871
46875
  const peripherals$ = scan$.pipe(tap(() => {
46872
- this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
46876
+ if (!skipConnectionUpdate) {
46877
+ this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
46878
+ }
46873
46879
  }), takeUntil(this.onDisconnected$), switchMap(() => this.bleEvents.discoverPeripheral$),
46874
46880
  // Filter out devices that are not Neurosity devices
46875
46881
  filter((peripheral) => {
@@ -47605,16 +47611,20 @@ class Neurosity {
47605
47611
  streamingState() {
47606
47612
  const isWifiOnline = (state) => [STATUS.ONLINE, STATUS.UPDATING].includes(state);
47607
47613
  return this.streamingMode$.pipe(switchMap((streamingMode) => {
47608
- if (this.isMissingBluetoothTransport) {
47609
- return this.cloudClient.status().pipe(map(({ state }) => ({
47610
- connected: isWifiOnline(state),
47611
- streamingMode,
47612
- activeMode: STREAMING_TYPE.WIFI
47613
- })));
47614
- }
47615
- return this.onDeviceChange().pipe(switchMap((selectDevice) => !selectDevice
47616
- ? EMPTY
47617
- : combineLatest({
47614
+ return this.onDeviceChange().pipe(switchMap((selectDevice) => {
47615
+ if (!selectDevice) {
47616
+ return EMPTY;
47617
+ }
47618
+ const isUnableToUseBluetooth = this.isMissingBluetoothTransport ||
47619
+ !osHasBluetoothSupport(selectDevice);
47620
+ if (isUnableToUseBluetooth) {
47621
+ return this.cloudClient.status().pipe(map(({ state }) => ({
47622
+ connected: isWifiOnline(state),
47623
+ streamingMode,
47624
+ activeMode: STREAMING_TYPE.WIFI
47625
+ })));
47626
+ }
47627
+ return combineLatest({
47618
47628
  wifiStatus: this.cloudClient.status(),
47619
47629
  bluetoothConnection: !!(this === null || this === void 0 ? void 0 : this.bluetoothClient)
47620
47630
  ? this.bluetoothClient.connection()
@@ -47631,13 +47641,11 @@ class Neurosity {
47631
47641
  };
47632
47642
  case STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:
47633
47643
  return {
47634
- connected: isWifiOnline(wifiStatus.state) ||
47635
- !isBluetoothConnected
47644
+ connected: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47636
47645
  ? isWifiOnline(wifiStatus.state)
47637
47646
  : isBluetoothConnected,
47638
47647
  streamingMode,
47639
- activeMode: isWifiOnline(wifiStatus.state) ||
47640
- !isBluetoothConnected
47648
+ activeMode: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47641
47649
  ? STREAMING_TYPE.WIFI
47642
47650
  : STREAMING_TYPE.BLUETOOTH
47643
47651
  };
@@ -47652,7 +47660,8 @@ class Neurosity {
47652
47660
  : STREAMING_TYPE.WIFI
47653
47661
  };
47654
47662
  }
47655
- }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)))));
47663
+ }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)));
47664
+ }));
47656
47665
  }));
47657
47666
  }
47658
47667
  /**
@@ -46845,10 +46845,14 @@ var Neurosity = (function (exports) {
46845
46845
  share());
46846
46846
  }
46847
46847
  scan(options) {
46848
- var _a, _b;
46848
+ var _a, _b, _c;
46849
46849
  const RESCAN_INTERVAL = 10000; // 10 seconds
46850
46850
  const seconds = (_a = options === null || options === void 0 ? void 0 : options.seconds) !== null && _a !== void 0 ? _a : RESCAN_INTERVAL / 1000;
46851
46851
  const once = (_b = options === null || options === void 0 ? void 0 : options.once) !== null && _b !== void 0 ? _b : false;
46852
+ // If we are already connected to a peripheral and start scanning,
46853
+ // be default, it will set the connection status to SCANNING and not
46854
+ // update it back if no device is connected to
46855
+ const skipConnectionUpdate = (_c = options === null || options === void 0 ? void 0 : options.skipConnectionUpdate) !== null && _c !== void 0 ? _c : false;
46852
46856
  const serviceUUIDs = [BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
46853
46857
  const allowDuplicates = true;
46854
46858
  const scanOptions = {};
@@ -46872,7 +46876,9 @@ var Neurosity = (function (exports) {
46872
46876
  ? scanOnce$
46873
46877
  : timer(0, RESCAN_INTERVAL).pipe(switchMap(() => scanOnce$));
46874
46878
  const peripherals$ = scan$.pipe(tap(() => {
46875
- this.connection$.next(exports.BLUETOOTH_CONNECTION.SCANNING);
46879
+ if (!skipConnectionUpdate) {
46880
+ this.connection$.next(exports.BLUETOOTH_CONNECTION.SCANNING);
46881
+ }
46876
46882
  }), takeUntil(this.onDisconnected$), switchMap(() => this.bleEvents.discoverPeripheral$),
46877
46883
  // Filter out devices that are not Neurosity devices
46878
46884
  filter((peripheral) => {
@@ -47608,16 +47614,20 @@ var Neurosity = (function (exports) {
47608
47614
  streamingState() {
47609
47615
  const isWifiOnline = (state) => [STATUS.ONLINE, STATUS.UPDATING].includes(state);
47610
47616
  return this.streamingMode$.pipe(switchMap((streamingMode) => {
47611
- if (this.isMissingBluetoothTransport) {
47612
- return this.cloudClient.status().pipe(map(({ state }) => ({
47613
- connected: isWifiOnline(state),
47614
- streamingMode,
47615
- activeMode: STREAMING_TYPE.WIFI
47616
- })));
47617
- }
47618
- return this.onDeviceChange().pipe(switchMap((selectDevice) => !selectDevice
47619
- ? EMPTY
47620
- : combineLatest({
47617
+ return this.onDeviceChange().pipe(switchMap((selectDevice) => {
47618
+ if (!selectDevice) {
47619
+ return EMPTY;
47620
+ }
47621
+ const isUnableToUseBluetooth = this.isMissingBluetoothTransport ||
47622
+ !osHasBluetoothSupport(selectDevice);
47623
+ if (isUnableToUseBluetooth) {
47624
+ return this.cloudClient.status().pipe(map(({ state }) => ({
47625
+ connected: isWifiOnline(state),
47626
+ streamingMode,
47627
+ activeMode: STREAMING_TYPE.WIFI
47628
+ })));
47629
+ }
47630
+ return combineLatest({
47621
47631
  wifiStatus: this.cloudClient.status(),
47622
47632
  bluetoothConnection: !!(this === null || this === void 0 ? void 0 : this.bluetoothClient)
47623
47633
  ? this.bluetoothClient.connection()
@@ -47634,13 +47644,11 @@ var Neurosity = (function (exports) {
47634
47644
  };
47635
47645
  case STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:
47636
47646
  return {
47637
- connected: isWifiOnline(wifiStatus.state) ||
47638
- !isBluetoothConnected
47647
+ connected: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47639
47648
  ? isWifiOnline(wifiStatus.state)
47640
47649
  : isBluetoothConnected,
47641
47650
  streamingMode,
47642
- activeMode: isWifiOnline(wifiStatus.state) ||
47643
- !isBluetoothConnected
47651
+ activeMode: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47644
47652
  ? STREAMING_TYPE.WIFI
47645
47653
  : STREAMING_TYPE.BLUETOOTH
47646
47654
  };
@@ -47655,7 +47663,8 @@ var Neurosity = (function (exports) {
47655
47663
  : STREAMING_TYPE.WIFI
47656
47664
  };
47657
47665
  }
47658
- }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)))));
47666
+ }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)));
47667
+ }));
47659
47668
  }));
47660
47669
  }
47661
47670
  /**
@@ -649,7 +649,7 @@ var r=require("./compare"),e=function(e,o,u){return r(e,o,u)>=0};module.exports=
649
649
  },{"semver/functions/gte":"A2P2"}],"ouKb":[function(require,module,exports) {
650
650
  "use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t){return o(e)||a(e,t)||c()}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function a(e,t){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)){var n=[],r=!0,i=!1,c=void 0;try{for(var a,o=e[Symbol.iterator]();!(r=(a=o.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(s){i=!0,c=s}finally{try{r||null==o.return||o.return()}finally{if(i)throw c}}return n}}function o(e){if(Array.isArray(e))return e}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function d(e,t,n){return t&&u(e.prototype,t),n&&u(e,n),e}var h=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,c){function a(e){try{s(r.next(e))}catch(t){c(t)}}function o(e){try{s(r.throw(e))}catch(t){c(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.WebBluetoothTransport=void 0;var v=require("@neurosity/ipk"),f=require("@neurosity/ipk"),p=require("@neurosity/ipk"),l=require("@neurosity/ipk"),g=require("rxjs"),m=require("rxjs"),b=require("rxjs/operators"),E=require("rxjs/operators"),N=require("rxjs/operators"),w=require("./isWebBluetoothSupported"),O=require("../utils/create6DigitPin"),C=require("../utils/stitch"),y=require("../utils/encoding"),x=require("../types"),T=require("../constants"),k=require("../constants"),R=require("../utils/osHasBluetoothSupport"),A={autoConnect:!0},_=function(){function t(){var e=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(s(this,t),this.type=x.TRANSPORT_TYPE.WEB,this.characteristicsByName={},this.connection$=new g.BehaviorSubject(x.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new g.BehaviorSubject([]),this.logs$=new g.ReplaySubject(10),this.onDisconnected$=this._onDisconnected().pipe((0,N.share)()),this.connectionStream$=this.connection$.asObservable().pipe((0,b.filter)(function(e){return!!e}),(0,E.distinctUntilChanged)(),(0,E.shareReplay)(1)),this._isAutoConnectEnabled$=new g.ReplaySubject(1),this.options=Object.assign(Object.assign({},A),n),!(0,w.isWebBluetoothSupported)()){throw this.addLog("Web Bluetooth is not supported"),new Error("Web Bluetooth is not supported")}this._isAutoConnectEnabled$.subscribe(function(t){e.addLog("Auto connect: ".concat(t?"enabled":"disabled"))}),this._isAutoConnectEnabled$.next(this.options.autoConnect),this.connection$.asObservable().subscribe(function(t){e.addLog("connection status is ".concat(t))}),this.onDisconnected$.subscribe(function(){e.connection$.next(x.BLUETOOTH_CONNECTION.DISCONNECTED)})}return d(t,[{key:"_getPairedDevices",value:function(){return h(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,navigator.bluetooth.getDevices();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e)}))}},{key:"_autoConnect",value:function(e){var t=this;return this._isAutoConnectEnabled$.pipe((0,b.switchMap)(function(n){return n?(0,g.merge)(e,t.onDisconnected$.pipe((0,b.switchMap)(function(){return e}))):m.NEVER}),(0,b.switchMap)(function(e){return(0,R.osHasBluetoothSupport)(e)?(0,g.of)(e):m.EMPTY}),(0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,c,a,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.deviceNickname,!this.isConnected()){t.next=4;break}return this.addLog("Auto connect: ".concat(r," is already connected. Skipping auto connect.")),t.abrupt("return");case 4:return t.next=6,this._getPairedDevices().then(function(e){return[null,e]}).catch(function(e){return[e,null]});case 6:if(c=t.sent,a=i(c,2),o=a[0],s=a[1],!o){t.next=12;break}throw new Error("failed to get devices: ".concat(null!==(n=null==o?void 0:o.message)&&void 0!==n?n:o));case 12:if(this.addLog("Auto connect: found ".concat(s.length," devices ").concat(s.map(function(e){return e.name}).join(", "))),u=s.findLast(function(e){return e.name===r})){t.next=16;break}throw new Error("couldn't find selected device in the list of paired devices.");case 16:return this.addLog("Auto connect: ".concat(r," was detected and previously paired")),t.abrupt("return",u);case 18:case"end":return t.stop()}},t,this)}))}),(0,b.tap)(function(){t.connection$.next(x.BLUETOOTH_CONNECTION.SCANNING)}),(0,b.switchMap)(function(e){return S(e)}),(0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return this.addLog("Advertisement received for ".concat(e.device.name)),t.next=3,this.getServerServiceAndCharacteristics(e.device);case 3:return t.abrupt("return",t.sent);case 4:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===x.BLUETOOTH_CONNECTION.CONNECTED}},{key:"connection",value:function(){return this.connectionStream$}},{key:"connect",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,this.requestDevice(e);case 3:return n=t.sent,t.next=6,this.getServerServiceAndCharacteristics(n);case 6:t.next=11;break;case 8:return t.prev=8,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 11:case"end":return t.stop()}},t,this,[[0,8]])}))}},{key:"requestDevice",value:function(t){return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,c;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.prev=0,this.addLog("Requesting Bluetooth Device..."),r=p.BLUETOOTH_DEVICE_NAME_PREFIXES.map(function(e){return{namePrefix:e}}),i=t?[{name:t}]:r,n.next=6,window.navigator.bluetooth.requestDevice({filters:[].concat(e(i),[{manufacturerData:[{companyIdentifier:l.BLUETOOTH_COMPANY_IDENTIFIER_HEX}]}]),optionalServices:[v.BLUETOOTH_PRIMARY_SERVICE_UUID_HEX]});case 6:return c=n.sent,n.abrupt("return",c);case 10:return n.prev=10,n.t0=n.catch(0),n.abrupt("return",Promise.reject(n.t0));case 13:case"end":return n.stop()}},n,this,[[0,10]])}))}},{key:"getServerServiceAndCharacteristics",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,this.device=e,this.connection$.getValue()===x.BLUETOOTH_CONNECTION.CONNECTING||this.connection$.next(x.BLUETOOTH_CONNECTION.CONNECTING),t.next=6,e.gatt.connect();case 6:return this.server=t.sent,this.addLog("Getting service..."),t.next=10,this.server.getPrimaryService(v.BLUETOOTH_PRIMARY_SERVICE_UUID_HEX);case 10:return this.service=t.sent,this.addLog("Got service ".concat(this.service.uuid,", getting characteristics...")),t.next=14,this.service.getCharacteristics();case 14:n=t.sent,this.addLog("Got characteristics"),this.characteristicsByName=Object.fromEntries(n.map(function(e){return[k.CHARACTERISTIC_UUIDS_TO_NAMES[e.uuid],e]})),this.connection$.next(x.BLUETOOTH_CONNECTION.CONNECTED),t.next=23;break;case 20:return t.prev=20,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 23:case"end":return t.stop()}},t,this,[[0,20]])}))}},{key:"_onDisconnected",value:function(){var e=this;return this.connection$.asObservable().pipe((0,b.switchMap)(function(t){return t===x.BLUETOOTH_CONNECTION.CONNECTED?L(e.device,"gattserverdisconnected"):m.NEVER}))}},{key:"disconnect",value:function(){var e,t;return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:(null===(t=null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.gatt)||void 0===t?void 0:t.connected)&&this.device.gatt.disconnect();case 2:case"end":return n.stop()}},n,this)}))}},{key:"getCharacteristicByName",value:function(e){var t;return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.abrupt("return",null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]);case 1:case"end":return n.stop()}},n,this)}))}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r,c=(0,g.defer)(function(){return t.getCharacteristicByName(n)}).pipe((0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isConnected()||!i){t.next=10;break}return t.prev=1,t.next=4,e.startNotifications();case 4:this.addLog("Started notifications for ".concat(n," characteristic")),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 10:return t.abrupt("return",e);case 11:case"end":return t.stop()}},t,this,[[1,7]])}))}),(0,b.switchMap)(function(e){return L(e,"characteristicvaluechanged",function(){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isConnected()||!i){t.next=10;break}return t.prev=1,t.next=4,e.stopNotifications();case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 10:case"end":return t.stop()}},t,this,[[1,7]])}))})}),(0,b.map)(function(e){var r=e.target.value,i=(0,y.decode)(t.type,r);return t.addLog("Received chunk with buffer size of ".concat(r.byteLength," and decoded size ").concat(i.length," for ").concat(n," characteristic: \n").concat(i)),i}),(0,C.stitchChunks)({delimiter:f.BLUETOOTH_CHUNK_DELIMITER}),(0,b.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}));return this.connection$.pipe((0,b.switchMap)(function(e){return e===x.BLUETOOTH_CONNECTION.CONNECTED?c:m.NEVER}))}},{key:"readCharacteristic",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,c,a,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.prev=0,this.addLog("Reading characteristic: ".concat(e)),n.next=4,this.getCharacteristicByName(e);case 4:if(r=n.sent){n.next=8;break}return this.addLog("Did not fund ".concat(e," characteristic")),n.abrupt("return",Promise.reject("Did not find characteristic by the name: ".concat(e)));case 8:return n.next=10,r.readValue();case 10:return i=n.sent,c=i,a=(0,y.decode)(this.type,c),o=t?JSON.parse(a):a,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(o)),n.abrupt("return",o);case 18:return n.prev=18,n.t0=n.catch(0),n.abrupt("return",Promise.reject("Error reading characteristic: ".concat(n.t0.message)));case 21:case"end":return n.stop()}},n,this,[[0,18]])}))}},{key:"writeCharacteristic",value:function(e,t){return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return this.addLog("Writing characteristic: ".concat(e)),n.next=3,this.getCharacteristicByName(e);case 3:if(r=n.sent){n.next=7;break}return this.addLog("Did not fund ".concat(e," characteristic")),n.abrupt("return",Promise.reject("Did not find characteristic by the name: ".concat(e)));case 7:return i=(0,y.encode)(this.type,t),n.next=10,r.writeValueWithResponse(i);case 10:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i,c=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:r=!1,i=this.connection$.asObservable().pipe((0,b.switchMap)(function(e){return e===x.BLUETOOTH_CONNECTION.CONNECTED?(0,g.defer)(function(){return c.getCharacteristicByName("actions")}).pipe((0,b.switchMap)(function(e){return n=e,c.pendingActions$})):m.NEVER}),(0,b.tap)(function(e){return h(c,void 0,void 0,regeneratorRuntime.mark(function t(){var i,c,a;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!(a=!!e.length)||r){t.next=12;break}return r=!0,t.prev=3,t.next=6,n.startNotifications();case 6:this.addLog("Started notifications for [actions] characteristic"),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(3),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(i=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==i?i:t.t0));case 12:if(a||!r){t.next=23;break}return r=!1,t.prev=14,t.next=17,n.stopNotifications();case 17:this.addLog("Stopped notifications for actions characteristic"),t.next=23;break;case 20:t.prev=20,t.t1=t.catch(14),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(c=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==c?c:t.t1));case 23:case"end":return t.stop()}},t,this,[[3,9],[14,20]])}))})),e.pipe((0,b.switchMap)(function(e){return(0,R.osHasBluetoothSupport)(e)?i:m.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return h(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,c,a,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,c=n.responseTimeout,a=void 0===c?T.DEFAULT_ACTION_RESPONSE_TIMEOUT:c,e.abrupt("return",new Promise(function(e,r){return h(o,void 0,void 0,regeneratorRuntime.mark(function c(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return c.next=2,this.getCharacteristicByName(t).catch(function(){r("Did not find characteristic by the name: ".concat(t))});case 2:if(c.sent){c.next=5;break}return c.abrupt("return");case 5:o=(0,O.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&a?(this._addPendingAction(o),u=(0,g.timer)(a).subscribe(function(){d._removePendingAction(o),r("Action with id ".concat(o," timed out after ").concat(a,"ms"))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,b.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,N.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e.message)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e.message)});case 9:case"end":return c.stop()}},c,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();function L(e,t,n){var r=this;return(0,m.fromEventPattern)(function(n){e.addEventListener(t,n)},function(i){return h(r,void 0,void 0,regeneratorRuntime.mark(function r(){return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(!n){r.next=3;break}return r.next=3,n();case 3:e.removeEventListener(t,i);case 4:case"end":return r.stop()}},r)}))})}function S(e){return new m.Observable(function(t){var n=new AbortController,r=n.signal,i=e.addEventListener("advertisementreceived",function(e){n.abort(),t.next(e),t.complete()},{once:!0});try{e.watchAdvertisements({signal:r})}catch(c){t.error(c)}return function(){n.abort(),e.removeEventListener("advertisementreceived",i)}})}exports.WebBluetoothTransport=_;
651
651
  },{"@neurosity/ipk":"ZOGh","rxjs":"Zr8e","rxjs/operators":"v3iE","./isWebBluetoothSupported":"ljun","../utils/create6DigitPin":"UDAB","../utils/stitch":"V73a","../utils/encoding":"jTvL","../types":"iwtf","../constants":"dGLb","../utils/osHasBluetoothSupport":"rH2Y"}],"FtS5":[function(require,module,exports) {
652
- "use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,t,n){return t&&c(e.prototype,t),n&&c(e,n),e}var s=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{s(r.next(e))}catch(t){a(t)}}function o(e){try{s(r.throw(e))}catch(t){a(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactNativeTransport=void 0;var u=require("@neurosity/ipk"),d=require("@neurosity/ipk"),h=require("@neurosity/ipk"),l=require("rxjs"),v=require("rxjs"),p=require("rxjs/operators"),f=require("rxjs/operators"),g=require("rxjs/operators"),m=require("../utils/create6DigitPin"),E=require("../utils/stitch"),b=require("../utils/encoding"),N=require("../types"),O=require("../constants"),C=require("../constants"),w=require("../constants"),T=require("../constants"),y=require("../utils/osHasBluetoothSupport"),_={autoConnect:!0},R=function(){function t(e){var n=this;if(a(this,t),this.type=N.TRANSPORT_TYPE.REACT_NATIVE,this.characteristicsByName={},this.connection$=new l.BehaviorSubject(N.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new l.BehaviorSubject([]),this.logs$=new l.ReplaySubject(10),this.connectionStream$=this.connection$.asObservable().pipe((0,p.filter)(function(e){return!!e}),(0,f.distinctUntilChanged)(),(0,f.shareReplay)(1)),this._isAutoConnectEnabled$=new l.ReplaySubject(1),!e){var r="React Native transport: missing options.";throw this.addLog(r),new Error(r)}this.options=Object.assign(Object.assign({},_),e);var i=this.options,c=i.BleManager,o=i.bleManagerEmitter,s=i.platform,u=i.autoConnect;if(!c){var d="React Native option: BleManager not provided.";throw this.addLog(d),new Error(d)}if(!o){var h="React Native option: bleManagerEmitter not provided.";throw this.addLog(h),new Error(h)}if(!s){var v="React Native option: platform not provided.";throw this.addLog(v),new Error(v)}this.BleManager=c,this.bleManagerEmitter=o,this.platform=s,this._isAutoConnectEnabled$.next(u),this._isAutoConnectEnabled$.subscribe(function(e){n.addLog("Auto connect: ".concat(e?"enabled":"disabled"))}),this.bleEvents={stopScan$:this._fromEvent("BleManagerStopScan"),discoverPeripheral$:this._fromEvent("BleManagerDiscoverPeripheral"),connectPeripheral$:this._fromEvent("BleManagerConnectPeripheral"),disconnectPeripheral$:this._fromEvent("BleManagerDisconnectPeripheral"),didUpdateValueForCharacteristic$:this._fromEvent("BleManagerDidUpdateValueForCharacteristic"),didUpdateState$:this._fromEvent("BleManagerDidUpdateState")},this.onDisconnected$=this.bleEvents.disconnectPeripheral$.pipe((0,g.share)()),this.BleManager.start({showAlert:!1}).then(function(){n.addLog("BleManger started")}).catch(function(e){var t;n.addLog("BleManger failed to start. ".concat(null!==(t=null==e?void 0:e.message)&&void 0!==t?t:e))}),this.connection$.asObservable().subscribe(function(e){n.addLog("connection status is ".concat(e))}),this.onDisconnected$.subscribe(function(){n.connection$.next(N.BLUETOOTH_CONNECTION.DISCONNECTED)})}return o(t,[{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===N.BLUETOOTH_CONNECTION.CONNECTED}},{key:"_autoConnect",value:function(e){var t=this,n=this.onDisconnected$.pipe((0,p.switchMap)(function(){return e}));return this._isAutoConnectEnabled$.pipe((0,p.switchMap)(function(t){return t?(0,l.merge)(e,n):v.NEVER}),(0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?t.scan().pipe((0,p.switchMap)(function(t){var n=t.find(function(t){return t.name===(null==e?void 0:e.deviceNickname)});return n?(0,l.of)(n):v.NEVER}),(0,g.distinct)(function(e){return e.id}),(0,g.take)(1)):v.NEVER}),(0,p.switchMap)(function(e){return s(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.connect(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"connection",value:function(){return this.connectionStream$}},{key:"_fromEvent",value:function(e){var t=this;return(0,v.fromEventPattern)(function(n){t.bleManagerEmitter.addListener(e,n)},function(){t.bleManagerEmitter.removeAllListeners(e)}).pipe((0,g.share)())}},{key:"scan",value:function(e){var t,n,r=this,a=null!==(t=null==e?void 0:e.seconds)&&void 0!==t?t:10,c=null!==(n=null==e?void 0:e.once)&&void 0!==n&&n,o=[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING],s={},d=new v.Observable(function(e){var t;try{r.BleManager.scan(o,a,!0,s).then(function(){r.addLog("BleManger scanning ".concat(c?"once":"indefintely")),e.next()})}catch(n){r.addLog("BleManger scanning ".concat(c?"once":"indefintely"," failed. ").concat(null!==(t=null==n?void 0:n.message)&&void 0!==t?t:n)),e.error(n)}return function(){r.BleManager.stopScan()}});return(c?d:(0,l.timer)(0,1e4).pipe((0,p.switchMap)(function(){return d}))).pipe((0,p.tap)(function(){r.connection$.next(N.BLUETOOTH_CONNECTION.SCANNING)}),(0,p.takeUntil)(this.onDisconnected$),(0,p.switchMap)(function(){return r.bleEvents.discoverPeripheral$}),(0,p.filter)(function(e){var t,n,r,i=null!==(r=null!==(n=null===(t=null==e?void 0:e.advertising)||void 0===t?void 0:t.localName)&&void 0!==n?n:e.name)&&void 0!==r?r:"";return!!i&&-1!==h.BLUETOOTH_DEVICE_NAME_PREFIXES.findIndex(function(e){return i.startsWith(e)})}),(0,g.scan)(function(e,t){var n,a,c,o,s,u,d,h,l=null!==(c=null!==(a=null===(n=null==t?void 0:t.advertising)||void 0===n?void 0:n.localName)&&void 0!==a?a:t.name)&&void 0!==c?c:"",v=null===(h=null===(d=(0,b.decode)(r.type,null!==(u=null===(s=null===(o=null==t?void 0:t.advertising)||void 0===o?void 0:o.manufacturerData)||void 0===s?void 0:s.bytes)&&void 0!==u?u:[]))||void 0===d?void 0:d.slice)||void 0===h?void 0:h.call(d,2);return Object.assign(Object.assign({},e),i({},t.id,Object.assign(Object.assign({},t),{name:l,manufactureDataString:v})))},{}),(0,f.distinctUntilChanged)(function(e,t){return JSON.stringify(e)===JSON.stringify(t)}),(0,p.map)(function(e){return Object.values(e)}),(0,g.share)())}},{key:"connect",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(t,r){return s(n,void 0,void 0,regeneratorRuntime.mark(function n(){var i,a=this;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(n.prev=0,e){n.next=4;break}return this.addLog("Peripheral not found"),n.abrupt("return");case 4:return this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTING),n.next=7,this.BleManager.connect(e.id);case 7:return this.addLog("Getting service..."),n.next=10,this.BleManager.retrieveServices(e.id,[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING]);case 10:if(i=n.sent){n.next=15;break}return this.addLog("Could not retreive services"),r(new Error("Could not retreive services")),n.abrupt("return");case 15:if(this.addLog("Got service ".concat(u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING,", getting characteristics...")),this.device=e,this.characteristicsByName=Object.fromEntries(i.characteristics.map(function(t){return[C.CHARACTERISTIC_UUIDS_TO_NAMES[t.characteristic.toLowerCase()],{characteristicUUID:t.characteristic,serviceUUID:t.service,peripheralId:e.id}]})),this.addLog("Got characteristics."),"android"!==this.platform){n.next=22;break}return n.next=22,this.BleManager.requestMTU(e.id,w.ANDROID_MAX_MTU).then(function(e){a.addLog("Successfully updated Android MTU to ".concat(e," bytes. Requested MTU: ").concat(w.ANDROID_MAX_MTU," bytes."))}).catch(function(e){a.addLog("Failed to set Android MTU of ".concat(w.ANDROID_MAX_MTU," bytes. Error: ").concat(e))});case 22:this.addLog("Successfully connected to peripheral ".concat(e.id)),this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTED),t(),n.next=30;break;case 27:n.prev=27,n.t0=n.catch(0),r(n.t0);case 30:case"end":return n.stop()}},n,this,[[0,27]])}))}));case 1:case"end":return t.stop()}},t)}))}},{key:"disconnect",value:function(){var e;return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,!(this.isConnected()&&(null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.id))){t.next=4;break}return t.next=4,this.BleManager.disconnect(this.device.id);case 4:t.next=9;break;case 6:return t.prev=6,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 9:case"end":return t.stop()}},t,this,[[0,6]])}))}},{key:"getCharacteristicByName",value:function(e){var t;if(!(e in this.characteristicsByName))throw new Error("Characteristic by name ".concat(e," is not found"));return null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r;return this.connection$.pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?(r=t.getCharacteristicByName(n),a=r.peripheralId,c=r.serviceUUID,o=r.characteristicUUID,(0,l.defer)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.startNotification(a,c,o);case 4:this.addLog("Started notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}).pipe((0,p.switchMap)(function(){return t.bleEvents.didUpdateValueForCharacteristic$}),(0,f.finalize)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.stopNotification(a,c,o);case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}),(0,p.filter)(function(e){return e.characteristic===o}),(0,p.map)(function(e){var n=e.value;return(0,b.decode)(t.type,n)}),(0,E.stitchChunks)({delimiter:d.BLUETOOTH_CHUNK_DELIMITER}),(0,p.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}))):v.NEVER;var r,a,c,o}))}},{key:"readCharacteristic",value:function(e){var t,n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return s(this,void 0,void 0,regeneratorRuntime.mark(function r(){var i,a,c,o,s,u,d;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(this.addLog("Reading characteristic: ".concat(e)),i=this.getCharacteristicByName(e),a=i.peripheralId,c=i.serviceUUID,o=i.characteristicUUID){r.next=4;break}return r.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return r.prev=4,r.next=7,this.BleManager.read(a,c,o);case 7:return s=r.sent,u=(0,b.decode)(this.type,s),d=n?JSON.parse(u):u,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(d)),r.abrupt("return",d);case 14:return r.prev=14,r.t0=r.catch(4),r.abrupt("return",Promise.reject(new Error("readCharacteristic ".concat(e," error. ").concat(null!==(t=null===r.t0||void 0===r.t0?void 0:r.t0.message)&&void 0!==t?t:r.t0))));case 17:case"end":return r.stop()}},r,this,[[4,14]])}))}},{key:"writeCharacteristic",value:function(e,t){return s(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,a,c,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(this.addLog("Writing characteristic: ".concat(e)),r=this.getCharacteristicByName(e),i=r.peripheralId,a=r.serviceUUID,c=r.characteristicUUID){n.next=4;break}return n.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return o=(0,b.encode)(this.type,t),n.next=7,this.BleManager.write(i,a,c,o,T.REACT_NATIVE_MAX_BYTE_SIZE);case 7:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=!1,r=this.connection$.asObservable().pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?i.pendingActions$:v.NEVER}),(0,p.tap)(function(e){return s(i,void 0,void 0,regeneratorRuntime.mark(function t(){var r,i,a,c,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(a=this.getCharacteristicByName("actions"),c=a.peripheralId,o=a.serviceUUID,s=a.characteristicUUID,!(u=!!e.length)||n){t.next=13;break}return n=!0,t.prev=4,t.next=7,this.BleManager.startNotification(c,o,s);case 7:this.addLog("Started notifications for [actions] characteristic"),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(4),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 13:if(u||!n){t.next=24;break}return n=!1,t.prev=15,t.next=18,this.BleManager.stopNotification(c,o,s);case 18:this.addLog("Stopped notifications for actions characteristic"),t.next=24;break;case 21:t.prev=21,t.t1=t.catch(15),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(i=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==i?i:t.t1));case 24:case"end":return t.stop()}},t,this,[[4,10],[15,21]])}))})),e.pipe((0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?r:v.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return s(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,a,c,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,a=n.responseTimeout,c=void 0===a?O.DEFAULT_ACTION_RESPONSE_TIMEOUT:a,e.abrupt("return",new Promise(function(e,r){return s(o,void 0,void 0,regeneratorRuntime.mark(function a(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:o=(0,m.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&c?(this._addPendingAction(o),u=(0,l.timer)(c).subscribe(function(){d._removePendingAction(o),r(new Error("Action with id ".concat(o," timed out after ").concat(c,"ms")))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,p.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,g.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e)});case 4:case"end":return a.stop()}},a,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();exports.ReactNativeTransport=R;
652
+ "use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,t,n){return t&&c(e.prototype,t),n&&c(e,n),e}var s=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{s(r.next(e))}catch(t){a(t)}}function o(e){try{s(r.throw(e))}catch(t){a(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactNativeTransport=void 0;var u=require("@neurosity/ipk"),d=require("@neurosity/ipk"),h=require("@neurosity/ipk"),l=require("rxjs"),v=require("rxjs"),p=require("rxjs/operators"),f=require("rxjs/operators"),g=require("rxjs/operators"),m=require("../utils/create6DigitPin"),E=require("../utils/stitch"),b=require("../utils/encoding"),N=require("../types"),O=require("../constants"),C=require("../constants"),w=require("../constants"),T=require("../constants"),y=require("../utils/osHasBluetoothSupport"),_={autoConnect:!0},R=function(){function t(e){var n=this;if(a(this,t),this.type=N.TRANSPORT_TYPE.REACT_NATIVE,this.characteristicsByName={},this.connection$=new l.BehaviorSubject(N.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new l.BehaviorSubject([]),this.logs$=new l.ReplaySubject(10),this.connectionStream$=this.connection$.asObservable().pipe((0,p.filter)(function(e){return!!e}),(0,f.distinctUntilChanged)(),(0,f.shareReplay)(1)),this._isAutoConnectEnabled$=new l.ReplaySubject(1),!e){var r="React Native transport: missing options.";throw this.addLog(r),new Error(r)}this.options=Object.assign(Object.assign({},_),e);var i=this.options,c=i.BleManager,o=i.bleManagerEmitter,s=i.platform,u=i.autoConnect;if(!c){var d="React Native option: BleManager not provided.";throw this.addLog(d),new Error(d)}if(!o){var h="React Native option: bleManagerEmitter not provided.";throw this.addLog(h),new Error(h)}if(!s){var v="React Native option: platform not provided.";throw this.addLog(v),new Error(v)}this.BleManager=c,this.bleManagerEmitter=o,this.platform=s,this._isAutoConnectEnabled$.next(u),this._isAutoConnectEnabled$.subscribe(function(e){n.addLog("Auto connect: ".concat(e?"enabled":"disabled"))}),this.bleEvents={stopScan$:this._fromEvent("BleManagerStopScan"),discoverPeripheral$:this._fromEvent("BleManagerDiscoverPeripheral"),connectPeripheral$:this._fromEvent("BleManagerConnectPeripheral"),disconnectPeripheral$:this._fromEvent("BleManagerDisconnectPeripheral"),didUpdateValueForCharacteristic$:this._fromEvent("BleManagerDidUpdateValueForCharacteristic"),didUpdateState$:this._fromEvent("BleManagerDidUpdateState")},this.onDisconnected$=this.bleEvents.disconnectPeripheral$.pipe((0,g.share)()),this.BleManager.start({showAlert:!1}).then(function(){n.addLog("BleManger started")}).catch(function(e){var t;n.addLog("BleManger failed to start. ".concat(null!==(t=null==e?void 0:e.message)&&void 0!==t?t:e))}),this.connection$.asObservable().subscribe(function(e){n.addLog("connection status is ".concat(e))}),this.onDisconnected$.subscribe(function(){n.connection$.next(N.BLUETOOTH_CONNECTION.DISCONNECTED)})}return o(t,[{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===N.BLUETOOTH_CONNECTION.CONNECTED}},{key:"_autoConnect",value:function(e){var t=this,n=this.onDisconnected$.pipe((0,p.switchMap)(function(){return e}));return this._isAutoConnectEnabled$.pipe((0,p.switchMap)(function(t){return t?(0,l.merge)(e,n):v.NEVER}),(0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?t.scan().pipe((0,p.switchMap)(function(t){var n=t.find(function(t){return t.name===(null==e?void 0:e.deviceNickname)});return n?(0,l.of)(n):v.NEVER}),(0,g.distinct)(function(e){return e.id}),(0,g.take)(1)):v.NEVER}),(0,p.switchMap)(function(e){return s(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.connect(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"connection",value:function(){return this.connectionStream$}},{key:"_fromEvent",value:function(e){var t=this;return(0,v.fromEventPattern)(function(n){t.bleManagerEmitter.addListener(e,n)},function(){t.bleManagerEmitter.removeAllListeners(e)}).pipe((0,g.share)())}},{key:"scan",value:function(e){var t,n,r,a=this,c=null!==(t=null==e?void 0:e.seconds)&&void 0!==t?t:10,o=null!==(n=null==e?void 0:e.once)&&void 0!==n&&n,s=null!==(r=null==e?void 0:e.skipConnectionUpdate)&&void 0!==r&&r,d=[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING],m={},E=new v.Observable(function(e){var t;try{a.BleManager.scan(d,c,!0,m).then(function(){a.addLog("BleManger scanning ".concat(o?"once":"indefintely")),e.next()})}catch(n){a.addLog("BleManger scanning ".concat(o?"once":"indefintely"," failed. ").concat(null!==(t=null==n?void 0:n.message)&&void 0!==t?t:n)),e.error(n)}return function(){a.BleManager.stopScan()}});return(o?E:(0,l.timer)(0,1e4).pipe((0,p.switchMap)(function(){return E}))).pipe((0,p.tap)(function(){s||a.connection$.next(N.BLUETOOTH_CONNECTION.SCANNING)}),(0,p.takeUntil)(this.onDisconnected$),(0,p.switchMap)(function(){return a.bleEvents.discoverPeripheral$}),(0,p.filter)(function(e){var t,n,r,i=null!==(r=null!==(n=null===(t=null==e?void 0:e.advertising)||void 0===t?void 0:t.localName)&&void 0!==n?n:e.name)&&void 0!==r?r:"";return!!i&&-1!==h.BLUETOOTH_DEVICE_NAME_PREFIXES.findIndex(function(e){return i.startsWith(e)})}),(0,g.scan)(function(e,t){var n,r,c,o,s,u,d,h,l=null!==(c=null!==(r=null===(n=null==t?void 0:t.advertising)||void 0===n?void 0:n.localName)&&void 0!==r?r:t.name)&&void 0!==c?c:"",v=null===(h=null===(d=(0,b.decode)(a.type,null!==(u=null===(s=null===(o=null==t?void 0:t.advertising)||void 0===o?void 0:o.manufacturerData)||void 0===s?void 0:s.bytes)&&void 0!==u?u:[]))||void 0===d?void 0:d.slice)||void 0===h?void 0:h.call(d,2);return Object.assign(Object.assign({},e),i({},t.id,Object.assign(Object.assign({},t),{name:l,manufactureDataString:v})))},{}),(0,f.distinctUntilChanged)(function(e,t){return JSON.stringify(e)===JSON.stringify(t)}),(0,p.map)(function(e){return Object.values(e)}),(0,g.share)())}},{key:"connect",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(t,r){return s(n,void 0,void 0,regeneratorRuntime.mark(function n(){var i,a=this;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(n.prev=0,e){n.next=4;break}return this.addLog("Peripheral not found"),n.abrupt("return");case 4:return this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTING),n.next=7,this.BleManager.connect(e.id);case 7:return this.addLog("Getting service..."),n.next=10,this.BleManager.retrieveServices(e.id,[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING]);case 10:if(i=n.sent){n.next=15;break}return this.addLog("Could not retreive services"),r(new Error("Could not retreive services")),n.abrupt("return");case 15:if(this.addLog("Got service ".concat(u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING,", getting characteristics...")),this.device=e,this.characteristicsByName=Object.fromEntries(i.characteristics.map(function(t){return[C.CHARACTERISTIC_UUIDS_TO_NAMES[t.characteristic.toLowerCase()],{characteristicUUID:t.characteristic,serviceUUID:t.service,peripheralId:e.id}]})),this.addLog("Got characteristics."),"android"!==this.platform){n.next=22;break}return n.next=22,this.BleManager.requestMTU(e.id,w.ANDROID_MAX_MTU).then(function(e){a.addLog("Successfully updated Android MTU to ".concat(e," bytes. Requested MTU: ").concat(w.ANDROID_MAX_MTU," bytes."))}).catch(function(e){a.addLog("Failed to set Android MTU of ".concat(w.ANDROID_MAX_MTU," bytes. Error: ").concat(e))});case 22:this.addLog("Successfully connected to peripheral ".concat(e.id)),this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTED),t(),n.next=30;break;case 27:n.prev=27,n.t0=n.catch(0),r(n.t0);case 30:case"end":return n.stop()}},n,this,[[0,27]])}))}));case 1:case"end":return t.stop()}},t)}))}},{key:"disconnect",value:function(){var e;return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,!(this.isConnected()&&(null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.id))){t.next=4;break}return t.next=4,this.BleManager.disconnect(this.device.id);case 4:t.next=9;break;case 6:return t.prev=6,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 9:case"end":return t.stop()}},t,this,[[0,6]])}))}},{key:"getCharacteristicByName",value:function(e){var t;if(!(e in this.characteristicsByName))throw new Error("Characteristic by name ".concat(e," is not found"));return null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r;return this.connection$.pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?(r=t.getCharacteristicByName(n),a=r.peripheralId,c=r.serviceUUID,o=r.characteristicUUID,(0,l.defer)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.startNotification(a,c,o);case 4:this.addLog("Started notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}).pipe((0,p.switchMap)(function(){return t.bleEvents.didUpdateValueForCharacteristic$}),(0,f.finalize)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.stopNotification(a,c,o);case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}),(0,p.filter)(function(e){return e.characteristic===o}),(0,p.map)(function(e){var n=e.value;return(0,b.decode)(t.type,n)}),(0,E.stitchChunks)({delimiter:d.BLUETOOTH_CHUNK_DELIMITER}),(0,p.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}))):v.NEVER;var r,a,c,o}))}},{key:"readCharacteristic",value:function(e){var t,n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return s(this,void 0,void 0,regeneratorRuntime.mark(function r(){var i,a,c,o,s,u,d;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(this.addLog("Reading characteristic: ".concat(e)),i=this.getCharacteristicByName(e),a=i.peripheralId,c=i.serviceUUID,o=i.characteristicUUID){r.next=4;break}return r.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return r.prev=4,r.next=7,this.BleManager.read(a,c,o);case 7:return s=r.sent,u=(0,b.decode)(this.type,s),d=n?JSON.parse(u):u,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(d)),r.abrupt("return",d);case 14:return r.prev=14,r.t0=r.catch(4),r.abrupt("return",Promise.reject(new Error("readCharacteristic ".concat(e," error. ").concat(null!==(t=null===r.t0||void 0===r.t0?void 0:r.t0.message)&&void 0!==t?t:r.t0))));case 17:case"end":return r.stop()}},r,this,[[4,14]])}))}},{key:"writeCharacteristic",value:function(e,t){return s(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,a,c,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(this.addLog("Writing characteristic: ".concat(e)),r=this.getCharacteristicByName(e),i=r.peripheralId,a=r.serviceUUID,c=r.characteristicUUID){n.next=4;break}return n.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return o=(0,b.encode)(this.type,t),n.next=7,this.BleManager.write(i,a,c,o,T.REACT_NATIVE_MAX_BYTE_SIZE);case 7:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=!1,r=this.connection$.asObservable().pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?i.pendingActions$:v.NEVER}),(0,p.tap)(function(e){return s(i,void 0,void 0,regeneratorRuntime.mark(function t(){var r,i,a,c,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(a=this.getCharacteristicByName("actions"),c=a.peripheralId,o=a.serviceUUID,s=a.characteristicUUID,!(u=!!e.length)||n){t.next=13;break}return n=!0,t.prev=4,t.next=7,this.BleManager.startNotification(c,o,s);case 7:this.addLog("Started notifications for [actions] characteristic"),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(4),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 13:if(u||!n){t.next=24;break}return n=!1,t.prev=15,t.next=18,this.BleManager.stopNotification(c,o,s);case 18:this.addLog("Stopped notifications for actions characteristic"),t.next=24;break;case 21:t.prev=21,t.t1=t.catch(15),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(i=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==i?i:t.t1));case 24:case"end":return t.stop()}},t,this,[[4,10],[15,21]])}))})),e.pipe((0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?r:v.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return s(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,a,c,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,a=n.responseTimeout,c=void 0===a?O.DEFAULT_ACTION_RESPONSE_TIMEOUT:a,e.abrupt("return",new Promise(function(e,r){return s(o,void 0,void 0,regeneratorRuntime.mark(function a(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:o=(0,m.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&c?(this._addPendingAction(o),u=(0,l.timer)(c).subscribe(function(){d._removePendingAction(o),r(new Error("Action with id ".concat(o," timed out after ").concat(c,"ms")))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,p.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,g.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e)});case 4:case"end":return a.stop()}},a,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();exports.ReactNativeTransport=R;
653
653
  },{"@neurosity/ipk":"ZOGh","rxjs":"Zr8e","rxjs/operators":"v3iE","../utils/create6DigitPin":"UDAB","../utils/stitch":"V73a","../utils/encoding":"jTvL","../types":"iwtf","../constants":"dGLb","../utils/osHasBluetoothSupport":"rH2Y"}],"Oj1i":[function(require,module,exports) {
654
654
  "use strict";function n(n,e,t){return e in n?Object.defineProperty(n,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[e]=t,n}Object.defineProperty(exports,"__esModule",{value:!0}),exports.epoch=exports.bufferToEpoch=exports.addInfo=void 0;var e=require("rxjs"),t=require("rxjs/operators"),r="data",o=256,i=function(n){return n instanceof Object&&n===Object(n)},a=function(n){return"function"==typeof n},u=function(n){return function(e){var t;return Object.assign(Object.assign({},n),{info:Object.assign(Object.assign({},null!==(t=null==n?void 0:n.info)&&void 0!==t?t:{}),e||{})})}},f=function(n){return(0,e.pipe)((0,t.map)(function(e){if(!i(e)||!i(n)&&!a(n))return e;var t=a(n)?n(e):n;return u(e)(t)}))};exports.addInfo=f;var c=function(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:r;return n[0][e].map(function(t,r){return n.map(function(n){return n[e][r]})})},p=function(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},a=i.samplingRate,u=void 0===a?o:a,f=i.dataProp,p=void 0===f?r:f;return(0,e.pipe)((0,t.map)(function(e){var t;return n(t={},p,c(e,p)),n(t,"info",Object.assign(Object.assign({},e[0]&&e[0].info?e[0].info:{}),{startTime:e[0].timestamp,samplingRate:e[0].info&&e[0].info.samplingRate?e[0].info.samplingRate:u})),t}))};exports.bufferToEpoch=p;var s=function(n){var o=n.duration,i=n.interval,a=n.samplingRate,u=n.dataProp,f=void 0===u?r:u;return(0,e.pipe)((0,t.bufferCount)(i),(0,t.scan)(function(n,e){return n.concat(e).slice(n.length<o?0:-o)}),(0,t.filter)(function(n){return n.length===o}),(0,exports.bufferToEpoch)({samplingRate:a,dataProp:f}))};exports.epoch=s;
655
655
  },{"rxjs":"Zr8e","rxjs/operators":"v3iE"}],"WTrV":[function(require,module,exports) {
@@ -659,7 +659,7 @@ var r=require("./compare"),e=function(e,o,u){return r(e,o,u)>=0};module.exports=
659
659
  },{"rxjs":"Zr8e","rxjs/operators":"v3iE","./web/WebBluetoothTransport":"ouKb","./react-native/ReactNativeTransport":"FtS5","./utils/csvBufferToEpoch":"WTrV","./types":"iwtf","./utils/osHasBluetoothSupport":"rH2Y"}],"TICO":[function(require,module,exports) {
660
660
  "use strict";var e=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&("get"in i?t.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,o,i)}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),t=this&&this.__exportStar||function(t,r){for(var o in t)"default"===o||Object.prototype.hasOwnProperty.call(r,o)||e(r,t,o)};Object.defineProperty(exports,"__esModule",{value:!0}),t(require("./BluetoothClient"),exports),t(require("./web/WebBluetoothTransport"),exports),t(require("./react-native/ReactNativeTransport"),exports),t(require("./utils/osHasBluetoothSupport"),exports),t(require("./types/index"),exports);
661
661
  },{"./BluetoothClient":"fihV","./web/WebBluetoothTransport":"ouKb","./react-native/ReactNativeTransport":"FtS5","./utils/osHasBluetoothSupport":"rH2Y","./types/index":"iwtf"}],"BZP9":[function(require,module,exports) {
662
- "use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(t,n){return!n||"object"!==e(n)&&"function"!=typeof n?r(t):n}function r(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function n(e){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&o(e,t)}function o(e,t){return(o=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function u(e,t){return a(e)||s(e,t)||c()}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function s(e,t){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)){var r=[],n=!0,i=!1,o=void 0;try{for(var u,c=e[Symbol.iterator]();!(n=(u=c.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(s){i=!0,o=s}finally{try{n||null==c.return||c.return()}finally{if(i)throw o}}return r}}function a(e){if(Array.isArray(e))return e}function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function h(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function d(e,t,r){return t&&h(e.prototype,t),r&&h(e,r),e}var f=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&("get"in i?t.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,i)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),m=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),v=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&f(t,e,r);return m(t,e),t},p=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(i,o){function u(e){try{s(n.next(e))}catch(t){o(t)}}function c(e){try{s(n.throw(e))}catch(t){o(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(u,c)}s((n=n.apply(e,t||[])).next())})},b=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Notion=exports.Neurosity=void 0;var g=require("rxjs"),C=require("rxjs"),y=require("rxjs/operators"),w=b(require("fast-deep-equal")),O=require("./api/index"),k=require("./api/index"),S=require("./types/streaming"),M=require("./utils/subscription"),_=require("./types/status"),x=v(require("./utils/errors")),E=v(require("./utils/platform")),T=v(require("./utils/hapticEffects")),A=require("./utils/oauth"),D=require("./utils/oauth"),N=require("./api/https/createOAuthURL"),I=require("./api/https/getOAuthToken"),R=require("./utils/is-node"),F=require("./utils/metrics"),P=require("./api/bluetooth"),j=require("./api/bluetooth/types"),L={timesync:!1,autoSelectDevice:!0,streamingMode:S.STREAMING_MODE.WIFI_ONLY,emulator:!1,emulatorHost:"localhost",emulatorAuthPort:9099,emulatorDatabasePort:9e3,emulatorFunctionsPort:5001,emulatorFirestorePort:8080,emulatorOptions:{}},U=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};l(this,e),this.streamingMode$=new C.ReplaySubject(1);var r=t.streamingMode,n=t.bluetoothTransport;this.options=Object.freeze(Object.assign(Object.assign({},L),t)),this.cloudClient=new O.CloudClient(this.options),n&&(this.bluetoothClient=new P.BluetoothClient({selectedDevice$:this.onDeviceChange(),createBluetoothToken:this.createBluetoothToken.bind(this),transport:n})),this._initStreamingMode(r,!!n)}return d(e,[{key:"_initStreamingMode",value:function(e,t){var r=[S.STREAMING_MODE.BLUETOOTH_WITH_WIFI_FALLBACK,S.STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK].includes(e),n=!Object.values(S.STREAMING_MODE).includes(e),i=r&&!t;this.isMissingBluetoothTransport=i,!e||n||i?this.streamingMode$.next(S.STREAMING_MODE.WIFI_ONLY):this.streamingMode$.next(e)}},{key:"streamingState",value:function(){var e=this,t=function(e){return[_.STATUS.ONLINE,_.STATUS.UPDATING].includes(e)};return this.streamingMode$.pipe((0,y.switchMap)(function(r){return e.isMissingBluetoothTransport?e.cloudClient.status().pipe((0,y.map)(function(e){var n=e.state;return{connected:t(n),streamingMode:r,activeMode:S.STREAMING_TYPE.WIFI}})):e.onDeviceChange().pipe((0,y.switchMap)(function(n){return n?(0,g.combineLatest)({wifiStatus:e.cloudClient.status(),bluetoothConnection:(null==e?void 0:e.bluetoothClient)?e.bluetoothClient.connection():(0,g.of)(j.BLUETOOTH_CONNECTION.DISCONNECTED)}).pipe((0,y.map)(function(e){var n=e.wifiStatus,i=e.bluetoothConnection===j.BLUETOOTH_CONNECTION.CONNECTED;switch(r){default:case S.STREAMING_MODE.WIFI_ONLY:return{connected:t(n.state),streamingMode:r,activeMode:S.STREAMING_TYPE.WIFI};case S.STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:return{connected:t(n.state)||!i?t(n.state):i,streamingMode:r,activeMode:t(n.state)||!i?S.STREAMING_TYPE.WIFI:S.STREAMING_TYPE.BLUETOOTH};case S.STREAMING_MODE.BLUETOOTH_WITH_WIFI_FALLBACK:return{connected:!!i||t(n.state),streamingMode:r,activeMode:i?S.STREAMING_TYPE.BLUETOOTH:S.STREAMING_TYPE.WIFI}}}),(0,y.distinctUntilChanged)(function(e,t){return(0,w.default)(e,t)})):C.EMPTY}))}))}},{key:"_withStreamingModeObservable",value:function(e){var t=e.wifi,r=e.bluetooth;return this.streamingState().pipe((0,y.switchMap)(function(e){switch(e.activeMode){case S.STREAMING_TYPE.WIFI:return t();case S.STREAMING_TYPE.BLUETOOTH:return r();default:return t()}}))}},{key:"_withStreamingModePromise",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.wifi,n=e.bluetooth,t.next=3,(0,C.firstValueFrom)(this.streamingState());case 3:i=t.sent,o=i.activeMode,t.t0=o,t.next=t.t0===S.STREAMING_TYPE.WIFI?8:t.t0===S.STREAMING_TYPE.BLUETOOTH?11:14;break;case 8:return t.next=10,r();case 10:return t.abrupt("return",t.sent);case 11:return t.next=13,n();case 13:return t.abrupt("return",t.sent);case 14:return t.next=16,r();case 16:return t.abrupt("return",t.sent);case 17:case"end":return t.stop()}},t,this)}))}},{key:"_getCloudMetricDependencies",value:function(){return{options:this.options,cloudClient:this.cloudClient,onDeviceChange:this.onDeviceChange.bind(this),status:this.status.bind(this)}}},{key:"login",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.login(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}},{key:"logout",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.logout();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"__getApp",value:function(){return this.cloudClient.__getApp()}},{key:"onAuthStateChanged",value:function(){return this.cloudClient.onAuthStateChanged()}},{key:"addDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"addDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.addDevice(e)}},{key:"removeDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"removeDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.removeDevice(e)}},{key:"transferDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"transferDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.transferDevice(e)}},{key:"onUserDevicesChange",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"onUserDevicesChange"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.onUserDevicesChange()}},{key:"onUserClaimsChange",value:function(){return this.cloudClient.onUserClaimsChange()}},{key:"getDevices",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.getDevices();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"selectDevice",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(r=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"selectDevice"),n=u(r,2),i=n[0],o=n[1],!i){t.next=3;break}return t.abrupt("return",Promise.reject(o));case 3:return t.next=5,this.cloudClient.selectDevice(e);case 5:return t.abrupt("return",t.sent);case 6:case"end":return t.stop()}},t,this)}))}},{key:"getSelectedDevice",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t,r,n,i;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"getSelectedDevice"),r=u(t,2),n=r[0],i=r[1],!n){e.next=3;break}return e.abrupt("return",Promise.reject(i));case 3:return e.next=5,this.cloudClient.getSelectedDevice();case 5:return e.abrupt("return",e.sent);case 6:case"end":return e.stop()}},e,this)}))}},{key:"getInfo",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t,r,n,i,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}return e.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(t=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"getInfo"),r=u(t,2),n=r[0],i=r[1],!n){e.next=7;break}return e.abrupt("return",Promise.reject(i));case 7:return e.next=9,this._withStreamingModePromise({wifi:function(){return o.cloudClient.getInfo()},bluetooth:function(){return o.bluetoothClient.getInfo()}});case 9:return e.abrupt("return",e.sent);case 10:case"end":return e.stop()}},e,this)}))}},{key:"onDeviceChange",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"onDeviceChange"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.onDeviceChange()}},{key:"disconnect",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._withStreamingModePromise({wifi:function(){return t.cloudClient.disconnect()},bluetooth:function(){return t.bluetoothClient.disconnect()}});case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"dispatchAction",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o,c=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}return t.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(r=(0,D.validateOAuthScopeForAction)(this.cloudClient.userClaims,e),n=u(r,2),i=n[0],o=n[1],!i){t.next=7;break}return t.abrupt("return",Promise.reject(o));case 7:return t.next=9,this._withStreamingModePromise({wifi:function(){return c.cloudClient.dispatchAction(e)},bluetooth:function(){return c.bluetoothClient.dispatchAction(e)}});case 9:return t.abrupt("return",t.sent);case 10:case"end":return t.stop()}},t,this)}))}},{key:"addMarker",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}throw x.mustSelectDevice;case 4:if(e){t.next=6;break}throw new Error("".concat(x.prefix,"A label is required for addMarker"));case 6:return t.next=8,this._withStreamingModePromise({wifi:function(){return r.cloudClient.dispatchAction({command:"marker",action:"add",message:{label:e,timestamp:r.cloudClient.timestamp}})},bluetooth:function(){return r.bluetoothClient.addMarker(e)}});case 8:return t.abrupt("return",t.sent);case 9:case"end":return t.stop()}},t,this)}))}},{key:"haptics",value:function(e){var t;return p(this,void 0,void 0,regeneratorRuntime.mark(function r(){var n,i,o,u,c,s,a=this;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return n="haptics",r.next=3,this.cloudClient.didSelectDevice();case 3:if(r.sent){r.next=5;break}return r.abrupt("return",Promise.reject(x.mustSelectDevice));case 5:return r.next=7,this.getSelectedDevice();case 7:if(r.t1=t=r.sent,r.t0=null===r.t1,r.t0){r.next=11;break}r.t0=void 0===t;case 11:if(!r.t0){r.next=15;break}r.t2=void 0,r.next=16;break;case 15:r.t2=t.modelVersion;case 16:if(i=r.t2,E.supportsHaptics(i)){r.next=20;break}return r.abrupt("return",Promise.reject(x.metricNotSupportedByModel(n,i)));case 20:o=E.getPlatformHapticMotors(i),r.t3=regeneratorRuntime.keys(e);case 22:if((r.t4=r.t3()).done){r.next=33;break}if(u=r.t4.value,Object.keys(o).includes(u)){r.next=26;break}return r.abrupt("return",Promise.reject(x.locationNotFound(u,i)));case 26:if(c=e[u],7,!(c.length>7)){r.next=30;break}return r.abrupt("return",Promise.reject(x.exceededMaxItems(7)));case 30:o[u]=c,r.next=22;break;case 33:return s={command:n,action:"queue",responseRequired:!0,responseTimeout:1e3,message:{effects:o}},r.next=36,this._withStreamingModePromise({wifi:function(){return a.cloudClient.dispatchAction(s)},bluetooth:function(){return a.bluetoothClient.dispatchAction(s)}});case 36:return r.abrupt("return",r.sent);case 37:case"end":return r.stop()}},r,this)}))}},{key:"getHapticEffects",value:function(){return T}},{key:"accelerometer",value:function(){var e=this,t="accelerometer",r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,t),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this.onDeviceChange().pipe((0,y.switchMap)(function(r){var n=(null==r?void 0:r.modelVersion)||E.MODEL_VERSION_1;return E.supportsAccel(n)?e._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:t,labels:(0,M.getLabels)(t),atomic:!0})},bluetooth:function(){return e.bluetoothClient.accelerometer()}}):(0,g.throwError)(function(){return x.metricNotSupportedByModel(t,n)})}))}},{key:"brainwaves",value:function(e){var t=this,r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"brainwaves"),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(t._getCloudMetricDependencies(),{metric:"brainwaves",labels:e?[e]:[],atomic:!1})},bluetooth:function(){return t.bluetoothClient.brainwaves(e)}})}},{key:"calm",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"calm"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:"awareness",labels:["calm"],atomic:!1})},bluetooth:function(){return e.bluetoothClient.calm()}})}},{key:"signalQuality",value:function(){var e=this,t="signalQuality",r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,t),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:t,labels:(0,M.getLabels)(t),atomic:!0})},bluetooth:function(){return e.bluetoothClient.signalQuality()}})}},{key:"settings",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"settings"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.observeNamespace("settings")}},{key:"focus",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"focus"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:"awareness",labels:["focus"],atomic:!1})},bluetooth:function(){return e.bluetoothClient.focus()}})}},{key:"kinesis",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"kinesis"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):(0,F.getCloudMetric)(this._getCloudMetricDependencies(),{metric:"kinesis",labels:e?[e]:[],atomic:!1})}},{key:"predictions",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"predictions"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):(0,F.getCloudMetric)(this._getCloudMetricDependencies(),{metric:"predictions",labels:e?[e]:[],atomic:!1})}},{key:"status",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"status"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return e.cloudClient.status()},bluetooth:function(){return e.bluetoothClient.status()}})}},{key:"changeSettings",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}return t.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(r=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"changeSettings"),n=u(r,2),i=n[0],o=n[1],!i){t.next=7;break}return t.abrupt("return",Promise.reject(o));case 7:return t.next=9,this.cloudClient.changeSettings(e);case 9:return t.abrupt("return",t.sent);case 10:case"end":return t.stop()}},t,this)}))}},{key:"goOffline",value:function(){this.cloudClient.goOffline()}},{key:"goOnline",value:function(){this.cloudClient.goOnline()}},{key:"createAccount",value:function(e){return this.cloudClient.createAccount(e)}},{key:"deleteAccount",value:function(){return this.cloudClient.deleteAccount()}},{key:"createBluetoothToken",value:function(){return this.cloudClient.createBluetoothToken()}},{key:"createCustomToken",value:function(){return this.cloudClient.createCustomToken()}},{key:"getTimesyncOffset",value:function(){return this.options.timesync||console.warn("getTimesyncOffset() requires options.timesync to be true."),this.options.timesync?this.cloudClient.getTimesyncOffset():0}},{key:"createOAuthURL",value:function(e){return R.isNode?(0,N.createOAuthURL)(e,this.options):Promise.reject(new Error("".concat(x.prefix,"the createOAuthURL method must be used on the server side (node.js) for security reasons.")))}},{key:"getOAuthToken",value:function(e){return R.isNode?(0,I.getOAuthToken)(e,this.options):Promise.reject(new Error("".concat(x.prefix,"the getOAuthToken method must be used on the server side (node.js) for security reasons.")))}},{key:"removeOAuthAccess",value:function(){return this.cloudClient.removeOAuthAccess()}},{key:"onUserExperiments",value:function(){return this.cloudClient.onUserExperiments()}},{key:"deleteUserExperiment",value:function(e){return this.cloudClient.deleteUserExperiment(e)}},{key:"bluetooth",get:function(){return null==this?void 0:this.bluetoothClient}},{key:"training",get:function(){var e=this;return{record:function(t){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){var r,n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return r=this.cloudClient.user&&"uid"in this.cloudClient.user?this.cloudClient.user.uid:null,n=Object.assign(Object.assign({fit:!1,baseline:!1,timestamp:this.cloudClient.timestamp},t),{userId:r}),e.next=8,this.cloudClient.actions.dispatch({command:"training",action:"record",message:n});case 8:case"end":return e.stop()}},e,this)}))},stop:function(t){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return e.next=6,this.cloudClient.actions.dispatch({command:"training",action:"stop",message:Object.assign({},t)});case 6:case"end":return e.stop()}},e,this)}))},stopAll:function(){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return e.next=6,this.cloudClient.actions.dispatch({command:"training",action:"stopAll",message:{}});case 6:case"end":return e.stop()}},e,this)}))}}}}]),e}();exports.Neurosity=U,U.credentialWithLink=k.credentialWithLink,U.createUser=O.createUser,U.SERVER_TIMESTAMP=k.SERVER_TIMESTAMP;var q=function(e){function r(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return l(this,r),e=t(this,n(r).call(this,i)),console.log("The Notion class is deprecated and will be removed in the next version of the SDK. Please use the Neurosity class instead. e.g. new Notion() => new Neurosity()"),e}return i(r,U),r}();exports.Notion=q;
662
+ "use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(t,n){return!n||"object"!==e(n)&&"function"!=typeof n?r(t):n}function r(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function n(e){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&o(e,t)}function o(e,t){return(o=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function u(e,t){return a(e)||c(e,t)||s()}function s(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function c(e,t){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)){var r=[],n=!0,i=!1,o=void 0;try{for(var u,s=e[Symbol.iterator]();!(n=(u=s.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(c){i=!0,o=c}finally{try{n||null==s.return||s.return()}finally{if(i)throw o}}return r}}function a(e){if(Array.isArray(e))return e}function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function h(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function d(e,t,r){return t&&h(e.prototype,t),r&&h(e,r),e}var f=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&("get"in i?t.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,i)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),m=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),v=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&f(t,e,r);return m(t,e),t},p=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(i,o){function u(e){try{c(n.next(e))}catch(t){o(t)}}function s(e){try{c(n.throw(e))}catch(t){o(t)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(u,s)}c((n=n.apply(e,t||[])).next())})},b=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Notion=exports.Neurosity=void 0;var g=require("rxjs"),C=require("rxjs"),y=require("rxjs/operators"),w=b(require("fast-deep-equal")),O=require("./api/index"),k=require("./api/index"),S=require("./types/streaming"),M=require("./utils/subscription"),_=require("./types/status"),x=v(require("./utils/errors")),E=v(require("./utils/platform")),T=v(require("./utils/hapticEffects")),A=require("./utils/oauth"),D=require("./utils/oauth"),N=require("./api/https/createOAuthURL"),I=require("./api/https/getOAuthToken"),R=require("./utils/is-node"),F=require("./utils/metrics"),P=require("./api/bluetooth"),j=require("./api/bluetooth/types"),L={timesync:!1,autoSelectDevice:!0,streamingMode:S.STREAMING_MODE.WIFI_ONLY,emulator:!1,emulatorHost:"localhost",emulatorAuthPort:9099,emulatorDatabasePort:9e3,emulatorFunctionsPort:5001,emulatorFirestorePort:8080,emulatorOptions:{}},U=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};l(this,e),this.streamingMode$=new C.ReplaySubject(1);var r=t.streamingMode,n=t.bluetoothTransport;this.options=Object.freeze(Object.assign(Object.assign({},L),t)),this.cloudClient=new O.CloudClient(this.options),n&&(this.bluetoothClient=new P.BluetoothClient({selectedDevice$:this.onDeviceChange(),createBluetoothToken:this.createBluetoothToken.bind(this),transport:n})),this._initStreamingMode(r,!!n)}return d(e,[{key:"_initStreamingMode",value:function(e,t){var r=[S.STREAMING_MODE.BLUETOOTH_WITH_WIFI_FALLBACK,S.STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK].includes(e),n=!Object.values(S.STREAMING_MODE).includes(e),i=r&&!t;this.isMissingBluetoothTransport=i,!e||n||i?this.streamingMode$.next(S.STREAMING_MODE.WIFI_ONLY):this.streamingMode$.next(e)}},{key:"streamingState",value:function(){var e=this,t=function(e){return[_.STATUS.ONLINE,_.STATUS.UPDATING].includes(e)};return this.streamingMode$.pipe((0,y.switchMap)(function(r){return e.onDeviceChange().pipe((0,y.switchMap)(function(n){return n?e.isMissingBluetoothTransport||!(0,P.osHasBluetoothSupport)(n)?e.cloudClient.status().pipe((0,y.map)(function(e){var n=e.state;return{connected:t(n),streamingMode:r,activeMode:S.STREAMING_TYPE.WIFI}})):(0,g.combineLatest)({wifiStatus:e.cloudClient.status(),bluetoothConnection:(null==e?void 0:e.bluetoothClient)?e.bluetoothClient.connection():(0,g.of)(j.BLUETOOTH_CONNECTION.DISCONNECTED)}).pipe((0,y.map)(function(e){var n=e.wifiStatus,i=e.bluetoothConnection===j.BLUETOOTH_CONNECTION.CONNECTED;switch(r){default:case S.STREAMING_MODE.WIFI_ONLY:return{connected:t(n.state),streamingMode:r,activeMode:S.STREAMING_TYPE.WIFI};case S.STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:return{connected:t(n.state)||!i?t(n.state):i,streamingMode:r,activeMode:t(n.state)||!i?S.STREAMING_TYPE.WIFI:S.STREAMING_TYPE.BLUETOOTH};case S.STREAMING_MODE.BLUETOOTH_WITH_WIFI_FALLBACK:return{connected:!!i||t(n.state),streamingMode:r,activeMode:i?S.STREAMING_TYPE.BLUETOOTH:S.STREAMING_TYPE.WIFI}}}),(0,y.distinctUntilChanged)(function(e,t){return(0,w.default)(e,t)})):C.EMPTY}))}))}},{key:"_withStreamingModeObservable",value:function(e){var t=e.wifi,r=e.bluetooth;return this.streamingState().pipe((0,y.switchMap)(function(e){switch(e.activeMode){case S.STREAMING_TYPE.WIFI:return t();case S.STREAMING_TYPE.BLUETOOTH:return r();default:return t()}}))}},{key:"_withStreamingModePromise",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.wifi,n=e.bluetooth,t.next=3,(0,C.firstValueFrom)(this.streamingState());case 3:i=t.sent,o=i.activeMode,t.t0=o,t.next=t.t0===S.STREAMING_TYPE.WIFI?8:t.t0===S.STREAMING_TYPE.BLUETOOTH?11:14;break;case 8:return t.next=10,r();case 10:return t.abrupt("return",t.sent);case 11:return t.next=13,n();case 13:return t.abrupt("return",t.sent);case 14:return t.next=16,r();case 16:return t.abrupt("return",t.sent);case 17:case"end":return t.stop()}},t,this)}))}},{key:"_getCloudMetricDependencies",value:function(){return{options:this.options,cloudClient:this.cloudClient,onDeviceChange:this.onDeviceChange.bind(this),status:this.status.bind(this)}}},{key:"login",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.login(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}},{key:"logout",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.logout();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"__getApp",value:function(){return this.cloudClient.__getApp()}},{key:"onAuthStateChanged",value:function(){return this.cloudClient.onAuthStateChanged()}},{key:"addDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"addDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.addDevice(e)}},{key:"removeDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"removeDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.removeDevice(e)}},{key:"transferDevice",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"transferDevice"),2),r=t[0],n=t[1];return r?Promise.reject(n):this.cloudClient.transferDevice(e)}},{key:"onUserDevicesChange",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"onUserDevicesChange"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.onUserDevicesChange()}},{key:"onUserClaimsChange",value:function(){return this.cloudClient.onUserClaimsChange()}},{key:"getDevices",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.getDevices();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"selectDevice",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(r=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"selectDevice"),n=u(r,2),i=n[0],o=n[1],!i){t.next=3;break}return t.abrupt("return",Promise.reject(o));case 3:return t.next=5,this.cloudClient.selectDevice(e);case 5:return t.abrupt("return",t.sent);case 6:case"end":return t.stop()}},t,this)}))}},{key:"getSelectedDevice",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t,r,n,i;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"getSelectedDevice"),r=u(t,2),n=r[0],i=r[1],!n){e.next=3;break}return e.abrupt("return",Promise.reject(i));case 3:return e.next=5,this.cloudClient.getSelectedDevice();case 5:return e.abrupt("return",e.sent);case 6:case"end":return e.stop()}},e,this)}))}},{key:"getInfo",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t,r,n,i,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}return e.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(t=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"getInfo"),r=u(t,2),n=r[0],i=r[1],!n){e.next=7;break}return e.abrupt("return",Promise.reject(i));case 7:return e.next=9,this._withStreamingModePromise({wifi:function(){return o.cloudClient.getInfo()},bluetooth:function(){return o.bluetoothClient.getInfo()}});case 9:return e.abrupt("return",e.sent);case 10:case"end":return e.stop()}},e,this)}))}},{key:"onDeviceChange",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"onDeviceChange"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.onDeviceChange()}},{key:"disconnect",value:function(){return p(this,void 0,void 0,regeneratorRuntime.mark(function e(){var t=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._withStreamingModePromise({wifi:function(){return t.cloudClient.disconnect()},bluetooth:function(){return t.bluetoothClient.disconnect()}});case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)}))}},{key:"dispatchAction",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o,s=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}return t.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(r=(0,D.validateOAuthScopeForAction)(this.cloudClient.userClaims,e),n=u(r,2),i=n[0],o=n[1],!i){t.next=7;break}return t.abrupt("return",Promise.reject(o));case 7:return t.next=9,this._withStreamingModePromise({wifi:function(){return s.cloudClient.dispatchAction(e)},bluetooth:function(){return s.bluetoothClient.dispatchAction(e)}});case 9:return t.abrupt("return",t.sent);case 10:case"end":return t.stop()}},t,this)}))}},{key:"addMarker",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}throw x.mustSelectDevice;case 4:if(e){t.next=6;break}throw new Error("".concat(x.prefix,"A label is required for addMarker"));case 6:return t.next=8,this._withStreamingModePromise({wifi:function(){return r.cloudClient.dispatchAction({command:"marker",action:"add",message:{label:e,timestamp:r.cloudClient.timestamp}})},bluetooth:function(){return r.bluetoothClient.addMarker(e)}});case 8:return t.abrupt("return",t.sent);case 9:case"end":return t.stop()}},t,this)}))}},{key:"haptics",value:function(e){var t;return p(this,void 0,void 0,regeneratorRuntime.mark(function r(){var n,i,o,u,s,c,a=this;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return n="haptics",r.next=3,this.cloudClient.didSelectDevice();case 3:if(r.sent){r.next=5;break}return r.abrupt("return",Promise.reject(x.mustSelectDevice));case 5:return r.next=7,this.getSelectedDevice();case 7:if(r.t1=t=r.sent,r.t0=null===r.t1,r.t0){r.next=11;break}r.t0=void 0===t;case 11:if(!r.t0){r.next=15;break}r.t2=void 0,r.next=16;break;case 15:r.t2=t.modelVersion;case 16:if(i=r.t2,E.supportsHaptics(i)){r.next=20;break}return r.abrupt("return",Promise.reject(x.metricNotSupportedByModel(n,i)));case 20:o=E.getPlatformHapticMotors(i),r.t3=regeneratorRuntime.keys(e);case 22:if((r.t4=r.t3()).done){r.next=33;break}if(u=r.t4.value,Object.keys(o).includes(u)){r.next=26;break}return r.abrupt("return",Promise.reject(x.locationNotFound(u,i)));case 26:if(s=e[u],7,!(s.length>7)){r.next=30;break}return r.abrupt("return",Promise.reject(x.exceededMaxItems(7)));case 30:o[u]=s,r.next=22;break;case 33:return c={command:n,action:"queue",responseRequired:!0,responseTimeout:1e3,message:{effects:o}},r.next=36,this._withStreamingModePromise({wifi:function(){return a.cloudClient.dispatchAction(c)},bluetooth:function(){return a.bluetoothClient.dispatchAction(c)}});case 36:return r.abrupt("return",r.sent);case 37:case"end":return r.stop()}},r,this)}))}},{key:"getHapticEffects",value:function(){return T}},{key:"accelerometer",value:function(){var e=this,t="accelerometer",r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,t),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this.onDeviceChange().pipe((0,y.switchMap)(function(r){var n=(null==r?void 0:r.modelVersion)||E.MODEL_VERSION_1;return E.supportsAccel(n)?e._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:t,labels:(0,M.getLabels)(t),atomic:!0})},bluetooth:function(){return e.bluetoothClient.accelerometer()}}):(0,g.throwError)(function(){return x.metricNotSupportedByModel(t,n)})}))}},{key:"brainwaves",value:function(e){var t=this,r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"brainwaves"),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(t._getCloudMetricDependencies(),{metric:"brainwaves",labels:e?[e]:[],atomic:!1})},bluetooth:function(){return t.bluetoothClient.brainwaves(e)}})}},{key:"calm",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"calm"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:"awareness",labels:["calm"],atomic:!1})},bluetooth:function(){return e.bluetoothClient.calm()}})}},{key:"signalQuality",value:function(){var e=this,t="signalQuality",r=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,t),2),n=r[0],i=r[1];return n?(0,g.throwError)(function(){return i}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:t,labels:(0,M.getLabels)(t),atomic:!0})},bluetooth:function(){return e.bluetoothClient.signalQuality()}})}},{key:"settings",value:function(){var e=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"settings"),2),t=e[0],r=e[1];return t?(0,g.throwError)(function(){return r}):this.cloudClient.observeNamespace("settings")}},{key:"focus",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"focus"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return(0,F.getCloudMetric)(e._getCloudMetricDependencies(),{metric:"awareness",labels:["focus"],atomic:!1})},bluetooth:function(){return e.bluetoothClient.focus()}})}},{key:"kinesis",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"kinesis"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):(0,F.getCloudMetric)(this._getCloudMetricDependencies(),{metric:"kinesis",labels:e?[e]:[],atomic:!1})}},{key:"predictions",value:function(e){var t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"predictions"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):(0,F.getCloudMetric)(this._getCloudMetricDependencies(),{metric:"predictions",labels:e?[e]:[],atomic:!1})}},{key:"status",value:function(){var e=this,t=u((0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"status"),2),r=t[0],n=t[1];return r?(0,g.throwError)(function(){return n}):this._withStreamingModeObservable({wifi:function(){return e.cloudClient.status()},bluetooth:function(){return e.bluetoothClient.status()}})}},{key:"changeSettings",value:function(e){return p(this,void 0,void 0,regeneratorRuntime.mark(function t(){var r,n,i,o;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.cloudClient.didSelectDevice();case 2:if(t.sent){t.next=4;break}return t.abrupt("return",Promise.reject(x.mustSelectDevice));case 4:if(r=(0,A.validateOAuthScopeForFunctionName)(this.cloudClient.userClaims,"changeSettings"),n=u(r,2),i=n[0],o=n[1],!i){t.next=7;break}return t.abrupt("return",Promise.reject(o));case 7:return t.next=9,this.cloudClient.changeSettings(e);case 9:return t.abrupt("return",t.sent);case 10:case"end":return t.stop()}},t,this)}))}},{key:"goOffline",value:function(){this.cloudClient.goOffline()}},{key:"goOnline",value:function(){this.cloudClient.goOnline()}},{key:"createAccount",value:function(e){return this.cloudClient.createAccount(e)}},{key:"deleteAccount",value:function(){return this.cloudClient.deleteAccount()}},{key:"createBluetoothToken",value:function(){return this.cloudClient.createBluetoothToken()}},{key:"createCustomToken",value:function(){return this.cloudClient.createCustomToken()}},{key:"getTimesyncOffset",value:function(){return this.options.timesync||console.warn("getTimesyncOffset() requires options.timesync to be true."),this.options.timesync?this.cloudClient.getTimesyncOffset():0}},{key:"createOAuthURL",value:function(e){return R.isNode?(0,N.createOAuthURL)(e,this.options):Promise.reject(new Error("".concat(x.prefix,"the createOAuthURL method must be used on the server side (node.js) for security reasons.")))}},{key:"getOAuthToken",value:function(e){return R.isNode?(0,I.getOAuthToken)(e,this.options):Promise.reject(new Error("".concat(x.prefix,"the getOAuthToken method must be used on the server side (node.js) for security reasons.")))}},{key:"removeOAuthAccess",value:function(){return this.cloudClient.removeOAuthAccess()}},{key:"onUserExperiments",value:function(){return this.cloudClient.onUserExperiments()}},{key:"deleteUserExperiment",value:function(e){return this.cloudClient.deleteUserExperiment(e)}},{key:"bluetooth",get:function(){return null==this?void 0:this.bluetoothClient}},{key:"training",get:function(){var e=this;return{record:function(t){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){var r,n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return r=this.cloudClient.user&&"uid"in this.cloudClient.user?this.cloudClient.user.uid:null,n=Object.assign(Object.assign({fit:!1,baseline:!1,timestamp:this.cloudClient.timestamp},t),{userId:r}),e.next=8,this.cloudClient.actions.dispatch({command:"training",action:"record",message:n});case 8:case"end":return e.stop()}},e,this)}))},stop:function(t){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return e.next=6,this.cloudClient.actions.dispatch({command:"training",action:"stop",message:Object.assign({},t)});case 6:case"end":return e.stop()}},e,this)}))},stopAll:function(){return p(e,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.cloudClient.didSelectDevice();case 2:if(e.sent){e.next=4;break}throw x.mustSelectDevice;case 4:return e.next=6,this.cloudClient.actions.dispatch({command:"training",action:"stopAll",message:{}});case 6:case"end":return e.stop()}},e,this)}))}}}}]),e}();exports.Neurosity=U,U.credentialWithLink=k.credentialWithLink,U.createUser=O.createUser,U.SERVER_TIMESTAMP=k.SERVER_TIMESTAMP;var q=function(e){function r(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return l(this,r),e=t(this,n(r).call(this,i)),console.log("The Notion class is deprecated and will be removed in the next version of the SDK. Please use the Neurosity class instead. e.g. new Notion() => new Neurosity()"),e}return i(r,U),r}();exports.Notion=q;
663
663
  },{"rxjs":"Zr8e","rxjs/operators":"v3iE","fast-deep-equal":"jIGR","./api/index":"LXvB","./types/streaming":"rekm","./utils/subscription":"yLLB","./types/status":"Ttuy","./utils/errors":"WDyG","./utils/platform":"wAkn","./utils/hapticEffects":"lLai","./utils/oauth":"xIH5","./api/https/createOAuthURL":"MDrB","./api/https/getOAuthToken":"nfg4","./utils/is-node":"vsps","./utils/metrics":"DCuD","./api/bluetooth":"TICO","./api/bluetooth/types":"iwtf"}],"QCba":[function(require,module,exports) {
664
664
  "use strict";var e=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&("get"in o?t.__esModule:!o.writable&&!o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),t=this&&this.__exportStar||function(t,r){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(r,i)||e(r,t,i)};Object.defineProperty(exports,"__esModule",{value:!0}),t(require("./Neurosity"),exports),t(require("./api/bluetooth"),exports);
665
665
  },{"./Neurosity":"BZP9","./api/bluetooth":"TICO"}]},{},["QCba"], null)
@@ -46842,10 +46842,14 @@ class ReactNativeTransport {
46842
46842
  share());
46843
46843
  }
46844
46844
  scan(options) {
46845
- var _a, _b;
46845
+ var _a, _b, _c;
46846
46846
  const RESCAN_INTERVAL = 10000; // 10 seconds
46847
46847
  const seconds = (_a = options === null || options === void 0 ? void 0 : options.seconds) !== null && _a !== void 0 ? _a : RESCAN_INTERVAL / 1000;
46848
46848
  const once = (_b = options === null || options === void 0 ? void 0 : options.once) !== null && _b !== void 0 ? _b : false;
46849
+ // If we are already connected to a peripheral and start scanning,
46850
+ // be default, it will set the connection status to SCANNING and not
46851
+ // update it back if no device is connected to
46852
+ const skipConnectionUpdate = (_c = options === null || options === void 0 ? void 0 : options.skipConnectionUpdate) !== null && _c !== void 0 ? _c : false;
46849
46853
  const serviceUUIDs = [BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
46850
46854
  const allowDuplicates = true;
46851
46855
  const scanOptions = {};
@@ -46869,7 +46873,9 @@ class ReactNativeTransport {
46869
46873
  ? scanOnce$
46870
46874
  : timer(0, RESCAN_INTERVAL).pipe(switchMap(() => scanOnce$));
46871
46875
  const peripherals$ = scan$.pipe(tap(() => {
46872
- this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
46876
+ if (!skipConnectionUpdate) {
46877
+ this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
46878
+ }
46873
46879
  }), takeUntil(this.onDisconnected$), switchMap(() => this.bleEvents.discoverPeripheral$),
46874
46880
  // Filter out devices that are not Neurosity devices
46875
46881
  filter((peripheral) => {
@@ -47605,16 +47611,20 @@ class Neurosity {
47605
47611
  streamingState() {
47606
47612
  const isWifiOnline = (state) => [STATUS.ONLINE, STATUS.UPDATING].includes(state);
47607
47613
  return this.streamingMode$.pipe(switchMap((streamingMode) => {
47608
- if (this.isMissingBluetoothTransport) {
47609
- return this.cloudClient.status().pipe(map(({ state }) => ({
47610
- connected: isWifiOnline(state),
47611
- streamingMode,
47612
- activeMode: STREAMING_TYPE.WIFI
47613
- })));
47614
- }
47615
- return this.onDeviceChange().pipe(switchMap((selectDevice) => !selectDevice
47616
- ? EMPTY
47617
- : combineLatest({
47614
+ return this.onDeviceChange().pipe(switchMap((selectDevice) => {
47615
+ if (!selectDevice) {
47616
+ return EMPTY;
47617
+ }
47618
+ const isUnableToUseBluetooth = this.isMissingBluetoothTransport ||
47619
+ !osHasBluetoothSupport(selectDevice);
47620
+ if (isUnableToUseBluetooth) {
47621
+ return this.cloudClient.status().pipe(map(({ state }) => ({
47622
+ connected: isWifiOnline(state),
47623
+ streamingMode,
47624
+ activeMode: STREAMING_TYPE.WIFI
47625
+ })));
47626
+ }
47627
+ return combineLatest({
47618
47628
  wifiStatus: this.cloudClient.status(),
47619
47629
  bluetoothConnection: !!(this === null || this === void 0 ? void 0 : this.bluetoothClient)
47620
47630
  ? this.bluetoothClient.connection()
@@ -47631,13 +47641,11 @@ class Neurosity {
47631
47641
  };
47632
47642
  case STREAMING_MODE.WIFI_WITH_BLUETOOTH_FALLBACK:
47633
47643
  return {
47634
- connected: isWifiOnline(wifiStatus.state) ||
47635
- !isBluetoothConnected
47644
+ connected: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47636
47645
  ? isWifiOnline(wifiStatus.state)
47637
47646
  : isBluetoothConnected,
47638
47647
  streamingMode,
47639
- activeMode: isWifiOnline(wifiStatus.state) ||
47640
- !isBluetoothConnected
47648
+ activeMode: isWifiOnline(wifiStatus.state) || !isBluetoothConnected
47641
47649
  ? STREAMING_TYPE.WIFI
47642
47650
  : STREAMING_TYPE.BLUETOOTH
47643
47651
  };
@@ -47652,7 +47660,8 @@ class Neurosity {
47652
47660
  : STREAMING_TYPE.WIFI
47653
47661
  };
47654
47662
  }
47655
- }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)))));
47663
+ }), distinctUntilChanged((a, b) => fastDeepEqual(a, b)));
47664
+ }));
47656
47665
  }));
47657
47666
  }
47658
47667
  /**