assets/scripts.js: Make persistent state actually persistent

Use localStorage to save persistent variables.
This commit is contained in:
Vladimir Panteleev 2019-07-28 05:24:03 +00:00
parent 336a65f31a
commit 449e112040

View file

@ -10,9 +10,11 @@ var captions = {};
var currentSegment; var currentSegment;
var nextSegment = null; var nextSegment = null;
var currentMoments = []; var currentMoments = [];
var persistentState = bv.stateHistory;
var globalChoices = {}; var globalChoices = {};
// Persistent state
var ls = window.localStorage || {};
function msToString(ms) { function msToString(ms) {
return new Date(ms).toUTCString().split(' ')[4]; return new Date(ms).toUTCString().split(' ')[4];
} }
@ -23,7 +25,7 @@ function getCurrentMs() {
function preconditionToJS(cond) { function preconditionToJS(cond) {
if (cond[0] == 'persistentState') { if (cond[0] == 'persistentState') {
return '!!persistentState["' + cond[1] + '"]'; return 'JSON.parse(ls["persistentState_' + cond[1] + '"])';
} else if (cond[0] == 'not') { } else if (cond[0] == 'not') {
return '!(' + preconditionToJS(cond[1]) + ')'; return '!(' + preconditionToJS(cond[1]) + ')';
} else if (cond[0] == 'and') { } else if (cond[0] == 'and') {
@ -457,7 +459,7 @@ function applyImpression(impressionData) {
if (impressionData && impressionData.type == 'userState') { if (impressionData && impressionData.type == 'userState') {
for (const [variable, value] of Object.entries(impressionData.data.persistent)) { for (const [variable, value] of Object.entries(impressionData.data.persistent)) {
console.log('persistentState set', variable, '=', value); console.log('persistentState set', variable, '=', value);
persistentState[variable] = value; ls["persistentState_" + variable] = JSON.stringify(value);
} }
} }
} }