All files / src/common cid-log.ts

0% Statements 0/10
0% Branches 0/2
0% Functions 0/6
0% Lines 0/10

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                                                                                       
import localforage from 'localforage'
import { setup } from '../setup/internal'
 
 
const FS_CID_LOG_PREFIX = "webnative.wnfs_cid_log"
 
 
function key() {
  return FS_CID_LOG_PREFIX + "-" + setup.endpoints.lobby
}
 
 
// QUERYING
 
 
export async function get(): Promise<Array<string>> {
  return (await localforage.getItem(key())) || []
}
 
export async function index(cid: string): Promise<[number, number]> {
  const log = await get()
  return [ log.indexOf(cid), log.length ]
}
 
export async function newest(): Promise<string> {
  return (await get())[0]
}
 
 
 
// MUTATION
 
 
export async function add(cid: string): Promise<void> {
  const log = await get()
  const newLog = [ cid, ...log ].slice(0, 1000)
  await localforage.setItem(key(), newLog)
}
 
 
export async function clear(): Promise<void> {
  await localforage.removeItem(key())
}