forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientState.js
More file actions
278 lines (250 loc) · 7.83 KB
/
clientState.js
File metadata and controls
278 lines (250 loc) · 7.83 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @file Helper functions for accessing client state. This state is stored in a
* combination of cookies and HTML5 web storage.
*/
import cookies from 'js-cookie';
var sessionStorage = window.sessionStorage;
import { mergeActivityResult } from './activityUtils';
var clientState = module.exports = {};
clientState.queryParams = require('./utils').queryParams;
/**
* Number of days before client state cookie expires.
* @type {number}
* @private
*/
clientState.EXPIRY_DAYS = 365;
/**
* Maximum number of lines of code that can be stored in the cookie
* @type {number}
* @private
*/
var MAX_LINES_TO_SAVE = 1000;
/**
* Values larger than this result are server-dependent and shouldn't be cached
* in client storage.
*/
clientState.MAXIMUM_CACHABLE_RESULT = 999;
var COOKIE_OPTIONS = {
expires: clientState.EXPIRY_DAYS,
path: '/'
};
clientState.reset = function () {
try {
cookies.remove('lines', {path: '/'});
sessionStorage.clear();
} catch (e) {}
};
/**
* 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. Posts to /milestone
* may be queued, so save the data in sessionStorage to present a consistent
* client view.
* @param {string} scriptName
* @param {number} levelId
* @param {number} timestamp
* @param {string} source
*/
clientState.writeSourceForLevel = function (scriptName, levelId, timestamp, source) {
safelySetItem(createKey(scriptName, levelId, 'source'), JSON.stringify({
source: source,
timestamp: timestamp
}));
};
/**
* Returns the progress attained for the given level.
* @param {string} scriptName The script name
* @param {number} levelId The level
* @returns {number}
*/
clientState.levelProgress = function (scriptName, levelId) {
var progressMap = clientState.allLevelsProgress();
return (progressMap[scriptName] || {})[levelId] || 0;
};
/**
* Tracks the users progress after they click run. Results larger than 999 are
* reserved for server-dependent changes and can't be cached locally.
* @param {boolean} result - Whether the user's solution is successful
* @param {number} lines - Number of lines of code user wrote in this solution
* @param {number} testResult - Indicates pass, fail, perfect
* @param {string} scriptName - Which script this is for
* @param {number} levelId - Which level this is for
*/
clientState.trackProgress = function (result, lines, testResult, scriptName, levelId) {
if (result && isFinite(lines)) {
addLines(lines);
}
var savedResult = clientState.levelProgress(scriptName, levelId);
if (testResult <= clientState.MAXIMUM_CACHABLE_RESULT &&
savedResult !== mergeActivityResult(savedResult, testResult)) {
setLevelProgress(scriptName, levelId, testResult);
}
};
/**
* Write down user progress for an entire script.
* @param {string} scriptName
* @param {Object<String, number>} progress
*/
clientState.batchTrackProgress = function (scriptName, progress) {
var data = {};
var keys = Object.keys(progress);
for (let i = 0; i < keys.length; i++) {
let level = keys[i];
if (progress[level] && progress[level] <= clientState.MAXIMUM_CACHABLE_RESULT) {
data[level] = progress[level];
}
}
var progressMap = clientState.allLevelsProgress();
progressMap[scriptName] = data;
safelySetItem('progress', JSON.stringify(progressMap));
};
/**
* Sets the progress attained for the given level
* @param {string} scriptName The script name
* @param {number} levelId The level
* @param {number} progress Indicates pass, fail, perfect
* @returns {number}
*/
function setLevelProgress(scriptName, levelId, progress) {
var progressMap = clientState.allLevelsProgress();
if (!progressMap[scriptName]) {
progressMap[scriptName] = {};
}
progressMap[scriptName][levelId] = progress;
safelySetItem('progress', JSON.stringify(progressMap));
}
/**
* Returns a map from (string) level id to progress value.
* @return {Object<String, number>}
*/
clientState.allLevelsProgress = function () {
var progressJson = sessionStorage.getItem('progress');
try {
return progressJson ? JSON.parse(progressJson) : {};
} catch (e) {
// Recover from malformed data.
return {};
}
};
/**
* Returns the number of lines completed from the cookie.
* @returns {number}
*/
clientState.lines = function () {
var linesStr = cookies.get('lines');
return isFinite(linesStr) ? Number(linesStr) : 0;
};
/**
* Adds the given number of completed lines.
* @param {number} addedLines
*/
function addLines(addedLines) {
var newLines = Math.min(clientState.lines() + Math.max(addedLines, 0), MAX_LINES_TO_SAVE);
cookies.set('lines', String(newLines), COOKIE_OPTIONS);
}
/**
* 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;
safelySetItem(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;
safelySetItem(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;
}
/**
* Don't throw storage errors in Safari private browsing mode.
*/
function safelySetItem(key, value) {
try {
sessionStorage.setItem(key, value);
} catch (e) {
if (e.name !== "QuotaExceededError") {
throw e;
}
}
}