const e = require("ready-resource"),
o = require("safety-catch"),
s = require("../../client/index.js"),
{
api: { hrpc: t },
} = require("../../../schema/spec"),
{ KeetMethod: i } = require("./base"),
n = require("./core"),
h = require("./call"),
r = require("./debug"),
a = require("./error"),
c = require("./network")
class d extends i {
_run() {
return this.backend.coreAPI.boot()
}
}
class u extends i {
async _run() {}
}
module.exports = class extends e {
constructor(e, i, n = {}) {
if (
(super(),
(this.keet = e),
(this.stream = i),
(this.opts = n),
(this.view = new s.View()),
(this.rpc = new t(i)),
(this._apis = null),
this.rpc.onBoot(this.onBoot.bind(this)),
this.rpc.onSelfDestruct(this.onSelfDestruct.bind(this)),
this.stream.on("close", this.close.bind(this)),
!this.keet.opened)
)
throw new Error("Keet must be opened before starting RPC")
this.ready().catch(n.onerror || o)
}
async _open() {
;((this.coreAPI = new n(this)),
(this.callAPI = new h(this)),
(this.debugAPI = new r(this)),
(this.errorAPI = new a(this)),
(this.networkAPI = new c(this)),
(this.apis = [this.coreAPI, this.callAPI, this.debugAPI, this.errorAPI, this.networkAPI]),
this.keet.on("v2:room-changed", this._onRoomChanged.bind(this)),
this.keet.on("v2:room-chat-changed", this._onRoomChatChanged.bind(this)),
this.keet.on("v2:room-stats-changed", this._onRoomStatsChanged.bind(this)),
this.keet.on("v2:room-digest-changed", this._onRoomDigestChanged.bind(this)),
this.keet.on("v2:room-member-changed", this._onRoomMemberChanged.bind(this)),
this.keet.on("v2:room-device-changed", this._onRoomDeviceChanged.bind(this)),
this.keet.on("v2:room-config-changed", this._onRoomConfigChanged.bind(this)),
this.keet.on("v2:room-reactions-changed", this._onRoomReactionsChanged.bind(this)),
this.keet.on("v2:room-join-requests-changed", this._onRoomJoinRequestsChanged.bind(this)),
this.view.members.on("update", this._pushMember.bind(this)),
this.view.devices.on("update", this._pushDevice.bind(this)),
this.view.lobby.on("reset", this._pushLobby.bind(this)),
this.view.identity.on("reset", this._pushIdentity.bind(this)),
this.view.roomInfo.on("update:reset", this._pushRoomInfo.bind(this)),
this.view.roomInfo.on("update:role", this._pushRoomInfoRole.bind(this)),
this.view.roomInfo.on("update:digest", this._pushRoomInfoDigest.bind(this)),
this.view.roomInfo.on("update:config", this._pushRoomInfoConfig.bind(this)),
this.view.roomInfo.on("update:message", this._pushRoomInfoMessage.bind(this)),
this.view.chat.on("reset", this._pushChat.bind(this)),
this.view.messageInfo.on("update:reset", this._pushMessageInfo.bind(this)),
this.view.messageInfo.on("update:role", this._pushMessageInfoRole.bind(this)),
this.view.messageInfo.on("update:member", this._pushMessageInfoMember.bind(this)),
this.view.messageInfo.on("update:chat", this._pushMessageInfoChat.bind(this)),
this.view.messageInfo.on("update:reactions", this._pushMessageInfoReactions.bind(this)),
await Promise.all(this.apis.map((e) => e.ready())))
}
async _close() {
this.apis && (await Promise.all(this.apis.map((e) => e.close())))
}
_onPairingChanged(e) {
this.coreAPI.onPairingChanged(e)
}
_onRoomChanged(e) {
this.coreAPI.onRoomChanged(e)
}
_onRoomStatsChanged(e) {
this.coreAPI.onRoomStatsChanged(e)
}
_onRoomChatChanged(e) {
this.coreAPI.onRoomChatChanged(e)
}
_onRoomDigestChanged(e) {
this.coreAPI.onRoomDigestChanged(e)
}
_onRoomMemberChanged(e) {
this.coreAPI.onRoomMemberChanged(e)
}
_onRoomDeviceChanged(e) {
this.coreAPI.onRoomDeviceChanged(e)
}
_onRoomConfigChanged(e) {
this.coreAPI.onRoomConfigChanged(e)
}
_onRoomReactionsChanged(e) {
this.coreAPI.onRoomReactionsChanged(e)
}
_onRoomJoinRequestsChanged(e) {
this.coreAPI.onRoomJoinRequestsChanged(e)
}
_pushMember(e, o) {
return this.rpc.coreMember(o)
}
_pushDevice(e, o) {
return this.rpc.coreDevice(o)
}
_pushMessage(e, o) {
return this.rpc.coreMessage(o)
}
_pushIdentity() {
return this.rpc.coreIdentityReset(this.view.identity)
}
_pushLobby() {
return this.rpc.coreLobbyReset(this.view.lobby)
}
_pushRoomInfoMessage(e) {
return this.rpc.coreRoomInfoLastMessage({ id: e.id, lastMessage: e.messageId })
}
_pushRoomInfoDigest(e) {
return this.rpc.coreRoomInfoDigest(e)
}
_pushRoomInfoConfig(e) {
return this.rpc.coreRoomInfoConfig(e)
}
_pushRoomInfoRole(e) {
return this.rpc.coreRoomInfoRole(e)
}
_pushRoomInfo(e) {
return this.rpc.coreRoomInfo(e)
}
_pushChat() {
return this.rpc.coreChatReset(this.view.chat)
}
_pushMessageInfo(e) {
return this.rpc.coreMessageInfo(e)
}
_pushMessageInfoChat(e) {
return this.rpc.coreMessageInfoChat(e)
}
_pushMessageInfoMember(e) {
return this.rpc.coreMessageInfoMember(e)
}
_pushMessageInfoRole(e) {
return this.rpc.coreMessageInfoRole(e)
}
_pushMessageInfoReactions(e) {
return this.rpc.coreMessageInfoReactions(e)
}
onBoot() {
return d.run(this)
}
onSelfDestruct() {
return u.run(this)
}
}