OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / lib / clock-list.js

33 lines · javascript ref:
export const LOADING_STATUS = {
  INITIAL: "initial",
  LOADING: "loading",
  LOADED: "loaded",
  ALL_LOADED: "all_loaded",
  ERROR: "error",
}
export const calcMinClock = (e) => {
  const t = e[e.length - 1]
  return t ? Math.max(0, (t.clock ?? 0) - 1) : null
}
export function mergeSortedItems(e, t, n = "append") {
  if (!t.length) return e
  if (!e.length) return t
  const o = new Set(e.map((e) => e.id)),
    r = t.filter((e) => !o.has(e.id))
  return "prepend" === n ? r.concat(e) : e.concat(r)
}
export function prependSortedItems(e, { sortedItems: t, minClock: n, loadingStatus: o }) {
  return {
    sortedItems: mergeSortedItems(t, e, "prepend"),
    minClock: n ?? calcMinClock(t),
    loadingStatus: o === LOADING_STATUS.INITIAL ? LOADING_STATUS.LOADED : o,
  }
}
export function appendSortedItems(e, { sortedItems: t, allLoaded: n }) {
  return {
    sortedItems: mergeSortedItems(t, e, "append"),
    minClock: calcMinClock(t),
    loadingStatus: n ? LOADING_STATUS.ALL_LOADED : LOADING_STATUS.LOADED,
  }
}