Skip to content

Commit

Permalink
refactor: explicitly run things in main
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 25, 2023
1 parent 6ef75ef commit a873e13
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/AutoThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ const make = Effect.gen(function* ($) {

const ix = Ix.builder.add(archive).add(edit).add(editModal)

// Run the message handler
yield* $(Effect.forkScoped(handleMessages))

return { ix } as const
return { ix, run: handleMessages } as const
})

export interface AutoThreads extends Effect.Effect.Success<typeof make> {}
Expand Down
5 changes: 1 addition & 4 deletions src/ChannelsCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ interface ChannelsCache
extends Effect.Effect.Success<typeof makeChannelsCache> {}

export const ChannelsCache = Tag<ChannelsCache>()
export const ChannelsCacheLive = Layer.scoped(
ChannelsCache,
Effect.tap(makeChannelsCache, _ => Effect.forkScoped(_.run)),
)
export const ChannelsCacheLive = Layer.scoped(ChannelsCache, makeChannelsCache)
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { Config, Effect, Layer, pipe } from "bot/_common"
import { Intents, Ix } from "dfx"
import { makeLive, runIx } from "dfx/gateway"
import * as Dotenv from "dotenv"
import { ChannelsCache, ChannelsCacheLive } from "./ChannelsCache.js"

Dotenv.config()

const program = Effect.gen(function* ($) {
const channels = yield* $(ChannelsCache)
const autoThreads = yield* $(AutoThreads)

const runInteractions = pipe(
Ix.builder.concat(autoThreads.ix),
runIx(Effect.catchAllCause(Effect.logErrorCause)),
)

yield* $(runInteractions)
yield* $(Effect.allParDiscard(channels.run, autoThreads.run, runInteractions))
})

const BotLive = makeLive({
Expand All @@ -24,7 +26,10 @@ const BotLive = makeLive({
},
})

const EnvLive = Layer.provideMerge(BotLive, AutoThreadsLive)
const EnvLive = Layer.provideMerge(
BotLive,
Layer.merge(AutoThreadsLive, ChannelsCacheLive),
)

pipe(
program,
Expand Down

0 comments on commit a873e13

Please sign in to comment.