-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsync.js
112 lines (102 loc) · 2.95 KB
/
sync.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { Alert } from "react-native";
import RNFetchBlob from 'react-native-fetch-blob'
import RNSync from "react-native-sync";
var settings = {
syncServerUrl: "",//"http://localhost:8080/pervasync/server", // required
syncUserName: "", //"user_1", // required
syncUserPassword: ""//"welcome1", // required
};
var settingsLoaded = false;
var configSuccess = false;
async function loadSettings() {
if (settingsLoaded) {
return;
}
console.log("begin loadSettings");
let path = RNFetchBlob.fs.dirs.DocumentDir + "/sync-settings.json";
if (await RNFetchBlob.fs.exists(path)) {
let str = await RNFetchBlob.fs.readFile(path, "utf8");
if (str) {
Object.assign(settings, JSON.parse(str));
}
} else {
//settings["syncServerUrl"] = "http://localhost:8080/pervasync/server";
settings["syncServerUrl"] = "http://203.195.233.31:8080/pervasync/server";
}
settingsLoaded = true;
console.log("end loadSettings");
}
async function saveSettings() {
let path = RNFetchBlob.fs.dirs.DocumentDir + "/sync-settings.json";
await RNFetchBlob.fs.writeFile(path, JSON.stringify(settings), "utf8");
}
async function config() {
try {
if (!settingsLoaded) {
await loadSettings();
}
if (!configSuccess) {
await RNSync.config(settings);
configSuccess = true;
console.log("configSuccess=true");
}
} catch (error) {
console.log("sync.js, config error=" + error);
console.log("sync.js, config error.stack=" + error.stack);
}
return configSuccess;
}
async function sync() {
if (!configSuccess) {
configSuccess = await config();
}
if (configSuccess) {
let syncSummary = await RNSync.sync();
console.log("syncSummary:\r\n" +
JSON.stringify(syncSummary, null, 4));
if (syncSummary.syncErrorMessages) {
Alert.alert("Sync Error", syncSummary.syncErrorMessages);
}
return syncSummary;
}
}
async function getRealm(syncSchemaName) {
if (!configSuccess) {
configSuccess = await config();
}
if (configSuccess) {
let syncRealm = await RNSync.getRealm(syncSchemaName);
return syncRealm;
}
}
async function getPath(syncFolderName) {
if (!configSuccess) {
configSuccess = await config();
}
if (configSuccess) {
let path = await RNSync.getPath(syncFolderName);
return path;
}
}
async function reset() {
try {
configSuccess = false;
await RNSync.config(settings, true);
configSuccess = true;
console.log("sync.js, reset success");
} catch (error) {
console.log("sync.js, reset error=" + error);
console.log("sync.js, reset error.stack=" + error.stack);
}
return configSuccess;
}
export default {
settings,
loadSettings,
saveSettings,
config,
sync,
getRealm,
getPath,
reset
}