OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / keet-core-4.14.4-pretty / lib / api / v2 / base.js

59 lines · javascript ref:
const t = require("ready-resource")
class s extends t {
  constructor(t) {
    ;(super(),
      (this.keet = t),
      (this.room = null),
      (this.dbSnapshot = null),
      (this.blobSnapshot = null),
      (this.localSnapshot = null))
  }
  async _close() {
    ;(await Promise.all([
      this.dbSnapshot ? this.dbSnapshot.close() : null,
      this.blobSnapshot ? this.blobSnapshot.close() : null,
      this.localSnapshot ? this.localSnapshot.close() : null,
    ]),
      this.room && this.keet.internalReleaseRoom(this.room))
  }
  setRoom(t) {
    return ((this.room = this.keet.internalOpenExistingRoom(t)), this.room)
  }
  snapshot() {
    if (!this.room) throw new Error("Must set a room before taking a db snapshot")
    return (this.dbSnapshot || (this.dbSnapshot = this.room.db.snapshot()), this.dbSnapshot)
  }
  blobSnapshot() {
    if (!this.room) throw new Error("Must set a room before taking a db snapshot")
    return this.blobSnapshot
      ? this.dbSnapshot
      : ((this.blobSnapshot = this.room.blobs.snapshot()), this.blobSnapshot)
  }
  localSnapshot() {
    return (
      this.localSnapshot || (this.localSnapshot = this.keet.local.snapshot()),
      this.localSnapshot
    )
  }
}
class o {
  constructor(t, o) {
    ;((this.backend = t), (this.keet = t.keet), (this.req = o), (this.ctx = new s(this.keet)))
  }
  async _run() {}
  async run() {
    this.backend.opened || (await this.backend.ready())
    try {
      const t = Date.now(),
        s = await this._run()
      return { header: { duration: Date.now() - t }, ...s }
    } finally {
      await this.ctx.close()
    }
  }
  static run(t, s) {
    return new this(t, s).run()
  }
}
module.exports = { KeetMethod: o }