UNPKG

4.65 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _GenericError = require("../error/GenericError");
8
9var _GenericError2 = _interopRequireDefault(_GenericError);
10
11var _Storage = require("./Storage");
12
13var _Storage2 = _interopRequireDefault(_Storage);
14
15var _Window = require("../window/Window");
16
17var _Window2 = _interopRequireDefault(_Window);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21/**
22 * Implementation of the {@codelink Storage} interface that relies on the
23 * native {@code sessionStorage} DOM storage for storing its entries.
24 */
25class SessionStorage extends _Storage2.default {
26 static get $dependencies() {
27 return [_Window2.default];
28 }
29 /**
30 * Initializes the session storage.
31 * @param {Window} window
32 */
33
34
35 constructor(window) {
36 super();
37 /**
38 * The DOM storage providing the actual storage of the entries.
39 *
40 * @type {Storage}
41 */
42
43 this._storage = window.getWindow().sessionStorage;
44 }
45 /**
46 * @inheritdoc
47 */
48
49
50 init() {
51 return this;
52 }
53 /**
54 * @inheritdoc
55 */
56
57
58 has(key) {
59 return !!this._storage.getItem(key);
60 }
61 /**
62 * @inheritdoc
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 * @inheritdoc
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 * @inheritdoc
101 */
102
103
104 delete(key) {
105 this._storage.removeItem(key);
106
107 return this;
108 }
109 /**
110 * @inheritdoc
111 */
112
113
114 clear() {
115 this._storage.clear();
116
117 return this;
118 }
119 /**
120 * @inheritdoc
121 */
122
123
124 keys() {
125 return new StorageIterator(this._storage);
126 }
127 /**
128 * @override
129 */
130
131
132 size() {
133 return this._storage.length;
134 }
135 /**
136 * Deletes the oldest entry in this storage.
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
164exports.default = SessionStorage;
165/**
166 * Implementation of the iterator protocol and the iterable protocol for DOM
167 * storage keys.
168 *
169 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
170 */
171
172class StorageIterator {
173 /**
174 * Initializes the DOM storage iterator.
175 *
176 * @param {Storage} storage The DOM storage to iterate through.
177 */
178 constructor(storage) {
179 /**
180 * The DOM storage being iterated.
181 *
182 * @type {Storage}
183 */
184 this._storage = storage;
185 /**
186 * The current index of the DOM storage key this iterator will return
187 * next.
188 *
189 * @type {number}
190 */
191
192 this._currentKeyIndex = 0;
193 }
194 /**
195 * Iterates to the next item. This method implements the iterator protocol.
196 *
197 * @return {{done: boolean, value: (undefined|string)}} The next value in
198 * the sequence and whether the iterator is done iterating through
199 * the values.
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 * Returns the iterator for this object (this iterator). This method
221 * implements the iterable protocol and provides compatibility with the
222 * {@code for..of} loops.
223 *
224 * @return {StorageIterator} This iterator.
225 */
226
227
228 [Symbol.iterator]() {
229 return this;
230 }
231
232}
233
234typeof $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});