forked from couchbaselabs/react-native-couchbase-lite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (36 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
import {AppState, NativeModules} from "react-native";
import Swagger from "swagger-client";
import spec from "./spec.json";
import base64 from 'base-64';
const Couchbase = NativeModules.ReactCBLite;
let manager, callback;
Couchbase.init(url => {
spec.host = url.split('/')[2];
new Swagger({spec: spec, usePromise: true})
.then(client => {
var encodedCredentials = "Basic " + base64.encode(url.split("//")[1].split('@')[0]);
client.clientAuthorizations.add("auth", new Swagger.ApiKeyAuthorization('Authorization', encodedCredentials, 'header'));
manager = client;
if (typeof callback == 'function') {
callback(manager);
}
});
// stop and start are needed because the OS appears to kill the listener when the app
// becomes inactive (when the screen is locked, or its put in the background)
AppState.addEventListener('change', (appState) => {
if (String(appState).match(/inactive|background/)) {
Couchbase.stopListener();
} else if (String(appState).match(/active/)) {
Couchbase.startListener();
}
});
});
Couchbase.initRESTClient = function (cb) {
if (typeof manager !== 'undefined') {
cb(manager); // If manager is already defined, don't wait.
} else {
callback = cb;
}
};
module.exports = Couchbase;