OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / preferences / preferences.saga.js

70 lines · javascript ref:
import {
  put as e,
  takeLatest as t,
  call as r,
  getContext as i,
  select as a,
} from "redux-saga/effects"
import { ENV_CONTEXT_KEY as n, LOCAL_PREFERENCES_CONTEXT_KEY as o } from "../constants"
import {
  updatePreferencesCmd as d,
  updatePreferencesSuccess as l,
  updatePreferencesError as c,
  preferencesDataEvt as f,
  preferencesMigrateEvt as s,
} from "./preferences.action"
import { taskErrorSaga as y } from "../error"
import { PREFERENCES as p, PREFERENCES_STORAGE_KEY as m } from "./preferences.constant"
import { getPreferencesData as u } from "./preferences.reducer"
import { isMigratedPreferencesKey as D, isValidPreferencesKey as v } from "./preferences.model"
export function* preferenceUpdateTask(t, n) {
  try {
    if (!v(t)) throw new Error("Preferences: invalid key")
    const d = { ...(yield a(u)), [t]: n },
      l = yield i(o)
    return (yield r(l.setItem, m, d), yield r(l.flush), yield e(f({ [t]: n })), !0)
  } catch (e) {
    yield y(`preferenceUpdate: ${t}`, e)
  }
  return !1
}
function* g(t) {
  const r = yield i(n)
  !1 === r?.isBlueBuildFlavour &&
    void 0 === t?.[p.CORE_WARNINGS_ENABLED] &&
    (yield e(f({ [p.CORE_WARNINGS_ENABLED]: !0 })))
}
export function* preferencesInitData() {
  try {
    const t = yield i(o)
    yield r(t.open)
    const a = yield r(t.getItem, m)
    if ((yield g(a), !a)) return
    const {
      validData: n,
      migrateData: d,
      invalidData: l,
    } = Object.keys(a).reduce(
      (e, t) =>
        v(t)
          ? ((e.validData[t] = a[t]), e)
          : D(t)
            ? ((e.migrateData[t] = a[t]), e)
            : ((e.invalidData = !0), e),
      { validData: {}, migrateData: {}, invalidData: !1 },
    )
    yield e(f(n))
    let c = l
    ;(Object.keys(d).length && (yield e(s(d)), (c = !0)),
      c && (yield r(t.setItem, m, n), yield r(t.flush)))
  } catch (e) {
    yield y("preferencesInitData", e)
  }
}
export function* preferencesSaga() {
  yield t(d, function* ({ payload: t }) {
    const { key: r, value: i } = t
    ;(yield preferenceUpdateTask(r, i)) ? yield e(l(r)) : yield e(c(r))
  })
}