-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjssql.session.storage.js
105 lines (98 loc) · 2.17 KB
/
jssql.session.storage.js
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
/**
* Class SessionStorage
*/
jsSQL.registerStorageEngine("SessionStorage", {
/**
* @constructor
*/
construct : function SessionStorage() {
if (!window.sessionStorage || typeof sessionStorage.setItem != "function")
throw new Error("sessionStorage is not supported");
},
setMany : function(map, next) {
jsSQL.nextTick(function() {
var err = null;
try {
for ( var key in map )
sessionStorage.setItem( key, JSON.stringify(map[key]) );
} catch (ex) {
err = ex;
}
if (next)
next(err);
});
},
getMany : function(keys, next) {
jsSQL.nextTick(function() {
var err = null, out = [];
try {
for (var i = 0, l = keys.length; i < l; i++)
out.push( JSON.parse(sessionStorage.getItem( keys[i] )) );
} catch (ex) {
err = ex;
}
if (next)
next(err, out);
});
},
/**
* Delete multiple items. If everything goes well, calls the onSuccess
* callback. Otherwise calls the onError callback.
* @param {Array} keys - An array of keys to delete
* @param {Function} onSuccess - This is called on success without arguments
* @param {Function} onError - This is called on error with the error as
* single argument
* @return {void} undefined - This method is async. so use the callbacks
*/
unsetMany : function(keys, next) {
jsSQL.nextTick(function() {
var err = null;
try {
for (var i = 0, l = keys.length; i < l; i++)
sessionStorage.removeItem( keys[i] );
} catch (ex) {
err = ex;
}
if (next)
next(err);
});
},
set : function(key, value, next) {
jsSQL.nextTick(function() {
var err = null;
try {
sessionStorage.setItem( key, JSON.stringify(value) );
} catch (ex) {
err = ex;
}
if (next)
next(err);
});
},
get : function(key, next) {
jsSQL.nextTick(function() {
var err = null, out;
try {
out = JSON.parse(sessionStorage.getItem( key ));
} catch (ex) {
err = ex;
}
if (next)
next(err, out);
});
},
unset : function(key, next) {
jsSQL.nextTick(function() {
var err = null;
try {
sessionStorage.removeItem( key );
} catch (ex) {
err = ex;
}
if (next)
next(err);
});
}
}, {
label : "Session Storage"
});