1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _GenericError = require("../error/GenericError");
|
8 |
|
9 | var _GenericError2 = _interopRequireDefault(_GenericError);
|
10 |
|
11 | var _Storage = require("./Storage");
|
12 |
|
13 | var _Storage2 = _interopRequireDefault(_Storage);
|
14 |
|
15 | var _Window = require("../window/Window");
|
16 |
|
17 | var _Window2 = _interopRequireDefault(_Window);
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | class SessionStorage extends _Storage2.default {
|
26 | static get $dependencies() {
|
27 | return [_Window2.default];
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | constructor(window) {
|
36 | super();
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | this._storage = window.getWindow().sessionStorage;
|
44 | }
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | init() {
|
51 | return this;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | has(key) {
|
59 | return !!this._storage.getItem(key);
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | get(key) {
|
67 | try {
|
68 | return JSON.parse(this._storage.getItem(key)).value;
|
69 | } catch (error) {
|
70 | throw new _GenericError2.default('ima.storage.SessionStorage.get: Failed to parse a session ' + `storage item value identified by the key ${key}: ` + error.message);
|
71 | }
|
72 | }
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | set(key, value) {
|
79 | try {
|
80 | this._storage.setItem(key, JSON.stringify({
|
81 | created: Date.now(),
|
82 | value
|
83 | }));
|
84 | } catch (error) {
|
85 | let storage = this._storage;
|
86 | let isItemTooBig = storage.length === 0 || storage.length === 1 && storage.key(0) === key;
|
87 |
|
88 | if (isItemTooBig) {
|
89 | throw error;
|
90 | }
|
91 |
|
92 | this._deleteOldestEntry();
|
93 |
|
94 | this.set(key, value);
|
95 | }
|
96 |
|
97 | return this;
|
98 | }
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 | delete(key) {
|
105 | this._storage.removeItem(key);
|
106 |
|
107 | return this;
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | clear() {
|
115 | this._storage.clear();
|
116 |
|
117 | return this;
|
118 | }
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | keys() {
|
125 | return new StorageIterator(this._storage);
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 | size() {
|
133 | return this._storage.length;
|
134 | }
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | _deleteOldestEntry() {
|
141 | let oldestEntry = {
|
142 | key: null,
|
143 | created: Date.now() + 1
|
144 | };
|
145 |
|
146 | for (let key of this.keys()) {
|
147 | let value = JSON.parse(this._storage.getItem(key));
|
148 |
|
149 | if (value.created < oldestEntry.created) {
|
150 | oldestEntry = {
|
151 | key,
|
152 | created: value.created
|
153 | };
|
154 | }
|
155 | }
|
156 |
|
157 | if (typeof oldestEntry.key === 'string') {
|
158 | this.delete(oldestEntry.key);
|
159 | }
|
160 | }
|
161 |
|
162 | }
|
163 |
|
164 | exports.default = SessionStorage;
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | class StorageIterator {
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 | constructor(storage) {
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 | this._storage = storage;
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | this._currentKeyIndex = 0;
|
193 | }
|
194 | |
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | next() {
|
204 | if (this._currentKeyIndex >= this._storage.length) {
|
205 | return {
|
206 | done: true,
|
207 | value: undefined
|
208 | };
|
209 | }
|
210 |
|
211 | let key = this._storage.key(this._currentKeyIndex);
|
212 |
|
213 | this._currentKeyIndex++;
|
214 | return {
|
215 | done: false,
|
216 | value: key
|
217 | };
|
218 | }
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | [Symbol.iterator]() {
|
229 | return this;
|
230 | }
|
231 |
|
232 | }
|
233 |
|
234 | typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/storage/SessionStorage', [], function (_export, _context) {
|
235 | 'use strict';
|
236 | return {
|
237 | setters: [],
|
238 | execute: function () {
|
239 | _export('default', exports.default);
|
240 | }
|
241 | };
|
242 | });
|