UNPKG

2.83 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 * Initializes the storage.
36 *
37 * @param {MapStorage} map The map storage to use.
38 * @param {SessionStorage} session The session storage to use.
39 */
40
41
42 constructor(map, session) {
43 super();
44 /**
45 * The map storage, synced with the session storage.
46 *
47 * @type {MapStorage}
48 */
49
50 this._map = map;
51 /**
52 * The session storage, synced with the map storage.
53 *
54 * @type {SessionStorage}
55 */
56
57 this._session = session;
58 }
59 /**
60 * @inheritdoc
61 */
62
63
64 init() {
65 this._map.clear();
66
67 for (let key of this._session.keys()) {
68 this._map.set(key, this._session[key]);
69 }
70
71 return this;
72 }
73 /**
74 * @inheritdoc
75 */
76
77
78 has(key) {
79 return this._map.has(key);
80 }
81 /**
82 * @inheritdoc
83 */
84
85
86 get(key) {
87 return this._map.get(key);
88 }
89 /**
90 * @inheritdoc
91 */
92
93
94 set(key, value) {
95 let canBeSerializedToJSON = !(value instanceof Promise) && (!(value instanceof _CacheEntry2.default) || !(value.getValue() instanceof Promise));
96
97 if (canBeSerializedToJSON) {
98 this._session.set(key, value);
99 }
100
101 this._map.set(key, value);
102
103 return this;
104 }
105 /**
106 * @inheritdoc
107 */
108
109
110 delete(key) {
111 this._session.delete(key);
112
113 this._map.delete(key);
114
115 return this;
116 }
117 /**
118 * @inheritdoc
119 */
120
121
122 clear() {
123 this._session.clear();
124
125 this._map.clear();
126
127 return this;
128 }
129 /**
130 * @inheritdoc
131 */
132
133
134 keys() {
135 return this._map.keys();
136 }
137 /**
138 * @override
139 */
140
141
142 size() {
143 return this._map.size();
144 }
145
146}
147
148exports.default = SessionMapStorage;
149
150typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/storage/SessionMapStorage', [], function (_export, _context) {
151 'use strict';
152 return {
153 setters: [],
154 execute: function () {
155 _export('default', exports.default);
156 }
157 };
158});