-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathclientState.js
More file actions
235 lines (212 loc) · 6.37 KB
/
Copy pathclientState.js
File metadata and controls
235 lines (212 loc) · 6.37 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @file Helper functions for accessing client state. This state is stored in a
* combination of cookies and HTML5 web storage.
*/
import {trySetSessionStorage} from '../utils';
import {mergeActivityResult} from './activityUtils';
// Note: sessionStorage is not shared between tabs.
var sessionStorage = window.sessionStorage;
var clientState = (module.exports = {});
clientState.queryParams = require('./utils').queryParams;
/**
* Number of days before client state cookie expires.
* @type {number}
* @private
*/
clientState.EXPIRY_DAYS = 365;
/**
* Values larger than this result are server-dependent and shouldn't be cached
* in client storage.
*/
clientState.MAXIMUM_CACHABLE_RESULT = 999;
clientState.reset = function () {
try {
sessionStorage.clear();
} catch (e) {}
};
/**
* Clear progress-related values from session storage.
*/
clientState.clearProgress = function () {
sessionStorage.removeItem('progress');
sessionStorage.removeItem('lines');
removeItemsWithPrefix(sessionStorage, 'source_');
};
/**
* Returns the client-cached copy of the level source for the given script
* level, if it's newer than the given timestamp.
* @param {string} scriptName
* @param {number} levelId
* @param {number=} timestamp
* @returns {string|undefined} Cached copy of the level source, or undefined if
* the cached copy is missing/stale.
*/
clientState.sourceForLevel = function (scriptName, levelId, timestamp) {
var data = sessionStorage.getItem(createKey(scriptName, levelId, 'source'));
if (data) {
var parsed;
try {
parsed = JSON.parse(data);
} catch (e) {
return;
}
if (!timestamp || parsed.timestamp > timestamp) {
return parsed.source;
}
}
};
/**
* Cache a copy of the level source along with a timestamp.
* @param {string} scriptName
* @param {number} levelId
* @param {number} timestamp
* @param {string} source
*/
clientState.writeSourceForLevel = function (
scriptName,
levelId,
timestamp,
source
) {
if (source === undefined) {
return;
}
trySetSessionStorage(
createKey(scriptName, levelId, 'source'),
JSON.stringify({
source: source,
timestamp: timestamp,
})
);
};
/**
* Merges the given testResult for the given level into the progress
* data stored in session storage.
* @param {string} scriptName
* @param {number} levelId
* @param {TestResults} testResult
*/
clientState.trackProgress = function (scriptName, levelId, testResult) {
// testResult values > 1000 are for server use only and should not be stored
// locally
if (!testResult || testResult > clientState.MAXIMUM_CACHABLE_RESULT) {
return;
}
const progressData = levelProgressByScript();
if (!progressData[scriptName]) {
progressData[scriptName] = {};
}
const savedResult = progressData[scriptName][levelId] || 0;
const mergedResult = mergeActivityResult(savedResult, testResult);
if (mergedResult !== savedResult) {
progressData[scriptName][levelId] = mergedResult;
trySetSessionStorage('progress', JSON.stringify(progressData));
}
};
/**
* Returns the level progress map for the given script.
* @param {string} scriptName The script name
* @returns {Object<number, number>} map from levelId -> testResult
*/
clientState.levelProgress = function (scriptName) {
var progressMap = levelProgressByScript();
return progressMap[scriptName] || {};
};
/**
* Returns a map from script name to level progress map
* @return {Object<String, Object>}
*/
function levelProgressByScript() {
var progressJson = sessionStorage.getItem('progress');
try {
return progressJson ? JSON.parse(progressJson) : {};
} catch (e) {
// Recover from malformed data.
return {};
}
}
/**
* Returns whether or not the user has seen a given video based on contents of the local storage
* @param videoId
* @returns {*}
*/
clientState.hasSeenVideo = function (videoId) {
return hasSeenVisualElement('video', videoId);
};
/**
* Records that a user has seen a given video in local storage
* @param videoId
*/
clientState.recordVideoSeen = function (videoId) {
recordVisualElementSeen('video', videoId);
};
/**
* Returns whether or not the user has seen the given callout based on contents of the local storage
* @param calloutId
* @returns {boolean}
*/
clientState.hasSeenCallout = function (calloutId) {
return hasSeenVisualElement('callout', calloutId);
};
/**
* Records that a user has seen a given callout in local storage
* @param calloutId
*/
clientState.recordCalloutSeen = function (calloutId) {
recordVisualElementSeen('callout', calloutId);
};
/**
* Private helper for videos and callouts - persists info in the local storage that a given element has been seen
* @param visualElementType
* @param visualElementId
*/
function recordVisualElementSeen(visualElementType, visualElementId) {
var elementSeenJson = sessionStorage.getItem(visualElementType) || '{}';
var elementSeen;
try {
elementSeen = JSON.parse(elementSeenJson);
elementSeen[visualElementId] = true;
trySetSessionStorage(visualElementType, JSON.stringify(elementSeen));
} catch (e) {
//Something went wrong parsing the json. Blow it up and just put in the new callout
elementSeen = {};
elementSeen[visualElementId] = true;
trySetSessionStorage(visualElementType, JSON.stringify(elementSeen));
}
}
/**
* Private helper for videos and callouts - looks in local storage to see if the element has been seen
* @param visualElementType
* @param visualElementId
*/
function hasSeenVisualElement(visualElementType, visualElementId) {
var elementSeenJson = sessionStorage.getItem(visualElementType) || '{}';
try {
var elementSeen = JSON.parse(elementSeenJson);
return elementSeen[visualElementId] === true;
} catch (e) {
return false;
}
}
/**
* Creates standardized keys for storing values in sessionStorage.
* @param {string} scriptName
* @param {number} levelId
* @param {string=} prefix
* @return {string}
*/
function createKey(scriptName, levelId, prefix) {
return (prefix ? prefix + '_' : '') + scriptName + '_' + levelId;
}
/**
* Removes all items from the given sessionStorage object that start with the
* given prefix.
*
* @param {Storage} sessionStorage
* @param {string} prefix
*/
function removeItemsWithPrefix(sessionStorage, prefix) {
Object.keys(sessionStorage)
.filter(key => key.startsWith(prefix))
.forEach(key => sessionStorage.removeItem(key));
}