Skip to content

Commit

Permalink
feat: InteractionsRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed May 4, 2023
1 parent a4d1b55 commit 7317b8d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Interactions/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Http from "@effect-http/client"
import { DiscordGateway } from "dfx/DiscordGateway"
import { DiscordREST, DiscordRESTError } from "dfx/DiscordREST"
import { DefinitionNotFound, handlers } from "./handlers.js"
import { Interaction, InteractionBuilder } from "./index.js"
import { Interaction, InteractionBuilder, builder } from "./index.js"
import { splitDefinitions } from "./utils.js"

export interface RunOpts {
Expand Down Expand Up @@ -70,3 +70,32 @@ export const run =

return $(sync ? run.zipParRight(globalSync).zipParRight(guildSync) : run)
})

const makeRegistry = Do($ => {
const ref = $(Ref.make(builder))

const register = (ix: InteractionBuilder<never, never>) =>
ref.update(_ => _.concat(ix))

const run = ref.get.flatMap(_ =>
_.runGateway(_ => _.catchAllCause(_ => _.logErrorCause)),
)

return { register, run } as const
})

export interface InteractionsRegistry {
readonly register: (
ix: InteractionBuilder<never, never>,
) => Effect<never, never, void>

readonly run: Effect<
DiscordREST | DiscordGateway,
DiscordRESTError | Http.ResponseDecodeError,
never
>
}

export const InteractionsRegistry = Tag<InteractionsRegistry>()
export const InteractionsRegistryLive =
makeRegistry.toLayer(InteractionsRegistry)
6 changes: 5 additions & 1 deletion src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export * as DiscordWS from "./DiscordGateway/DiscordWS.js"
export * as Shard from "./DiscordGateway/Shard.js"
export * as ShardStore from "./DiscordGateway/ShardStore.js"
export * as WS from "./DiscordGateway/WS.js"
export { run as runIx } from "./Interactions/gateway.js"
export {
InteractionsRegistry,
InteractionsRegistryLive,
run as runIx,
} from "./Interactions/gateway.js"

export const MemoryRateLimit = LiveMemoryRateLimitStore >> LiveRateLimiter

Expand Down

0 comments on commit 7317b8d

Please sign in to comment.