@ima/core
Version:
IMA.js framework for isomorphic javascript application
99 lines (98 loc) • 2.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "SessionMapStorage", {
enumerable: true,
get: function() {
return SessionMapStorage;
}
});
const _MapStorage = require("./MapStorage");
const _SessionStorage = require("./SessionStorage");
const _Storage = require("./Storage");
const _CacheEntry = require("../cache/CacheEntry");
class SessionMapStorage extends _Storage.Storage {
/**
* The map storage, synced with the session storage.
*/ _map;
/**
* The session storage, synced with the map storage.
*/ _session;
static get $dependencies() {
return [
_MapStorage.MapStorage,
_SessionStorage.SessionStorage
];
}
/**
* Initializes the storage.
*
* @param map The map storage to use.
* @param session The session storage to use.
*/ constructor(map, session){
super();
this._map = map;
this._session = session;
}
/**
* @inheritDoc
*/ init() {
this._map.clear();
for (const key of this._session.keys()){
if (!key) {
continue;
}
const sessionValue = this._session.get(key);
if (sessionValue) {
this._map.set(key, sessionValue);
}
}
return this;
}
/**
* @inheritDoc
*/ has(key) {
return this._map.has(key);
}
/**
* @inheritDoc
*/ get(key) {
return this._map.get(key);
}
/**
* @inheritDoc
*/ set(key, value) {
const canBeSerializedToJSON = !(value instanceof Promise) && (!(value instanceof _CacheEntry.CacheEntry) || !(value.getValue() instanceof Promise));
if (canBeSerializedToJSON) {
this._session.set(key, value);
}
this._map.set(key, value);
return this;
}
/**
* @inheritDoc
*/ delete(key) {
this._session.delete(key);
this._map.delete(key);
return this;
}
/**
* @inheritDoc
*/ clear() {
this._session.clear();
this._map.clear();
return this;
}
/**
* @inheritDoc
*/ keys() {
return this._map.keys();
}
/**
* @override
*/ size() {
return this._map.size();
}
}
//# sourceMappingURL=SessionMapStorage.js.map