Skip to content

Commit

Permalink
add: use single event listener per 0xFableOrg#91
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggingfuture committed Feb 16, 2024
1 parent a980cd7 commit 2f67bb8
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions packages/webapp/src/store/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// =================================================================================================

import _ from "lodash"
import { Log } from "viem"
import { getPublicClient } from "wagmi/actions"

Expand Down Expand Up @@ -75,15 +76,33 @@ export function subscribeToGame(ID: bigint|null) {
}
if (needsSub) {
currentlySubscribedID = ID
eventNames.forEach(eventName => {
unsubFunctions.push(publicClient.watchContractEvent({
address: deployment.Game,
abi: gameABI,
eventName: eventName as any,
args: { gameID: ID },
onLogs: logs => gameEventListener(eventName, logs)
}))
})

const eventsABI = gameABI.filter((abi) => abi.type === "event" && eventNames.includes(abi.name));

/**
*
* Related to https://github.com/0xFableOrg/0xFable/issues/91
*
* viem limitations: watchEvent scoped to multiple events supposedly not also scoped with indexed arguments (args).
* https://viem.sh/docs/actions/public/watchEvent.html#multiple-events
*
* use `watchEvent` (instead of watchContractEvent) which will create filter against gameID
* this is not expected usage of watchEvent and might break when upgrade
* also only works if the args (gameID) is expected across all event topics
*/
unsubFunctions.push(publicClient.watchEvent({
address: deployment.Game,
events: eventsABI,
args: { gameID: ID },
onLogs: logs => {
_.toPairs(_.groupBy(logs, (log:any)=>log.eventName))
.forEach(
([eventName, logs]:[string,any]) => {
gameEventListener(eventName, logs)
}
)
}
}))
}
}

Expand Down

0 comments on commit 2f67bb8

Please sign in to comment.