Skip to content

Commit 8084c55

Browse files
committed
formatting
1 parent 70e6b3d commit 8084c55

1 file changed

Lines changed: 46 additions & 28 deletions

File tree

‎src/State.js‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import Log from './Log';
55
import random from './random';
66

77
export default class State {
8-
constructor({id, nonce, data, created}={}) {
8+
constructor({id, nonce, data, created} = {}) {
99
this._id = id || random();
1010
this._data = data;
11-
12-
if (nonce === true){
11+
12+
if (nonce === true) {
1313
this._nonce = random();
1414
}
15-
else if (nonce){
15+
else if (nonce) {
1616
this._nonce = nonce;
1717
}
18-
18+
1919
if (typeof created === 'number' && created > 0) {
20-
this._created = created;
20+
this._created = created;
2121
}
2222
else {
23-
this._created = parseInt(Date.now() / 1000);
23+
this._created = parseInt(Date.now() / 1000);
2424
}
2525
}
26-
26+
2727
get id() {
2828
return this._id;
2929
}
@@ -40,39 +40,57 @@ export default class State {
4040
toStorageString() {
4141
Log.info("State.toStorageString");
4242
return JSON.stringify({
43-
id:this.id,
44-
nonce:this.nonce,
45-
data:this.data,
46-
created:this.created
43+
id: this.id,
44+
nonce: this.nonce,
45+
data: this.data,
46+
created: this.created
4747
});
4848
}
49-
50-
static fromStorageString(storageString){
49+
50+
static fromStorageString(storageString) {
5151
Log.info("State.fromStorageString");
5252
return new State(JSON.parse(storageString));
5353
}
54-
55-
static clearStaleState(storage, age){
54+
55+
static clearStaleState(storage, age) {
5656
Log.info("State.clearStaleState");
57-
58-
var cutoff = Date.now()/1000 - age;
59-
57+
58+
var cutoff = Date.now() / 1000 - age;
59+
6060
return storage.getAllKeys().then(keys => {
6161
Log.info("got keys", keys);
62-
62+
6363
var promises = [];
64-
for(let key of keys){
64+
for (let key of keys) {
6565
var p = storage.get(key).then(item => {
66-
var state = State.fromStorageString(item)
67-
68-
Log.info("got item from key", key, state.created);
69-
70-
if (state.created <= cutoff) {
71-
Log.info("key being removed", key);
66+
let remove = false;
67+
68+
if (item) {
69+
try {
70+
var state = State.fromStorageString(item)
71+
72+
Log.info("got item from key: ", key, state.created);
73+
74+
if (state.created <= cutoff) {
75+
remove = true;
76+
}
77+
}
78+
catch (e) {
79+
Log.error("Error parsing state for key", key, e.message);
80+
remove = true;
81+
}
82+
}
83+
else {
84+
Log.info("no item in storage for key: ", key);
85+
remove = true;
86+
}
87+
88+
if (remove) {
89+
Log.info("removed item for key: ", key);
7290
return storage.remove(key);
7391
}
7492
});
75-
93+
7694
promises.push(p);
7795
}
7896

0 commit comments

Comments
 (0)