-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (47 loc) · 1.31 KB
/
index.js
File metadata and controls
56 lines (47 loc) · 1.31 KB
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
'use strict'
const ApiClient = require('./api');
module.exports = class StatelessClient {
constructor(projectKey, scope) {
this.projectKey = projectKey;
this.scope = scope;
this.changeIds = [];
this.api = new ApiClient();
this._data = null;
this._getInitialData();
}
async getValue(key) {
await this._getInitialData();
return this._data[key];
}
async setValue(key, value) {
await this.api.updateValue(key, value);
this._getInitialData()
}
async _getInitialData() {
const responseJson = await this.api.getValue(this.projectKey, this.scope)
const currentData = responseJson['state']['data'];
this._setChangeId(responseJson['changeId']);
this._setData(currentData)
}
get _currentChangeId() {
if (this.changeIds.length > 0) {
return this.changeIds[0]
} else {
throw "Store not initialized before writing, you must check out by initing this class."
}
}
_setChangeId(changeId) {
this.changeIds.unshift(changeId);
}
_setData(data) {
this._data = data;
}
async _getChangeInfo(changeId) {
const response = await fetch(`${this.apiURL}/api/changes/${changeId}`);
if (!response.ok) {
throw "Could not connect to Stateless store"
}
const changeInfo = await response.json();
return changeInfo;
}
};