const K_PREFIX = 'dmx_';

/**
 * 根据一个key，创建一个Storage
 * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Window/localStorage
 * @param {string} key - 字符串，一个key
 * @returns {object}
 */
export class MyLocalStorage {
  constructor(key) {
    if (!key) {
      throw new Error('非法的key，无法创建Storage');
    }

    this.key = K_PREFIX + key;
  }

  getItem() {
    localStorage.getItem(this.key);
  }

  setItem(value) {
    localStorage.setItem(this.key, value);
  }

  removeItem() {
    localStorage.removeItem(this.key);
  }
}

/**
 * 根据一个key，创建一个Storage
 * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Window/sessionStorage
 * @param {string} key
 * @returns
 */
export class MySessionStorage {
  constructor(key) {
    if (!key) {
      throw new Error('非法的key，无法创建Storage');
    }

    this.key = K_PREFIX + key;
  }

  getItem() {
    sessionStorage.getItem(this.key);
  }

  setItem(value) {
    sessionStorage.setItem(this.key, value);
  }

  removeItem() {
    sessionStorage.removeItem(this.key);
  }
}
