All files / ima/storage SessionMapStorage.js

92% Statements 23/25
80% Branches 4/5
90% Functions 9/10
92% Lines 23/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119            3x                 4x                   6x             6x             6x             6x 6x       6x             8x             5x               16x     16x 15x     16x 16x             2x 2x 2x             13x 13x 13x             1x                     3x  
import ns from '../namespace';
import MapStorage from './MapStorage';
import SessionStorage from './SessionStorage';
import Storage from './Storage';
import CacheEntry from '../cache/CacheEntry';
 
ns.namespace('ima.storage');
 
/**
 * The {@codelink SessionMap} storage is an implementation of the
 * {@codelink Storage} interface acting as a synchronization proxy between
 * the underlying map storage and the {@code sessionStorage} DOM storage.
 */
export default class SessionMapStorage extends Storage {
  static get $dependencies() {
    return [MapStorage, SessionStorage];
  }
 
  /**
	 * Initializes the storage.
	 *
	 * @param {MapStorage} map The map storage to use.
	 * @param {SessionStorage} session The session storage to use.
	 */
  constructor(map, session) {
    super();
 
    /**
		 * The map storage, synced with the session storage.
		 *
		 * @type {MapStorage}
		 */
    this._map = map;
 
    /**
		 * The session storage, synced with the map storage.
		 *
		 * @type {SessionStorage}
		 */
    this._session = session;
  }
 
  /**
	 * @inheritdoc
	 */
  init() {
    this._map.clear();
    for (let key of this._session.keys()) {
      this._map.set(key, this._session[key]);
    }
 
    return this;
  }
 
  /**
	 * @inheritdoc
	 */
  has(key) {
    return this._map.has(key);
  }
 
  /**
	 * @inheritdoc
	 */
  get(key) {
    return this._map.get(key);
  }
 
  /**
	 * @inheritdoc
	 */
  set(key, value) {
    let canBeSerializedToJSON =
      !(value instanceof Promise) &&
      (!(value instanceof 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();
  }
}
 
ns.ima.storage.SessionMapStorage = SessionMapStorage;