OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-indexing / index.js

123 lines · javascript ref:
const i = require("hyperbee"),
  { SchemaBuilder: e } = require("@holepunchto/keet-core-schemas"),
  { isFeatureSupported: t } = require("@holepunchto/keet-core-api"),
  a = require("./lib/internal"),
  s = require("./lib/plugins"),
  n = require("./lib/chat"),
  r = require("./lib/mailbox"),
  p = require("./lib/private-profile"),
  o = require("./lib/reactions"),
  h = require("./lib/invitations"),
  l = require("./lib/devices"),
  c = require("./lib/members"),
  u = require("./lib/config")
class w {
  constructor(i, t, { tx: w, primary: y } = {}) {
    ;((this.base = i),
      (this.bee = t),
      (this.core = t.core),
      (this.isApplyView = !!y),
      (this.internal = new a(this)),
      (this.plugins = new s(this)))
    const b = new e({ view: this, tx: w })
    ;((this.collections = b.createCollections()),
      (this.indexes = b.createIndexes()),
      (this.dex = b.dex),
      (this.chat = new n(this)),
      (this.mailbox = new r(this)),
      (this.privateProfile = new p(this)),
      (this.reactions = new o(this)),
      (this.devices = new l(this)),
      (this.invitations = new h(this)),
      (this.members = new c(this)),
      (this.config = new u(this)),
      this._initialize(),
      (this._primaryView = y),
      (this._applyView = null))
  }
  _initialize() {
    ;(this.internal.initialize(),
      this.plugins.initialize(),
      this.chat.initialize(),
      this.mailbox.initialize(),
      this.privateProfile.initialize(),
      this.reactions.initialize(),
      this.devices.initialize(),
      this.invitations.initialize(),
      this.members.initialize(),
      this.config.initialize())
  }
  _shouldSkipOperation(i) {
    switch (i.type) {
      case 24:
      case 34:
      case 35:
      case 36:
      case 37:
        return !0
      default:
        return !1
    }
  }
  async isFeatureSupported(i) {
    const e = await this.internal.getCodeVersion()
    return t(e, i)
  }
  async isUnsupportedFeature(i) {
    return !(await this.isFeatureSupported(i))
  }
  async applyOperation(i, e) {
    if (!this.isApplyView) throw new Error("Only the apply view should handle operations")
    this._shouldSkipOperation(i) ||
      (a.Operations.has(i.type) && (await this.internal.autobaseApply(i, e)),
      l.Operations.has(i.type) && (await this.devices.autobaseApply(i, e)),
      h.Operations.has(i.type) && (await this.invitations.autobaseApply(i, e)),
      c.Operations.has(i.type) && (await this.members.autobaseApply(i, e)),
      u.Operations.has(i.type) && (await this.config.autobaseApply(i, e)),
      n.Operations.has(i.type) && (await this.chat.autobaseApply(i, e)),
      r.Operations.has(i.type) && (await this.mailbox.autobaseApply(i, e)),
      p.Operations.has(i.type) && (await this.privateProfile.autobaseApply(i, e)),
      o.Operations.has(i.type) && (await this.reactions.autobaseApply(i, e)),
      s.Operations.has(i.type) && (await this.plugins.autobaseApply(i, e)))
  }
  async apply(i) {
    if (this.isApplyView) throw new Error("An apply view cannot be created from an apply view")
    const e = await this.createApplyView()
    await e.plugins.load()
    for (const t of i) {
      const i = { key: t.from.key, seq: t.length - 1 },
        { timestamp: a, ops: s } = t.value,
        n = { id: i, timestamp: a }
      for (const i of s) await e.applyOperation(i, n)
    }
    await e.commit()
  }
  async createApplyView() {
    if (this._applyView) throw new Error("Can only have one apply view active at a time")
    const i = await this.dex.writeTransaction({ update: !1 })
    return (
      (this._applyView = new this.constructor(this.base, i.bee, { tx: i, primary: this })),
      this._applyView
    )
  }
  async commit() {
    if (!this.isApplyView) throw new Error("Can only commit an apply view")
    ;(await this.dex.commit(), (this._primaryView._applyView = null))
  }
  close() {
    return this.bee.close()
  }
}
function y(e, t) {
  const a = e.get("keet"),
    s = new i(a, { alwaysDuplicate: !1, extension: !1 })
  return new w(t, s)
}
function b(i) {
  return i.close()
}
function m(i, e) {
  return e.apply(i)
}
module.exports = { KeetView: w, autobaseApply: m, autobaseOpen: y, autobaseClose: b }