All files storage.ts

100% Statements 33/33
100% Branches 8/8
100% Functions 14/14
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                  1x 1x             2x 2x 1x   1x 1x     1x   1x               2x 1x   1x   2x             1x             2x 2x 1x   1x 1x     1x   1x               2x 1x   1x   2x             1x             1x             2x             1x             1x             1x                 1x                       1x          
/*
集本地存储,内存存储,发布订阅于一身
*/
 
export class Storage<TKey extends string> {
  private readonly cacheMap: Map<string, any>
  private readonly eventKeyNameSpace: string
 
  constructor() {
    this.cacheMap = new Map<string, any>()
    this.eventKeyNameSpace = 'bbg_custom_event_'
  }
 
  /**
   * 本地存储获取一个值,如果是数字字面量,一定是 number,因为基于 JSON.parse
   */
  getItem<T = string>(key: TKey) {
    let value = window.localStorage.getItem(key)
    if (value === null) {
      return value
    }
    try {
      value = JSON.parse(value)
    } catch (err) {
      // eslint no empty
      console.log()
    }
    return value as T | null
  }
 
  /**
   * 本地存储设置一个值,value可以是对象
   */
  setItem(key: TKey, value: { [key: string]: any } | string | number) {
    let retValue
    if (typeof value === 'object') {
      retValue = JSON.stringify(value)
    } else {
      retValue = String(value)
    }
    window.localStorage.setItem(key, retValue)
  }
 
  /**
   * 清空localstorage
   */
  clear() {
    window.localStorage.clear()
  }
 
  /**
   * 本地会话存储获取一个值,如果是数字字面量,一定是 number,因为基于 JSON.parse
   */
  getSessionItem<T = string>(key: TKey) {
    let value = window.sessionStorage.getItem(key)
    if (value === null) {
      return value
    }
    try {
      value = JSON.parse(value)
    } catch (err) {
      // eslint no empty
      console.log()
    }
    return value as T | null
  }
 
  /**
   * 本地会话存储设置一个值,value可以是一个值
   */
  setSessionItem(key: TKey, value: unknown) {
    let retValue
    if (typeof value === 'object') {
      retValue = JSON.stringify(value)
    } else {
      retValue = String(value)
    }
    window.sessionStorage.setItem(key, retValue)
  }
 
  /**
   * 清空sessionStorage
   */
  sessionClear() {
    window.sessionStorage.clear()
  }
 
  /**
   * 在内存设 kv
   */
  set(key: TKey, value: any) {
    this.cacheMap.set(key, value)
  }
 
  /**
   * 在内存获取 kv
   */
  get<T = string>(key: TKey) {
    return this.cacheMap.get(key) as T | null
  }
 
  /**
   * 在内存移除 kv
   */
  remove(key: TKey) {
    this.cacheMap.delete(key)
  }
 
  /**
   * 本地存储移除一个值
   */
  removeItem(key: TKey) {
    window.localStorage.removeItem(key)
  }
 
  /**
   * 本地会话存储移除一个值
   */
  removeSessionItem(key: TKey) {
    window.sessionStorage.removeItem(key)
  }
 
  /**
   * 监听事件
   * @param key 要监听的事件名
   * @param callback 监听回调
   */
  on(key: TKey, callback: (params: CustomEventInit) => void) {
    window.addEventListener(this.eventKeyNameSpace + key, callback)
  }
 
  /**
   * 发射事件
   * @param key 要发射的事件名
   * @param eventInit 事件参数
   * @param eventInit.detail 携带的数据
   * @param eventInit.bubbles bubbles 是否可以冒泡
   * @param eventInit.cancelable cancelable 是否可以取消
   */
  emit(key: TKey, eventInit?: CustomEventInit) {
    window.dispatchEvent(
      new CustomEvent(this.eventKeyNameSpace + key, eventInit),
    )
  }
}