Skip to content

Commit a088a63

Browse files
committed
Synchronize settings with backend
1 parent 9d6f8bb commit a088a63

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

js/storage.js

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,85 @@ export class SettingsManager {
1515
constructor(documentId) {
1616
this.documentId = documentId;
1717
this._settings = new Settings();
18-
this.available = new Promise((resolve, reject) => {
19-
chrome.storage.sync.get([this.documentId], async (result) => {
20-
if (result[this.documentId] != null) {
21-
await this._settings.set(result[this.documentId]);
22-
} else {
23-
await this._settings.set({});
18+
this.available = new Promise(async (resolve, _) => {
19+
20+
const localSettings = await this.retrieveSettingsFromLocalStorage();
21+
const serverSettings = await this.retrieveSettingsFromServer();
22+
23+
if (localSettings == null && serverSettings == null) {
24+
await this._settings.set({});
25+
resolve(true);
26+
27+
return;
28+
}
29+
30+
if (localSettings == null) {
31+
await this._settings.set(serverSettings);
32+
resolve(true);
33+
34+
this.storeLocally(serverSettings);
35+
36+
return;
37+
}
38+
39+
if (serverSettings == null) {
40+
if (localSettings.lastUpdated == null) {
41+
localSettings.lastUpdated = Date.now();
42+
this.storeLocally(localSettings);
2443
}
2544

45+
await this._settings.set(localSettings);
46+
resolve(true);
47+
48+
this.storeOnServer(localSettings);
49+
50+
return;
51+
}
52+
53+
if ( localSettings.lastUpdated > serverSettings.lastUpdated ) {
54+
await this._settings.set(localSettings);
2655
resolve(true);
56+
57+
this.storeOnServer(localSettings);
58+
} else {
59+
await this._settings.set(serverSettings);
60+
resolve(true);
61+
62+
this.storeLocally(serverSettings);
63+
}
64+
});
65+
}
66+
67+
async retrieveSettingsFromServer() {
68+
const authToken = await Auth.getAuthToken();
69+
70+
if (!authToken) {
71+
return null;
72+
}
73+
74+
try {
75+
const response = await fetch(`https://linenumbers.app/api/v1/documentSettings?authToken=${authToken}&document=${this.documentId}`, {
76+
method: 'GET',
77+
});
78+
79+
if (response.ok) {
80+
// Successfully retrieved from server
81+
const rawSettings = await response.json();
82+
83+
return rawSettings;
84+
}
85+
} catch(e) {
86+
console.warn(e);
87+
return null;
88+
}
89+
90+
return null;
91+
}
92+
93+
retrieveSettingsFromLocalStorage() {
94+
return new Promise((resolve, _) => {
95+
chrome.storage.sync.get([this.documentId], result => {
96+
resolve(result[this.documentId]);
2797
});
2898
});
2999
}
@@ -32,20 +102,29 @@ export class SettingsManager {
32102
return this._settings;
33103
}
34104

35-
async store() {
36-
const rawSettings = this.settings.raw;
105+
storeLocally(rawSettings) {
37106
chrome.storage.sync.set({[this.documentId]: rawSettings}, function() {});
107+
}
38108

109+
async storeOnServer(rawSettings) {
39110
// Send update to server
40111
const authToken = await Auth.getAuthToken();
41112
if (authToken != null) {
42-
fetch(`https://linenumbers.app/api/v1/storeSettings?document=${this.documentId}`, {
113+
fetch(`https://linenumbers.app/api/v1/documentSettings?document=${this.documentId}&authToken=${authToken}`, {
43114
method: 'POST',
44115
headers: { 'Content-Type': 'application/json' },
45116
body: JSON.stringify(rawSettings),
46117
});
47118
}
48119
}
120+
121+
async store() {
122+
const rawSettings = this.settings.raw;
123+
rawSettings.lastUpdated = Date.now();
124+
125+
this.storeLocally(rawSettings);
126+
this.storeOnServer(rawSettings);
127+
}
49128
}
50129

51130
export const numbering = {

0 commit comments

Comments
 (0)