-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error support for InteractionBuilder
- Loading branch information
Tim Smart
committed
Dec 3, 2022
1 parent
77a6acb
commit 3747f96
Showing
3 changed files
with
85 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,56 @@ | ||
import { filter } from "callbag-effect-ts/Source" | ||
import { spawn } from "./Sharder/index.js" | ||
|
||
export const makeFromDispatch = | ||
export const fromDispatchFactory = | ||
<R, E>( | ||
source: EffectSource<R, E, Discord.GatewayPayload<Discord.ReceiveEvent>>, | ||
) => | ||
<K extends keyof Discord.ReceiveEvents>( | ||
event: K, | ||
): EffectSource<R, E, Discord.ReceiveEvents[K]> => | ||
pipe( | ||
source, | ||
filter((p) => p.t === event), | ||
).map((p) => p.d! as any) | ||
source.filter((p) => p.t === event).map((p) => p.d! as any) | ||
|
||
export const handleDispatchFactory = | ||
<R, E>( | ||
source: EffectSource<R, E, Discord.GatewayPayload<Discord.ReceiveEvent>>, | ||
) => | ||
<K extends keyof Discord.ReceiveEvents, R1, E1, A>( | ||
event: K, | ||
handle: (event: Discord.ReceiveEvents[K]) => Effect<R1, E1, A>, | ||
): Effect<R | R1, E | E1, void> => | ||
source | ||
.filter((p) => p.t === event) | ||
.map((p) => p.d! as Discord.ReceiveEvents[K]) | ||
.chainPar((a) => EffectSource.fromEffect(handle(a))).runDrain | ||
|
||
export const make = Do(($) => { | ||
const shards = $(spawn.share) | ||
const raw = $(shards.chainPar((s) => s.raw).share) | ||
const dispatch = $(shards.chainPar((s) => s.dispatch).share) | ||
const fromDispatch = makeFromDispatch(dispatch) | ||
const fromDispatch = fromDispatchFactory(dispatch) | ||
const handleDispatch = handleDispatchFactory(dispatch) | ||
|
||
return { | ||
shards, | ||
raw, | ||
dispatch, | ||
fromDispatch, | ||
handleDispatch, | ||
} | ||
}) | ||
|
||
export interface DiscordGateway extends Success<typeof make> {} | ||
export const DiscordGateway = Tag<DiscordGateway>() | ||
export const LiveDiscordGateway = Layer.fromEffect(DiscordGateway)(make) | ||
|
||
export const handleDispatch = < | ||
K extends keyof Discord.ReceiveEvents, | ||
R1, | ||
E1, | ||
A, | ||
>( | ||
event: K, | ||
handle: (event: Discord.ReceiveEvents[K]) => Effect<R1, E1, A>, | ||
) => | ||
Effect.serviceWithEffect(DiscordGateway)((a) => | ||
a.handleDispatch(event, handle), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters