Skip to content

Commit

Permalink
Handle Disconnects Gracefully in SpellCast
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTocs committed Jun 24, 2024
1 parent a12e5df commit 736966b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
38 changes: 30 additions & 8 deletions libs/castmate-core/src/pubsub/pubsub-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const PubSubManager = Service(
private onMessage = new EventList<(plugin: string, event: string, context: object) => any>()

private onConnect = new EventList()
private onBeforeDisconnect = new EventList()

constructor() {}

Expand All @@ -44,22 +45,25 @@ export const PubSubManager = Service(
}

start() {
logger.log("Starting PubSub Connection")
this.shouldConnect = true
this.connect()
}

stop() {
logger.log("Stopping PubSub Connection")
this.shouldConnect = false
this.disconnect()
}

private disconnect() {
private async disconnect() {
if (this.azSocket) {
try {
await this.onBeforeDisconnect.run()
} catch {}

const socket = this.azSocket
logger.log("Disconnecting from Cloud PubSub")
this.azSocket?.stop()
this.azSocket = undefined
socket?.stop()
}

this.connected = false
Expand Down Expand Up @@ -136,12 +140,9 @@ export const PubSubManager = Service(
})

this.azSocket.on("disconnected", (ev) => {
logger.error("Lost Connection to CastMate Pubsub", ev.message)

//ON DISCONNECT

this.connected = false
this.connecting = false
logger.error("Lost Connection to CastMate Pubsub", ev.message)
})

await this.azSocket.start()
Expand All @@ -161,6 +162,7 @@ export const PubSubManager = Service(
}

try {
//logger.log("Sending", eventName, this.connected, this.connecting)
await this.azSocket.sendEvent(eventName, data, "json")
} catch (err) {
logger.error("Cloud PubSub Error", err)
Expand Down Expand Up @@ -194,6 +196,14 @@ export const PubSubManager = Service(
unregisterOnConnect(func: () => any) {
this.onConnect.unregister(func)
}

registerOnBeforeDisconnect(func: () => any) {
this.onBeforeDisconnect.register(func)
}

unregisterOnBeforeDisconnect(func: () => any) {
this.onBeforeDisconnect.unregister(func)
}
}
)

Expand Down Expand Up @@ -229,11 +239,13 @@ export function onCloudPubSubMessage<T extends object>(
if (activeFunc()) {
if (!registered) {
registered = true
//logger.log("Registering", pluginId, eventName)
PubSubManager.getInstance().registerOnMessage(handler)
}
} else {
if (registered) {
registered = false
//logger.log("Unregistering", pluginId, eventName)
PubSubManager.getInstance().unregisterOnMessage(handler)
}
}
Expand All @@ -259,6 +271,16 @@ export function onCloudPubSubConnect(func: () => any) {
})
}

export function onCloudPubSubBeforeDisconnect(func: () => any) {
onLoad(() => {
PubSubManager.getInstance().registerOnBeforeDisconnect(func)
})

onUnload(() => {
PubSubManager.getInstance().unregisterOnBeforeDisconnect(func)
})
}

export function useSendCloudPubSubMessage<T extends object>(eventName: string) {
if (!initingPlugin) throw new Error()
const pluginId = initingPlugin.id
Expand Down
5 changes: 5 additions & 0 deletions plugins/spellcast/main/src/spell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
writeYAML,
ProfileManager,
onCloudPubSubConnect,
onCloudPubSubBeforeDisconnect,
} from "castmate-core"
import {
SpellConfig,
Expand Down Expand Up @@ -411,6 +412,10 @@ export function setupSpells() {
await updateActiveSpells()
})

onCloudPubSubBeforeDisconnect(async () => {
await setActiveSpells({ spells: [] })
})

onProfilesChanged(async (activeProfiles, inactiveProfiles) => {
await updateActiveSpells()
})
Expand Down

0 comments on commit 736966b

Please sign in to comment.