@neurosity/sdk 6.0.0-next.11
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.
- package/LICENSE +21 -0
- package/README.md +46 -0
- package/dist/browser/neurosity.iife.js +48736 -0
- package/dist/browser/neurosity.js +672 -0
- package/dist/browser/neurosity.js.map +1 -0
- package/dist/cjs/Notion.d.ts +619 -0
- package/dist/cjs/Notion.js +1291 -0
- package/dist/cjs/api/bluetooth/BluetoothClient.d.ts +59 -0
- package/dist/cjs/api/bluetooth/BluetoothClient.js +287 -0
- package/dist/cjs/api/bluetooth/BluetoothTransport.d.ts +32 -0
- package/dist/cjs/api/bluetooth/BluetoothTransport.js +2 -0
- package/dist/cjs/api/bluetooth/constants.d.ts +4 -0
- package/dist/cjs/api/bluetooth/constants.js +9 -0
- package/dist/cjs/api/bluetooth/index.d.ts +4 -0
- package/dist/cjs/api/bluetooth/index.js +20 -0
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.d.ts +65 -0
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.js +386 -0
- package/dist/cjs/api/bluetooth/react-native/types/BleManagerTypes.d.ts +63 -0
- package/dist/cjs/api/bluetooth/react-native/types/BleManagerTypes.js +11 -0
- package/dist/cjs/api/bluetooth/react-native/types/ReactNativeTypes.d.ts +180 -0
- package/dist/cjs/api/bluetooth/react-native/types/ReactNativeTypes.js +10 -0
- package/dist/cjs/api/bluetooth/types/index.d.ts +31 -0
- package/dist/cjs/api/bluetooth/types/index.js +22 -0
- package/dist/cjs/api/bluetooth/utils/create6DigitPin.d.ts +1 -0
- package/dist/cjs/api/bluetooth/utils/create6DigitPin.js +8 -0
- package/dist/cjs/api/bluetooth/utils/csvBufferToEpoch.d.ts +11 -0
- package/dist/cjs/api/bluetooth/utils/csvBufferToEpoch.js +36 -0
- package/dist/cjs/api/bluetooth/utils/encoding.d.ts +3 -0
- package/dist/cjs/api/bluetooth/utils/encoding.js +22 -0
- package/dist/cjs/api/bluetooth/utils/osHasBluetoothSupport.d.ts +2 -0
- package/dist/cjs/api/bluetooth/utils/osHasBluetoothSupport.js +23 -0
- package/dist/cjs/api/bluetooth/utils/stitch.d.ts +5 -0
- package/dist/cjs/api/bluetooth/utils/stitch.js +22 -0
- package/dist/cjs/api/bluetooth/web/WebBluetoothTransport.d.ts +47 -0
- package/dist/cjs/api/bluetooth/web/WebBluetoothTransport.js +412 -0
- package/dist/cjs/api/bluetooth/web/isMaybeWebWorkerContext.d.ts +1 -0
- package/dist/cjs/api/bluetooth/web/isMaybeWebWorkerContext.js +8 -0
- package/dist/cjs/api/bluetooth/web/isWebBluetoothSupported.d.ts +1 -0
- package/dist/cjs/api/bluetooth/web/isWebBluetoothSupported.js +11 -0
- package/dist/cjs/api/firebase/FirebaseApp.d.ts +20 -0
- package/dist/cjs/api/firebase/FirebaseApp.js +70 -0
- package/dist/cjs/api/firebase/FirebaseDevice.d.ts +68 -0
- package/dist/cjs/api/firebase/FirebaseDevice.js +146 -0
- package/dist/cjs/api/firebase/FirebaseUser.d.ts +53 -0
- package/dist/cjs/api/firebase/FirebaseUser.js +434 -0
- package/dist/cjs/api/firebase/config.d.ts +8 -0
- package/dist/cjs/api/firebase/config.js +11 -0
- package/dist/cjs/api/firebase/deviceStore.d.ts +26 -0
- package/dist/cjs/api/firebase/deviceStore.js +191 -0
- package/dist/cjs/api/firebase/index.d.ts +3 -0
- package/dist/cjs/api/firebase/index.js +19 -0
- package/dist/cjs/api/https/config.d.ts +1 -0
- package/dist/cjs/api/https/config.js +4 -0
- package/dist/cjs/api/https/createOAuthURL.d.ts +3 -0
- package/dist/cjs/api/https/createOAuthURL.js +18 -0
- package/dist/cjs/api/https/getOAuthToken.d.ts +3 -0
- package/dist/cjs/api/https/getOAuthToken.js +34 -0
- package/dist/cjs/api/https/utils.d.ts +2 -0
- package/dist/cjs/api/https/utils.js +13 -0
- package/dist/cjs/api/index.d.ts +108 -0
- package/dist/cjs/api/index.js +306 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +19 -0
- package/dist/cjs/skills/NotionOnDevice.d.ts +7 -0
- package/dist/cjs/skills/NotionOnDevice.js +25 -0
- package/dist/cjs/skills/createSkill.d.ts +7 -0
- package/dist/cjs/skills/createSkill.js +40 -0
- package/dist/cjs/skills/index.d.ts +2 -0
- package/dist/cjs/skills/index.js +18 -0
- package/dist/cjs/subscriptions/SubscriptionManager.d.ts +11 -0
- package/dist/cjs/subscriptions/SubscriptionManager.js +27 -0
- package/dist/cjs/timesync/Timesync.d.ts +20 -0
- package/dist/cjs/timesync/Timesync.js +78 -0
- package/dist/cjs/timesync/index.d.ts +1 -0
- package/dist/cjs/timesync/index.js +17 -0
- package/dist/cjs/types/accelerometer.d.ts +10 -0
- package/dist/cjs/types/accelerometer.js +2 -0
- package/dist/cjs/types/actions.d.ts +16 -0
- package/dist/cjs/types/actions.js +2 -0
- package/dist/cjs/types/awareness.d.ts +1 -0
- package/dist/cjs/types/awareness.js +2 -0
- package/dist/cjs/types/brainwaves.d.ts +26 -0
- package/dist/cjs/types/brainwaves.js +2 -0
- package/dist/cjs/types/calm.d.ts +6 -0
- package/dist/cjs/types/calm.js +2 -0
- package/dist/cjs/types/client.d.ts +22 -0
- package/dist/cjs/types/client.js +2 -0
- package/dist/cjs/types/credentials.d.ts +12 -0
- package/dist/cjs/types/credentials.js +2 -0
- package/dist/cjs/types/deviceInfo.d.ts +18 -0
- package/dist/cjs/types/deviceInfo.js +2 -0
- package/dist/cjs/types/epoch.d.ts +12 -0
- package/dist/cjs/types/epoch.js +2 -0
- package/dist/cjs/types/experiment.d.ts +9 -0
- package/dist/cjs/types/experiment.js +2 -0
- package/dist/cjs/types/focus.d.ts +6 -0
- package/dist/cjs/types/focus.js +2 -0
- package/dist/cjs/types/hapticEffects.d.ts +125 -0
- package/dist/cjs/types/hapticEffects.js +2 -0
- package/dist/cjs/types/kinesis.d.ts +6 -0
- package/dist/cjs/types/kinesis.js +2 -0
- package/dist/cjs/types/marker.d.ts +7 -0
- package/dist/cjs/types/marker.js +2 -0
- package/dist/cjs/types/metrics.d.ts +21 -0
- package/dist/cjs/types/metrics.js +2 -0
- package/dist/cjs/types/oauth.d.ts +20 -0
- package/dist/cjs/types/oauth.js +2 -0
- package/dist/cjs/types/options.d.ts +51 -0
- package/dist/cjs/types/options.js +2 -0
- package/dist/cjs/types/sample.d.ts +15 -0
- package/dist/cjs/types/sample.js +2 -0
- package/dist/cjs/types/settings.d.ts +13 -0
- package/dist/cjs/types/settings.js +2 -0
- package/dist/cjs/types/signalQuality.d.ts +13 -0
- package/dist/cjs/types/signalQuality.js +2 -0
- package/dist/cjs/types/skill.d.ts +68 -0
- package/dist/cjs/types/skill.js +2 -0
- package/dist/cjs/types/status.d.ts +26 -0
- package/dist/cjs/types/status.js +22 -0
- package/dist/cjs/types/streaming.d.ts +15 -0
- package/dist/cjs/types/streaming.js +20 -0
- package/dist/cjs/types/subscriptions.d.ts +23 -0
- package/dist/cjs/types/subscriptions.js +2 -0
- package/dist/cjs/types/training.d.ts +19 -0
- package/dist/cjs/types/training.js +2 -0
- package/dist/cjs/types/user.d.ts +12 -0
- package/dist/cjs/types/user.js +2 -0
- package/dist/cjs/utils/errors.d.ts +5 -0
- package/dist/cjs/utils/errors.js +17 -0
- package/dist/cjs/utils/filterInternalKeys.d.ts +3 -0
- package/dist/cjs/utils/filterInternalKeys.js +21 -0
- package/dist/cjs/utils/hapticEffects.d.ts +123 -0
- package/dist/cjs/utils/hapticEffects.js +130 -0
- package/dist/cjs/utils/heartbeat.d.ts +4 -0
- package/dist/cjs/utils/heartbeat.js +34 -0
- package/dist/cjs/utils/is-node.d.ts +1 -0
- package/dist/cjs/utils/is-node.js +9 -0
- package/dist/cjs/utils/metrics.d.ts +1 -0
- package/dist/cjs/utils/metrics.js +56 -0
- package/dist/cjs/utils/oauth.d.ts +9 -0
- package/dist/cjs/utils/oauth.js +92 -0
- package/dist/cjs/utils/pick.d.ts +1 -0
- package/dist/cjs/utils/pick.js +5 -0
- package/dist/cjs/utils/pipes.d.ts +46 -0
- package/dist/cjs/utils/pipes.js +71 -0
- package/dist/cjs/utils/platform.d.ts +30 -0
- package/dist/cjs/utils/platform.js +47 -0
- package/dist/cjs/utils/subscription.d.ts +6 -0
- package/dist/cjs/utils/subscription.js +55 -0
- package/dist/cjs/utils/transferDevice.d.ts +9 -0
- package/dist/cjs/utils/transferDevice.js +2 -0
- package/dist/cjs/utils/whileOnline.d.ts +8 -0
- package/dist/cjs/utils/whileOnline.js +15 -0
- package/dist/electron/index.js +92 -0
- package/dist/electron/index.js.map +1 -0
- package/dist/esm/Notion.d.ts +619 -0
- package/dist/esm/Notion.js +1260 -0
- package/dist/esm/api/bluetooth/BluetoothClient.d.ts +59 -0
- package/dist/esm/api/bluetooth/BluetoothClient.js +283 -0
- package/dist/esm/api/bluetooth/BluetoothTransport.d.ts +32 -0
- package/dist/esm/api/bluetooth/BluetoothTransport.js +1 -0
- package/dist/esm/api/bluetooth/constants.d.ts +4 -0
- package/dist/esm/api/bluetooth/constants.js +6 -0
- package/dist/esm/api/bluetooth/index.d.ts +4 -0
- package/dist/esm/api/bluetooth/index.js +4 -0
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.d.ts +65 -0
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.js +382 -0
- package/dist/esm/api/bluetooth/react-native/types/BleManagerTypes.d.ts +63 -0
- package/dist/esm/api/bluetooth/react-native/types/BleManagerTypes.js +8 -0
- package/dist/esm/api/bluetooth/react-native/types/ReactNativeTypes.d.ts +180 -0
- package/dist/esm/api/bluetooth/react-native/types/ReactNativeTypes.js +9 -0
- package/dist/esm/api/bluetooth/types/index.d.ts +31 -0
- package/dist/esm/api/bluetooth/types/index.js +19 -0
- package/dist/esm/api/bluetooth/utils/create6DigitPin.d.ts +1 -0
- package/dist/esm/api/bluetooth/utils/create6DigitPin.js +4 -0
- package/dist/esm/api/bluetooth/utils/csvBufferToEpoch.d.ts +11 -0
- package/dist/esm/api/bluetooth/utils/csvBufferToEpoch.js +31 -0
- package/dist/esm/api/bluetooth/utils/encoding.d.ts +3 -0
- package/dist/esm/api/bluetooth/utils/encoding.js +17 -0
- package/dist/esm/api/bluetooth/utils/osHasBluetoothSupport.d.ts +2 -0
- package/dist/esm/api/bluetooth/utils/osHasBluetoothSupport.js +16 -0
- package/dist/esm/api/bluetooth/utils/stitch.d.ts +5 -0
- package/dist/esm/api/bluetooth/utils/stitch.js +18 -0
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.d.ts +47 -0
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.js +408 -0
- package/dist/esm/api/bluetooth/web/isMaybeWebWorkerContext.d.ts +1 -0
- package/dist/esm/api/bluetooth/web/isMaybeWebWorkerContext.js +4 -0
- package/dist/esm/api/bluetooth/web/isWebBluetoothSupported.d.ts +1 -0
- package/dist/esm/api/bluetooth/web/isWebBluetoothSupported.js +7 -0
- package/dist/esm/api/firebase/FirebaseApp.d.ts +20 -0
- package/dist/esm/api/firebase/FirebaseApp.js +63 -0
- package/dist/esm/api/firebase/FirebaseDevice.d.ts +68 -0
- package/dist/esm/api/firebase/FirebaseDevice.js +139 -0
- package/dist/esm/api/firebase/FirebaseUser.d.ts +53 -0
- package/dist/esm/api/firebase/FirebaseUser.js +426 -0
- package/dist/esm/api/firebase/config.d.ts +8 -0
- package/dist/esm/api/firebase/config.js +8 -0
- package/dist/esm/api/firebase/deviceStore.d.ts +26 -0
- package/dist/esm/api/firebase/deviceStore.js +184 -0
- package/dist/esm/api/firebase/index.d.ts +3 -0
- package/dist/esm/api/firebase/index.js +3 -0
- package/dist/esm/api/https/config.d.ts +1 -0
- package/dist/esm/api/https/config.js +1 -0
- package/dist/esm/api/https/createOAuthURL.d.ts +3 -0
- package/dist/esm/api/https/createOAuthURL.js +11 -0
- package/dist/esm/api/https/getOAuthToken.d.ts +3 -0
- package/dist/esm/api/https/getOAuthToken.js +27 -0
- package/dist/esm/api/https/utils.d.ts +2 -0
- package/dist/esm/api/https/utils.js +9 -0
- package/dist/esm/api/index.d.ts +108 -0
- package/dist/esm/api/index.js +299 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/neurosity.mjs +48722 -0
- package/dist/esm/skills/NotionOnDevice.d.ts +7 -0
- package/dist/esm/skills/NotionOnDevice.js +21 -0
- package/dist/esm/skills/createSkill.d.ts +7 -0
- package/dist/esm/skills/createSkill.js +36 -0
- package/dist/esm/skills/index.d.ts +2 -0
- package/dist/esm/skills/index.js +2 -0
- package/dist/esm/subscriptions/SubscriptionManager.d.ts +11 -0
- package/dist/esm/subscriptions/SubscriptionManager.js +23 -0
- package/dist/esm/timesync/Timesync.d.ts +20 -0
- package/dist/esm/timesync/Timesync.js +71 -0
- package/dist/esm/timesync/index.d.ts +1 -0
- package/dist/esm/timesync/index.js +1 -0
- package/dist/esm/types/accelerometer.d.ts +10 -0
- package/dist/esm/types/accelerometer.js +1 -0
- package/dist/esm/types/actions.d.ts +16 -0
- package/dist/esm/types/actions.js +1 -0
- package/dist/esm/types/awareness.d.ts +1 -0
- package/dist/esm/types/awareness.js +1 -0
- package/dist/esm/types/brainwaves.d.ts +26 -0
- package/dist/esm/types/brainwaves.js +1 -0
- package/dist/esm/types/calm.d.ts +6 -0
- package/dist/esm/types/calm.js +1 -0
- package/dist/esm/types/client.d.ts +22 -0
- package/dist/esm/types/client.js +1 -0
- package/dist/esm/types/credentials.d.ts +12 -0
- package/dist/esm/types/credentials.js +1 -0
- package/dist/esm/types/deviceInfo.d.ts +18 -0
- package/dist/esm/types/deviceInfo.js +1 -0
- package/dist/esm/types/epoch.d.ts +12 -0
- package/dist/esm/types/epoch.js +1 -0
- package/dist/esm/types/experiment.d.ts +9 -0
- package/dist/esm/types/experiment.js +1 -0
- package/dist/esm/types/focus.d.ts +6 -0
- package/dist/esm/types/focus.js +1 -0
- package/dist/esm/types/hapticEffects.d.ts +125 -0
- package/dist/esm/types/hapticEffects.js +1 -0
- package/dist/esm/types/kinesis.d.ts +6 -0
- package/dist/esm/types/kinesis.js +1 -0
- package/dist/esm/types/marker.d.ts +7 -0
- package/dist/esm/types/marker.js +1 -0
- package/dist/esm/types/metrics.d.ts +21 -0
- package/dist/esm/types/metrics.js +1 -0
- package/dist/esm/types/oauth.d.ts +20 -0
- package/dist/esm/types/oauth.js +1 -0
- package/dist/esm/types/options.d.ts +51 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/sample.d.ts +15 -0
- package/dist/esm/types/sample.js +1 -0
- package/dist/esm/types/settings.d.ts +13 -0
- package/dist/esm/types/settings.js +1 -0
- package/dist/esm/types/signalQuality.d.ts +13 -0
- package/dist/esm/types/signalQuality.js +1 -0
- package/dist/esm/types/skill.d.ts +68 -0
- package/dist/esm/types/skill.js +1 -0
- package/dist/esm/types/status.d.ts +26 -0
- package/dist/esm/types/status.js +19 -0
- package/dist/esm/types/streaming.d.ts +15 -0
- package/dist/esm/types/streaming.js +17 -0
- package/dist/esm/types/subscriptions.d.ts +23 -0
- package/dist/esm/types/subscriptions.js +1 -0
- package/dist/esm/types/training.d.ts +19 -0
- package/dist/esm/types/training.js +1 -0
- package/dist/esm/types/user.d.ts +12 -0
- package/dist/esm/types/user.js +1 -0
- package/dist/esm/utils/errors.d.ts +5 -0
- package/dist/esm/utils/errors.js +11 -0
- package/dist/esm/utils/filterInternalKeys.d.ts +3 -0
- package/dist/esm/utils/filterInternalKeys.js +17 -0
- package/dist/esm/utils/hapticEffects.d.ts +123 -0
- package/dist/esm/utils/hapticEffects.js +125 -0
- package/dist/esm/utils/heartbeat.d.ts +4 -0
- package/dist/esm/utils/heartbeat.js +29 -0
- package/dist/esm/utils/is-node.d.ts +1 -0
- package/dist/esm/utils/is-node.js +5 -0
- package/dist/esm/utils/metrics.d.ts +1 -0
- package/dist/esm/utils/metrics.js +52 -0
- package/dist/esm/utils/oauth.d.ts +9 -0
- package/dist/esm/utils/oauth.js +64 -0
- package/dist/esm/utils/pick.d.ts +1 -0
- package/dist/esm/utils/pick.js +1 -0
- package/dist/esm/utils/pipes.d.ts +46 -0
- package/dist/esm/utils/pipes.js +65 -0
- package/dist/esm/utils/platform.d.ts +30 -0
- package/dist/esm/utils/platform.js +41 -0
- package/dist/esm/utils/subscription.d.ts +6 -0
- package/dist/esm/utils/subscription.js +24 -0
- package/dist/esm/utils/transferDevice.d.ts +9 -0
- package/dist/esm/utils/transferDevice.js +1 -0
- package/dist/esm/utils/whileOnline.d.ts +8 -0
- package/dist/esm/utils/whileOnline.js +11 -0
- package/dist/examples/auth.html +32 -0
- package/dist/examples/cloud.html +17 -0
- package/dist/examples/index.html +1 -0
- package/dist/examples/neurosity.iife.js +48736 -0
- package/dist/examples/neurosity.js +672 -0
- package/dist/examples/neurosity.mjs +48722 -0
- package/dist/examples/training.html +49 -0
- package/package.json +78 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FirebaseUser = exports.createUser = exports.credentialWithLink = void 0;
|
|
16
|
+
const rxjs_1 = require("rxjs");
|
|
17
|
+
const operators_1 = require("rxjs/operators");
|
|
18
|
+
const app_1 = __importDefault(require("firebase/app"));
|
|
19
|
+
const SERVER_TIMESTAMP = app_1.default.database.ServerValue.TIMESTAMP;
|
|
20
|
+
/**
|
|
21
|
+
* @hidden
|
|
22
|
+
*/
|
|
23
|
+
exports.credentialWithLink = app_1.default.auth.EmailAuthProvider.credentialWithLink;
|
|
24
|
+
/**
|
|
25
|
+
* @hidden
|
|
26
|
+
*/
|
|
27
|
+
function createUser(...args) {
|
|
28
|
+
return new app_1.default.User(...args);
|
|
29
|
+
}
|
|
30
|
+
exports.createUser = createUser;
|
|
31
|
+
/**
|
|
32
|
+
* @hidden
|
|
33
|
+
*/
|
|
34
|
+
class FirebaseUser {
|
|
35
|
+
constructor(firebaseApp) {
|
|
36
|
+
this.app = firebaseApp.app;
|
|
37
|
+
this.app.auth().onAuthStateChanged((user) => {
|
|
38
|
+
this.user = user;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
auth() {
|
|
42
|
+
return this.app.auth();
|
|
43
|
+
}
|
|
44
|
+
createAccount(credentials) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const { email, password } = credentials;
|
|
47
|
+
const [error, user] = yield this.app
|
|
48
|
+
.auth()
|
|
49
|
+
.createUserWithEmailAndPassword(email, password)
|
|
50
|
+
.then((user) => [null, user])
|
|
51
|
+
.catch((error) => [error, null]);
|
|
52
|
+
if (error) {
|
|
53
|
+
return Promise.reject(error);
|
|
54
|
+
}
|
|
55
|
+
return user;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
deleteAccount() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const user = this.app.auth().currentUser;
|
|
61
|
+
if (!user) {
|
|
62
|
+
return Promise.reject(new Error(`You are trying to delete an account that is not authenticated. To delete an account, the account must have signed in recently.`));
|
|
63
|
+
}
|
|
64
|
+
const [devicesError, devices] = yield this.getDevices()
|
|
65
|
+
.then((response) => [null, response])
|
|
66
|
+
.catch((error) => [error, null]);
|
|
67
|
+
if (devicesError) {
|
|
68
|
+
return Promise.reject(devicesError);
|
|
69
|
+
}
|
|
70
|
+
if (devices.length) {
|
|
71
|
+
const removeDeviceError = yield Promise.all(devices.map((device) => this.removeDevice(device.deviceId)))
|
|
72
|
+
.then(() => null)
|
|
73
|
+
.catch((error) => error);
|
|
74
|
+
if (removeDeviceError) {
|
|
75
|
+
return Promise.reject(removeDeviceError);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return user.delete();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
onAuthStateChanged() {
|
|
82
|
+
return new rxjs_1.Observable((subscriber) => {
|
|
83
|
+
try {
|
|
84
|
+
this.app.auth().onAuthStateChanged((user) => {
|
|
85
|
+
subscriber.next(user);
|
|
86
|
+
}, (error) => {
|
|
87
|
+
subscriber.error(error);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
subscriber.error(error);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
onLogin() {
|
|
96
|
+
return new rxjs_1.Observable((subscriber) => {
|
|
97
|
+
const unsubscribe = this.app
|
|
98
|
+
.auth()
|
|
99
|
+
.onAuthStateChanged((user) => {
|
|
100
|
+
if (!!user) {
|
|
101
|
+
subscriber.next(user);
|
|
102
|
+
subscriber.complete();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return () => unsubscribe();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
login(credentials) {
|
|
109
|
+
if ("customToken" in credentials) {
|
|
110
|
+
const { customToken } = credentials;
|
|
111
|
+
return this.app.auth().signInWithCustomToken(customToken);
|
|
112
|
+
}
|
|
113
|
+
if ("idToken" in credentials && "providerId" in credentials) {
|
|
114
|
+
const provider = new app_1.default.auth.OAuthProvider(credentials.providerId);
|
|
115
|
+
const oAuthCredential = provider.credential(credentials.idToken);
|
|
116
|
+
return this.app.auth().signInWithCredential(oAuthCredential);
|
|
117
|
+
}
|
|
118
|
+
if ("email" in credentials && "password" in credentials) {
|
|
119
|
+
const { email, password } = credentials;
|
|
120
|
+
return this.app
|
|
121
|
+
.auth()
|
|
122
|
+
.signInWithEmailAndPassword(email, password);
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Either {email,password}, {customToken}, or {idToken,providerId} is required`);
|
|
125
|
+
}
|
|
126
|
+
logout() {
|
|
127
|
+
return this.app.auth().signOut();
|
|
128
|
+
}
|
|
129
|
+
createCustomToken() {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
const [error, customToken] = yield this.app
|
|
132
|
+
.functions()
|
|
133
|
+
.httpsCallable("createCustomToken")()
|
|
134
|
+
.then(({ data }) => [null, data])
|
|
135
|
+
.catch((error) => [error, null]);
|
|
136
|
+
if (error) {
|
|
137
|
+
return Promise.reject(error);
|
|
138
|
+
}
|
|
139
|
+
return customToken;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
removeOAuthAccess() {
|
|
143
|
+
var _a;
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
const userId = (_a = this.user) === null || _a === void 0 ? void 0 : _a.uid;
|
|
146
|
+
if (!userId) {
|
|
147
|
+
return Promise.reject(`OAuth access can only be removed while logged in via OAuth.`);
|
|
148
|
+
}
|
|
149
|
+
const [error, response] = yield this.app
|
|
150
|
+
.functions()
|
|
151
|
+
.httpsCallable("removeAccessOAuthApp")()
|
|
152
|
+
.then(({ data }) => [null, data])
|
|
153
|
+
.catch((error) => [error, null]);
|
|
154
|
+
if (error) {
|
|
155
|
+
return Promise.reject(error);
|
|
156
|
+
}
|
|
157
|
+
const logoutError = yield this.logout()
|
|
158
|
+
.then(() => false)
|
|
159
|
+
.catch((error) => error);
|
|
160
|
+
if (logoutError) {
|
|
161
|
+
return Promise.reject(logoutError);
|
|
162
|
+
}
|
|
163
|
+
return response;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
getDevices() {
|
|
167
|
+
var _a;
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
const userId = (_a = this.user) === null || _a === void 0 ? void 0 : _a.uid;
|
|
170
|
+
if (!userId) {
|
|
171
|
+
return Promise.reject(`Please login.`);
|
|
172
|
+
}
|
|
173
|
+
const snapshot = yield this.app
|
|
174
|
+
.database()
|
|
175
|
+
.ref(this.getUserDevicesPath())
|
|
176
|
+
.once("value");
|
|
177
|
+
const userDevices = snapshot.val();
|
|
178
|
+
return this.userDevicesToDeviceInfoList(userDevices);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
addDevice(deviceId) {
|
|
182
|
+
var _a;
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
const userId = (_a = this.user) === null || _a === void 0 ? void 0 : _a.uid;
|
|
185
|
+
if (!userId) {
|
|
186
|
+
return Promise.reject(`Please login.`);
|
|
187
|
+
}
|
|
188
|
+
const devices = yield this.getDevices().catch((error) => {
|
|
189
|
+
console.log(error);
|
|
190
|
+
});
|
|
191
|
+
const deviceAlreadyInAccount = devices &&
|
|
192
|
+
devices.length &&
|
|
193
|
+
devices.map(({ deviceId }) => deviceId).includes(deviceId);
|
|
194
|
+
if (deviceAlreadyInAccount) {
|
|
195
|
+
return Promise.reject(`The device is already added to this account.`);
|
|
196
|
+
}
|
|
197
|
+
const [isValid, invalidErrorMessage] = yield this.isDeviceIdValid(deviceId)
|
|
198
|
+
.then((isValid) => [isValid])
|
|
199
|
+
.catch((error) => [false, error]);
|
|
200
|
+
if (!isValid) {
|
|
201
|
+
return Promise.reject(invalidErrorMessage);
|
|
202
|
+
}
|
|
203
|
+
const claimedByPath = this.getDeviceClaimedByPath(deviceId);
|
|
204
|
+
const userDevicePath = this.getUserClaimedDevicePath(deviceId);
|
|
205
|
+
const [hasError, errorMessage] = yield this.app
|
|
206
|
+
.database()
|
|
207
|
+
.ref()
|
|
208
|
+
.update({
|
|
209
|
+
[claimedByPath]: userId,
|
|
210
|
+
[userDevicePath]: {
|
|
211
|
+
claimedOn: SERVER_TIMESTAMP
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
.then(() => [false])
|
|
215
|
+
.catch((error) => [true, error]);
|
|
216
|
+
if (hasError) {
|
|
217
|
+
return Promise.reject(errorMessage);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
removeDevice(deviceId) {
|
|
222
|
+
var _a;
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
const userId = (_a = this.user) === null || _a === void 0 ? void 0 : _a.uid;
|
|
225
|
+
if (!userId) {
|
|
226
|
+
return Promise.reject(`Please login.`);
|
|
227
|
+
}
|
|
228
|
+
const claimedByPath = this.getDeviceClaimedByPath(deviceId);
|
|
229
|
+
const userDevicePath = this.getUserClaimedDevicePath(deviceId);
|
|
230
|
+
const claimedByRef = this.app.database().ref(claimedByPath);
|
|
231
|
+
const userDeviceRef = this.app.database().ref(userDevicePath);
|
|
232
|
+
const [hasError, errorMessage] = yield Promise.all([
|
|
233
|
+
claimedByRef.remove(),
|
|
234
|
+
userDeviceRef.remove()
|
|
235
|
+
])
|
|
236
|
+
.then(() => [false])
|
|
237
|
+
.catch((error) => [true, error]);
|
|
238
|
+
if (hasError) {
|
|
239
|
+
return Promise.reject(errorMessage);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
transferDevice(options) {
|
|
244
|
+
var _a;
|
|
245
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
+
const userId = (_a = this.user) === null || _a === void 0 ? void 0 : _a.uid;
|
|
247
|
+
if (!userId) {
|
|
248
|
+
return Promise.reject(new Error(`transferDevice: auth is required.`));
|
|
249
|
+
}
|
|
250
|
+
if (!("recipientsEmail" in options) &&
|
|
251
|
+
!("recipientsUserId" in options)) {
|
|
252
|
+
return Promise.reject(new Error(`transferDevice: either 'recipientsEmail' or 'recipientsUserId' key is required.`));
|
|
253
|
+
}
|
|
254
|
+
if (!(options === null || options === void 0 ? void 0 : options.deviceId)) {
|
|
255
|
+
return Promise.reject(new Error(`transferDevice: a deviceId is required.`));
|
|
256
|
+
}
|
|
257
|
+
const [error, response] = yield this.app
|
|
258
|
+
.functions()
|
|
259
|
+
.httpsCallable("transferDeviceOwnership")(options)
|
|
260
|
+
.then(({ data }) => [null, data])
|
|
261
|
+
.catch((error) => [error, null]);
|
|
262
|
+
if (error) {
|
|
263
|
+
return Promise.reject(error);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
isDeviceIdValid(deviceId) {
|
|
268
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
269
|
+
// hex string of 32 characters
|
|
270
|
+
const hexRegEx = /[0-9A-Fa-f]{32}/g;
|
|
271
|
+
if (!deviceId ||
|
|
272
|
+
deviceId.length !== 32 ||
|
|
273
|
+
!hexRegEx.test(deviceId)) {
|
|
274
|
+
return Promise.reject("The device id is incorrectly formatted.");
|
|
275
|
+
}
|
|
276
|
+
const claimedByPath = this.getDeviceClaimedByPath(deviceId);
|
|
277
|
+
const claimedByRef = this.app.database().ref(claimedByPath);
|
|
278
|
+
const claimedBySnapshot = yield claimedByRef
|
|
279
|
+
.once("value")
|
|
280
|
+
.catch(() => null);
|
|
281
|
+
if (!claimedBySnapshot || claimedBySnapshot.exists()) {
|
|
282
|
+
return Promise.reject("The device has already been claimed.");
|
|
283
|
+
}
|
|
284
|
+
return true;
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
onUserDevicesChange() {
|
|
288
|
+
return this.onAuthStateChanged().pipe((0, operators_1.switchMap)((user) => {
|
|
289
|
+
if (!user) {
|
|
290
|
+
return rxjs_1.EMPTY;
|
|
291
|
+
}
|
|
292
|
+
const userDevicesPath = this.getUserDevicesPath();
|
|
293
|
+
const userDevicesRef = this.app.database().ref(userDevicesPath);
|
|
294
|
+
return (0, rxjs_1.fromEventPattern)((handler) => userDevicesRef.on("value", handler), (handler) => userDevicesRef.off("value", handler)).pipe((0, operators_1.map)(([snapshot]) => snapshot.val()), (0, operators_1.switchMap)((userDevices) => {
|
|
295
|
+
return (0, rxjs_1.from)(this.userDevicesToDeviceInfoList(userDevices));
|
|
296
|
+
}));
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
onUserClaimsChange() {
|
|
300
|
+
return this.onAuthStateChanged().pipe((0, operators_1.switchMap)((user) => {
|
|
301
|
+
if (!user) {
|
|
302
|
+
return rxjs_1.EMPTY;
|
|
303
|
+
}
|
|
304
|
+
const claimsUpdatedOnPath = this.getUserClaimsUpdatedOnPath();
|
|
305
|
+
const claimsUpdatedOnRef = this.app
|
|
306
|
+
.database()
|
|
307
|
+
.ref(claimsUpdatedOnPath);
|
|
308
|
+
return (0, rxjs_1.fromEventPattern)((handler) => claimsUpdatedOnRef.on("value", handler), (handler) => claimsUpdatedOnRef.off("value", handler)).pipe((0, operators_1.map)(([snapshot]) => snapshot.val()), (0, operators_1.switchMap)(() => {
|
|
309
|
+
// Force refresh of auth id token
|
|
310
|
+
return (0, rxjs_1.from)(this.getIdToken(true)).pipe((0, operators_1.switchMap)(() => (0, rxjs_1.from)(this.getClaims())));
|
|
311
|
+
}));
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
getIdToken(forceRefresh = false) {
|
|
315
|
+
var _a;
|
|
316
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
+
const user = (_a = this.app.auth()) === null || _a === void 0 ? void 0 : _a.currentUser;
|
|
318
|
+
if (!user) {
|
|
319
|
+
return Promise.reject(`getUserIdToken: unable to get currentUser`);
|
|
320
|
+
}
|
|
321
|
+
yield user.getIdToken(forceRefresh).catch((error) => {
|
|
322
|
+
console.error(error);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
getClaims() {
|
|
327
|
+
var _a;
|
|
328
|
+
const user = (_a = this.app.auth()) === null || _a === void 0 ? void 0 : _a.currentUser;
|
|
329
|
+
if (!user) {
|
|
330
|
+
return Promise.reject(`getUserClaims: unable to get currentUser`);
|
|
331
|
+
}
|
|
332
|
+
return user
|
|
333
|
+
.getIdTokenResult()
|
|
334
|
+
.then((token) => token.claims)
|
|
335
|
+
.catch((error) => {
|
|
336
|
+
console.error(error);
|
|
337
|
+
return null;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
userDevicesToDeviceInfoList(userDevices) {
|
|
341
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
342
|
+
const devicesInfoSnapshots = Object.keys(userDevices !== null && userDevices !== void 0 ? userDevices : {}).map((deviceId) => this.app
|
|
343
|
+
.database()
|
|
344
|
+
.ref(this.getDeviceInfoPath(deviceId))
|
|
345
|
+
.once("value"));
|
|
346
|
+
const devicesList = yield Promise.all(devicesInfoSnapshots).then((snapshots) => snapshots.map((snapshot) => snapshot.val()));
|
|
347
|
+
const validDevices = devicesList.filter((device) => !!device);
|
|
348
|
+
validDevices.sort((a, b) => {
|
|
349
|
+
return (userDevices[a.deviceId].claimedOn -
|
|
350
|
+
userDevices[b.deviceId].claimedOn);
|
|
351
|
+
});
|
|
352
|
+
return validDevices;
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
hasDevicePermission(deviceId) {
|
|
356
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
357
|
+
const deviceInfoPath = this.getDeviceInfoPath(deviceId);
|
|
358
|
+
const hasPermission = yield this.app
|
|
359
|
+
.database()
|
|
360
|
+
.ref(deviceInfoPath)
|
|
361
|
+
.once("value")
|
|
362
|
+
.then(() => true)
|
|
363
|
+
.catch(() => false);
|
|
364
|
+
return hasPermission;
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
getDeviceClaimedByPath(deviceId) {
|
|
368
|
+
return `devices/${deviceId}/status/claimedBy`;
|
|
369
|
+
}
|
|
370
|
+
getUserClaimedDevicePath(deviceId) {
|
|
371
|
+
const userId = this.user.uid;
|
|
372
|
+
return `users/${userId}/devices/${deviceId}`;
|
|
373
|
+
}
|
|
374
|
+
getUserDevicesPath() {
|
|
375
|
+
const userId = this.user.uid;
|
|
376
|
+
return `users/${userId}/devices`;
|
|
377
|
+
}
|
|
378
|
+
getUserClaimsUpdatedOnPath() {
|
|
379
|
+
const userId = this.user.uid;
|
|
380
|
+
return `users/${userId}/claimsUpdatedOn`;
|
|
381
|
+
}
|
|
382
|
+
getDeviceInfoPath(deviceId) {
|
|
383
|
+
return `devices/${deviceId}/info`;
|
|
384
|
+
}
|
|
385
|
+
onUserExperiments() {
|
|
386
|
+
return this.onAuthStateChanged().pipe((0, operators_1.switchMap)((user) => {
|
|
387
|
+
if (!user) {
|
|
388
|
+
return rxjs_1.EMPTY;
|
|
389
|
+
}
|
|
390
|
+
const userId = this.user.uid;
|
|
391
|
+
const userExperimentsRef = this.app
|
|
392
|
+
.database()
|
|
393
|
+
.ref("experiments")
|
|
394
|
+
.orderByChild("userId")
|
|
395
|
+
.equalTo(userId)
|
|
396
|
+
.limitToFirst(100);
|
|
397
|
+
return (0, rxjs_1.fromEventPattern)((handler) => userExperimentsRef.on("value", handler), (handler) => userExperimentsRef.off("value", handler)).pipe((0, operators_1.map)(([snapshot]) => snapshot.val()),
|
|
398
|
+
// transform experiments map into sorted list
|
|
399
|
+
(0, operators_1.map)((experimentsMaps) => {
|
|
400
|
+
return Object.entries(experimentsMaps !== null && experimentsMaps !== void 0 ? experimentsMaps : {})
|
|
401
|
+
.map(([id, value]) => {
|
|
402
|
+
var _a;
|
|
403
|
+
return (Object.assign({ id: (_a = value === null || value === void 0 ? void 0 : value.id) !== null && _a !== void 0 ? _a : id }, value));
|
|
404
|
+
})
|
|
405
|
+
.sort((a, b) => new Date(b === null || b === void 0 ? void 0 : b.timestamp).getTime() -
|
|
406
|
+
new Date(a === null || a === void 0 ? void 0 : a.timestamp).getTime());
|
|
407
|
+
}));
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
deleteUserExperiment(experimentId) {
|
|
411
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
412
|
+
if (!experimentId) {
|
|
413
|
+
return Promise.reject(`deleteUserExperiment: please provide an experiment id`);
|
|
414
|
+
}
|
|
415
|
+
const removeExperiment = (experimentId) => {
|
|
416
|
+
return this.app
|
|
417
|
+
.database()
|
|
418
|
+
.ref("experiments")
|
|
419
|
+
.child(experimentId)
|
|
420
|
+
.remove();
|
|
421
|
+
};
|
|
422
|
+
const removeRelations = (experimentId) => {
|
|
423
|
+
return this.app.functions().httpsCallable("removeRelations")({
|
|
424
|
+
experimentId
|
|
425
|
+
});
|
|
426
|
+
};
|
|
427
|
+
yield Promise.all([
|
|
428
|
+
removeExperiment(experimentId),
|
|
429
|
+
removeRelations(experimentId)
|
|
430
|
+
]).catch(() => { });
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
exports.FirebaseUser = FirebaseUser;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.config = void 0;
|
|
4
|
+
exports.config = {
|
|
5
|
+
apiKey: "AIzaSyB0TkZ83Fj0CIzn8AAmE-Osc92s3ER8hy8",
|
|
6
|
+
authDomain: "neurosity-device.firebaseapp.com",
|
|
7
|
+
databaseURL: "https://neurosity-device.firebaseio.com",
|
|
8
|
+
projectId: "neurosity-device",
|
|
9
|
+
storageBucket: "neurosity-device.appspot.com",
|
|
10
|
+
messagingSenderId: "212595049674"
|
|
11
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface IDevice {
|
|
2
|
+
info: any;
|
|
3
|
+
status: any;
|
|
4
|
+
subscriptions: any;
|
|
5
|
+
metrics: any;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
export declare const createDeviceStore: (app: any, deviceId: any, subscriptionManager: any) => {
|
|
11
|
+
set: (namespace: any, payload: any) => any;
|
|
12
|
+
once: (namespace: any, eventType?: string) => Promise<any>;
|
|
13
|
+
update: (namespace: any, payload: any) => any;
|
|
14
|
+
lastOfChildValue: (namespace: any, key: any, value: any) => Promise<unknown>;
|
|
15
|
+
onNamespace: (namespace: string, callback: Function) => Function;
|
|
16
|
+
offNamespace: (namespace: string, listener: Function) => void;
|
|
17
|
+
dispatchAction: (action: any) => Promise<any>;
|
|
18
|
+
nextMetric: (metricName: string, metricValue: {
|
|
19
|
+
[label: string]: any;
|
|
20
|
+
}) => Promise<void>;
|
|
21
|
+
onMetric: (subscription: any, callback: Function) => any;
|
|
22
|
+
subscribeToMetric: (subscription: any) => any;
|
|
23
|
+
unsubscribeFromMetric: (subscription: any) => void;
|
|
24
|
+
removeMetricListener(subscription: any, listener: Function): void;
|
|
25
|
+
disconnect(): void;
|
|
26
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createDeviceStore = void 0;
|
|
16
|
+
const app_1 = __importDefault(require("firebase/app"));
|
|
17
|
+
const SERVER_TIMESTAMP = app_1.default.database.ServerValue.TIMESTAMP;
|
|
18
|
+
/**
|
|
19
|
+
* @hidden
|
|
20
|
+
*/
|
|
21
|
+
const createDeviceStore = (app, deviceId, subscriptionManager) => {
|
|
22
|
+
const deviceRef = app.database().ref(`devices/${deviceId}`);
|
|
23
|
+
const clientId = deviceRef.child("subscriptions").push().key;
|
|
24
|
+
const clientRef = deviceRef.child(`clients/${clientId}`);
|
|
25
|
+
let listenersToRemove = [];
|
|
26
|
+
const set = (namespace, payload) => {
|
|
27
|
+
return deviceRef.child(namespace).set(payload);
|
|
28
|
+
};
|
|
29
|
+
const push = (namespace, payload) => {
|
|
30
|
+
return deviceRef.child(namespace).push(payload);
|
|
31
|
+
};
|
|
32
|
+
const update = (namespace, payload) => {
|
|
33
|
+
return deviceRef.child(namespace).update(payload);
|
|
34
|
+
};
|
|
35
|
+
const on = (eventType = "value", namespace, callback) => {
|
|
36
|
+
const listener = deviceRef
|
|
37
|
+
.child(namespace)
|
|
38
|
+
.on(eventType, (snapshot) => {
|
|
39
|
+
callback(snapshot.val(), snapshot);
|
|
40
|
+
});
|
|
41
|
+
listenersToRemove.push(() => {
|
|
42
|
+
deviceRef.child(namespace).off(eventType, listener);
|
|
43
|
+
});
|
|
44
|
+
return listener;
|
|
45
|
+
};
|
|
46
|
+
const off = (namespace, eventType, listener) => {
|
|
47
|
+
if (listener) {
|
|
48
|
+
deviceRef.child(namespace).off(eventType, listener);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
deviceRef.child(namespace).off(eventType);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const once = (namespace, eventType = "value") => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
+
const snapshot = yield deviceRef.child(namespace).once(eventType);
|
|
56
|
+
return snapshot.val();
|
|
57
|
+
});
|
|
58
|
+
const remove = (namespace) => {
|
|
59
|
+
deviceRef.child(namespace).remove();
|
|
60
|
+
};
|
|
61
|
+
const bindListener = (eventType, namespace, callback, overrideResponse) => {
|
|
62
|
+
on(eventType, namespace, (data) => {
|
|
63
|
+
if (data !== null) {
|
|
64
|
+
off(namespace, eventType);
|
|
65
|
+
const response = overrideResponse ? overrideResponse : data;
|
|
66
|
+
callback(response);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
const lastOfChildValue = (namespace, key, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
const snapshot = yield deviceRef
|
|
72
|
+
.child(namespace)
|
|
73
|
+
.orderByChild(key)
|
|
74
|
+
.equalTo(value)
|
|
75
|
+
.limitToLast(1)
|
|
76
|
+
.once("value");
|
|
77
|
+
const results = snapshot.val();
|
|
78
|
+
const [match] = Object.values(results || {});
|
|
79
|
+
return match || null;
|
|
80
|
+
});
|
|
81
|
+
// Add client connections and subscriptions to db and remove them when offline
|
|
82
|
+
const connectedListener = app
|
|
83
|
+
.database()
|
|
84
|
+
.ref(".info/connected")
|
|
85
|
+
.on("value", (snapshot) => {
|
|
86
|
+
if (!snapshot.val()) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
clientRef
|
|
90
|
+
.onDisconnect()
|
|
91
|
+
.remove()
|
|
92
|
+
.then(() => {
|
|
93
|
+
clientRef.set(SERVER_TIMESTAMP);
|
|
94
|
+
// NOTION-115: Re-subscribe when internet connection is lost and regained
|
|
95
|
+
update("subscriptions", subscriptionManager.get()).then(() => {
|
|
96
|
+
subscriptionManager.toList().forEach((subscription) => {
|
|
97
|
+
const childPath = `subscriptions/${subscription.id}`;
|
|
98
|
+
deviceRef.child(childPath).onDisconnect().remove();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
listenersToRemove.push(() => {
|
|
104
|
+
app
|
|
105
|
+
.database()
|
|
106
|
+
.ref(".info/connected")
|
|
107
|
+
.off("value", connectedListener);
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
set,
|
|
111
|
+
once,
|
|
112
|
+
update,
|
|
113
|
+
lastOfChildValue,
|
|
114
|
+
onNamespace: (namespace, callback) => {
|
|
115
|
+
return on("value", namespace, (data) => {
|
|
116
|
+
callback(data);
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
offNamespace: (namespace, listener) => {
|
|
120
|
+
off(namespace, "value", listener);
|
|
121
|
+
},
|
|
122
|
+
dispatchAction: (action) => __awaiter(void 0, void 0, void 0, function* () {
|
|
123
|
+
const snapshot = yield push("actions", action);
|
|
124
|
+
const actionId = snapshot.key;
|
|
125
|
+
const actionPath = `actions/${actionId}`;
|
|
126
|
+
snapshot.onDisconnect().remove();
|
|
127
|
+
if (action.responseRequired) {
|
|
128
|
+
const responseTimeout = action.responseTimeout || 600000; // defaults to 10 minutes
|
|
129
|
+
const timeout = new Promise((_, reject) => {
|
|
130
|
+
const id = setTimeout(() => {
|
|
131
|
+
clearTimeout(id);
|
|
132
|
+
snapshot.remove();
|
|
133
|
+
reject(`Action response timed out in ${responseTimeout}ms.`);
|
|
134
|
+
}, responseTimeout);
|
|
135
|
+
});
|
|
136
|
+
const response = new Promise((resolve) => {
|
|
137
|
+
bindListener("value", `${actionPath}/response`, resolve);
|
|
138
|
+
});
|
|
139
|
+
return Promise.race([response, timeout]);
|
|
140
|
+
}
|
|
141
|
+
return actionId;
|
|
142
|
+
}),
|
|
143
|
+
nextMetric: (metricName, metricValue) => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
+
set(`metrics/${metricName}`, metricValue);
|
|
145
|
+
}),
|
|
146
|
+
onMetric: (subscription, callback) => {
|
|
147
|
+
const { atomic, metric, labels } = subscription;
|
|
148
|
+
const child = atomic
|
|
149
|
+
? `metrics/${metric}`
|
|
150
|
+
: `metrics/${metric}/${labels[0]}`;
|
|
151
|
+
return on("value", child, (data) => {
|
|
152
|
+
if (data !== null) {
|
|
153
|
+
callback(data);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
subscribeToMetric: (subscription) => {
|
|
158
|
+
const id = deviceRef.child("subscriptions").push().key;
|
|
159
|
+
const childPath = `subscriptions/${id}`;
|
|
160
|
+
const subscriptionCreated = Object.assign({ id,
|
|
161
|
+
clientId }, subscription);
|
|
162
|
+
set(childPath, subscriptionCreated);
|
|
163
|
+
deviceRef.child(childPath).onDisconnect().remove();
|
|
164
|
+
return subscriptionCreated;
|
|
165
|
+
},
|
|
166
|
+
unsubscribeFromMetric: (subscription) => {
|
|
167
|
+
remove(`subscriptions/${subscription.id}`);
|
|
168
|
+
},
|
|
169
|
+
removeMetricListener(subscription, listener) {
|
|
170
|
+
const { atomic, metric, labels } = subscription;
|
|
171
|
+
const child = atomic
|
|
172
|
+
? `metrics/${metric}`
|
|
173
|
+
: `metrics/${metric}/${labels[0]}`;
|
|
174
|
+
off(child, "value", listener);
|
|
175
|
+
},
|
|
176
|
+
disconnect() {
|
|
177
|
+
clientRef.remove();
|
|
178
|
+
listenersToRemove.forEach((removeListener) => {
|
|
179
|
+
removeListener();
|
|
180
|
+
});
|
|
181
|
+
subscriptionManager
|
|
182
|
+
.toList()
|
|
183
|
+
.filter((subscription) => subscription.clientId === clientId)
|
|
184
|
+
.forEach((subscription) => {
|
|
185
|
+
const childPath = `subscriptions/${subscription.id}`;
|
|
186
|
+
deviceRef.child(childPath).remove();
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
exports.createDeviceStore = createDeviceStore;
|