UNPKG

2.81 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _MapStorage = require('./MapStorage');
8
9var _MapStorage2 = _interopRequireDefault(_MapStorage);
10
11var _SessionStorage = require('./SessionStorage');
12
13var _SessionStorage2 = _interopRequireDefault(_SessionStorage);
14
15var _Storage = require('./Storage');
16
17var _Storage2 = _interopRequireDefault(_Storage);
18
19var _CacheEntry = require('../cache/CacheEntry');
20
21var _CacheEntry2 = _interopRequireDefault(_CacheEntry);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25/**
26 * The {@codelink SessionMap} storage is an implementation of the
27 * {@codelink Storage} interface acting as a synchronization proxy between
28 * the underlying map storage and the {@code sessionStorage} DOM storage.
29 */
30class SessionMapStorage extends _Storage2.default {
31 static get $dependencies() {
32 return [_MapStorage2.default, _SessionStorage2.default];
33 }
34
35 /**
36 * Initializes the storage.
37 *
38 * @param {MapStorage} map The map storage to use.
39 * @param {SessionStorage} session The session storage to use.
40 */
41 constructor(map, session) {
42 super();
43
44 /**
45 * The map storage, synced with the session storage.
46 *
47 * @type {MapStorage}
48 */
49 this._map = map;
50
51 /**
52 * The session storage, synced with the map storage.
53 *
54 * @type {SessionStorage}
55 */
56 this._session = session;
57 }
58
59 /**
60 * @inheritdoc
61 */
62 init() {
63 this._map.clear();
64 for (let key of this._session.keys()) {
65 this._map.set(key, this._session[key]);
66 }
67
68 return this;
69 }
70
71 /**
72 * @inheritdoc
73 */
74 has(key) {
75 return this._map.has(key);
76 }
77
78 /**
79 * @inheritdoc
80 */
81 get(key) {
82 return this._map.get(key);
83 }
84
85 /**
86 * @inheritdoc
87 */
88 set(key, value) {
89 let canBeSerializedToJSON = !(value instanceof Promise) && (!(value instanceof _CacheEntry2.default) || !(value.getValue() instanceof Promise));
90 if (canBeSerializedToJSON) {
91 this._session.set(key, value);
92 }
93
94 this._map.set(key, value);
95 return this;
96 }
97
98 /**
99 * @inheritdoc
100 */
101 delete(key) {
102 this._session.delete(key);
103 this._map.delete(key);
104 return this;
105 }
106
107 /**
108 * @inheritdoc
109 */
110 clear() {
111 this._session.clear();
112 this._map.clear();
113 return this;
114 }
115
116 /**
117 * @inheritdoc
118 */
119 keys() {
120 return this._map.keys();
121 }
122
123 /**
124 * @override
125 */
126 size() {
127 return this._map.size();
128 }
129}
130exports.default = SessionMapStorage;
131
132typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/storage/SessionMapStorage', [], function (_export, _context) {
133 'use strict';
134 return {
135 setters: [],
136 execute: function () {
137 _export('default', exports.default);
138 }
139 };
140});