Skip to content

Commit

Permalink
feat: seperate runtime from core
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 7, 2022
1 parent 52183dc commit 0f3712c
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 129 deletions.
2 changes: 1 addition & 1 deletion src/DiscordGateway/Shard/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const send = (ref: Ref<boolean>, seqRef: Ref<Maybe<number>>) =>

const maybeSend = (ref: Ref<boolean>, seqRef: Ref<Maybe<number>>) =>
ref.get.flatMap(
(acked): Effect<never, never, DWS.Message> =>
(acked): Effect<never, never, DiscordWS.Message> =>
acked ? send(ref, seqRef) : Effect.succeed(WS.Reconnect),
)

Expand Down
13 changes: 8 additions & 5 deletions src/DiscordGateway/Shard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ export const make = (shard: [id: number, count: number]) =>
Do(($) => {
const { token, gateway } = $(Effect.service(Config.DiscordConfig))

const socket = $(DWS.make())
const socket = $(DiscordWS.make())

const [emit, outgoing] = EffectSource.asyncEmitter<never, DWS.Message>()
const limiter = $(Effect.service(RateLimitStore.RateLimiter))
const [emit, outgoing] = EffectSource.asyncEmitter<
never,
DiscordWS.Message
>()
const limiter = $(Effect.service(RateLimit.RateLimiter))
const sendEffect = outgoing
.tap(() => limiter.maybeWait("shard.send", Duration.minutes(1), 120))
.run(socket.sink)

const raw = $(socket.source.share)
const sendMessage = (a: DWS.Message) =>
const sendMessage = (a: DiscordWS.Message) =>
Effect.sync(() => {
emit.data(a)
})
Expand Down Expand Up @@ -81,7 +84,7 @@ export const make = (shard: [id: number, count: number]) =>
.zipPar(heartbeatEffects)
.zipPar(identifyEffects)
.zipPar(invalidEffects)
.zipPar(sendEffect),
.zipPar(sendEffect).asUnit,
raw,
dispatch,
send: (p: Discord.GatewayPayload) => emit.data(p),
Expand Down
2 changes: 1 addition & 1 deletion src/DiscordGateway/Shard/invalidSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const fromRaw = <R, E>(
Discord.GatewayOpcode.INVALID_SESSION,
)
.tap((p) => (p.d ? Effect.unit() : latestReady.set(Maybe.none)))
.map((): DWS.Message => WS.Reconnect)
.map((): DiscordWS.Message => WS.Reconnect)
4 changes: 2 additions & 2 deletions src/DiscordGateway/Sharder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const configs = (totalCount: number) =>
})

const spawnEffect = Effect.structPar({
gateway: Rest.rest
gateway: rest
.getGatewayBot()
.flatMap((r) => r.json)
.catchAll(() =>
Expand All @@ -58,7 +58,7 @@ const spawnEffect = Effect.structPar({
}),
),
config: Config.gateway,
limiter: Effect.service(RateLimitStore.RateLimiter),
limiter: Effect.service(RateLimit.RateLimiter),
})
.bind("configs", ({ gateway, config }) =>
configs(config.shardCount ?? gateway.shards),
Expand Down
4 changes: 2 additions & 2 deletions src/DiscordREST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const make = Do(($) => {
const { token, rest } = $(Effect.service(Config.DiscordConfig))

const log = $(Effect.service(Log.Log))
const store = $(Effect.service(RateLimitStore.RateLimitStore))
const { maybeWait } = $(Effect.service(RateLimitStore.RateLimiter))
const store = $(Effect.service(RateLimit.RateLimitStore))
const { maybeWait } = $(Effect.service(RateLimit.RateLimiter))

const globalRateLimit = maybeWait(
"rest.global",
Expand Down
10 changes: 5 additions & 5 deletions src/Interactions/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const run =
<R, R2, E, E2>(
postHandler: (
effect: Effect<
R | Rest.DiscordREST | Discord.Interaction,
R | DiscordREST | Discord.Interaction,
| E
| Http.FetchError
| Http.StatusCodeError
Expand All @@ -27,10 +27,10 @@ export const run =
splitDefinitions(ix.definitions)

const application = $(
Rest.rest.getCurrentBotApplicationInformation().flatMap((a) => a.json),
rest.getCurrentBotApplicationInformation().flatMap((a) => a.json),
)

const globalSync = Rest.rest.bulkOverwriteGlobalApplicationCommands(
const globalSync = rest.bulkOverwriteGlobalApplicationCommands(
application.id,
{
body: JSON.stringify(GlobalApplicationCommand.map((a) => a.command)),
Expand All @@ -39,7 +39,7 @@ export const run =

const guildSync = GuildApplicationCommand.length
? Gateway.handleDispatch("GUILD_CREATE", (a) =>
Rest.rest.bulkOverwriteGuildApplicationCommands(
rest.bulkOverwriteGuildApplicationCommands(
application.id,
a.id,
GuildApplicationCommand.map((a) => a.command) as any,
Expand All @@ -52,7 +52,7 @@ export const run =
const run = Gateway.handleDispatch("INTERACTION_CREATE", (i) =>
pipe(
handle[i.type](i).tap((r) =>
Rest.rest.createInteractionResponse(i.id, i.token, r),
rest.createInteractionResponse(i.id, i.token, r),
),
postHandler,
Effect.provideService(InteractionContext)(i),
Expand Down
16 changes: 3 additions & 13 deletions src/Interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ export {
InteractionDefinition,
} from "./definitions.js"

export {
makeConfigLayer as makeWebhookConfig,
makeHandler as makeWebhookHandler,
makeSimpleHandler as makeSimpleWebhookHandler,
MakeConfigOpts as MakeWebhookConfigOpts,
WebhookConfig,
WebhookParseError,
BadWebhookSignature,
} from "./webhook.js"

export { response as r } from "../Helpers/interactions.js"

export class InteractionBuilder<R, E> {
Expand All @@ -41,11 +31,11 @@ export class InteractionBuilder<R, E> {
)
.map((c) => c.command)

return Rest.rest
return rest
.getCurrentBotApplicationInformation()
.flatMap((r) => r.json)
.flatMap((app) =>
Rest.rest.bulkOverwriteGlobalApplicationCommands(app.id, {
rest.bulkOverwriteGlobalApplicationCommands(app.id, {
body: JSON.stringify(commands),
}),
)
Expand All @@ -59,7 +49,7 @@ export class InteractionBuilder<R, E> {
)
.map((c) => c.command)

return Rest.rest.bulkOverwriteGuildApplicationCommands(
return rest.bulkOverwriteGuildApplicationCommands(
appId,
guildId,
commands as any,
Expand Down
4 changes: 2 additions & 2 deletions src/RateLimitStore/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ interface Counter {
expires: number
}

export const make = (): RateLimitStore.RateLimitStore => {
const buckets = new Map<string, RateLimitStore.BucketDetails>()
export const make = (): RateLimit.RateLimitStore => {
const buckets = new Map<string, RateLimit.BucketDetails>()
const routes = new Map<string, string>()
const counters = new Map<string, Counter>()

Expand Down
60 changes: 30 additions & 30 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
export type { Effect } from "@effect/io/Effect"
export type { Layer } from "@effect/io/Layer"
export type { Schedule } from "@effect/io/Schedule"
export type { Cause } from "@effect/io/Cause"
export type { Chunk } from "@fp-ts/data/Chunk"
export { Context, Tag } from "@fp-ts/data/Context"
export type { Duration } from "@fp-ts/data/Duration"
export type { Equal } from "@fp-ts/data/Equal"
export type { Option as Maybe } from "@fp-ts/data/Option"
export type { Either } from "@fp-ts/data/Either"
export type { Exit } from "@effect/io/Exit"
export type { EffectSource } from "callbag-effect-ts/Source"
export type { EffectSink } from "callbag-effect-ts/Sink"
export type { HashMap } from "@fp-ts/data/HashMap"
export type { Ref } from "@effect/io/Ref"

export * as Config from "dfx/DiscordConfig/index"
export * as Discord from "dfx/types"
export * as Http from "dfx/Http/index"
export * as Log from "dfx/Log/index"
export * as RateLimitStore from "dfx/RateLimitStore/index"
export * as Rest from "dfx/DiscordREST/index"
export * as Ix from "dfx/Interactions/index"

export * as Flags from "./Helpers/flags.js"
export * as IxHelpers from "./Helpers/interactions.js"
export * as Intents from "./Helpers/intents.js"
export * as Members from "./Helpers/members.js"
export * as Perms from "./Helpers/permissions.js"
export * as UI from "./Helpers/ui.js"
// export type { Effect } from "@effect/io/Effect"
// export type { Layer } from "@effect/io/Layer"
// export type { Schedule } from "@effect/io/Schedule"
// export type { Cause } from "@effect/io/Cause"
// export type { Chunk } from "@fp-ts/data/Chunk"
// export { Context, Tag } from "@fp-ts/data/Context"
// export type { Duration } from "@fp-ts/data/Duration"
// export type { Equal } from "@fp-ts/data/Equal"
// export type { Option as Maybe } from "@fp-ts/data/Option"
// export type { Either } from "@fp-ts/data/Either"
// export type { Exit } from "@effect/io/Exit"
// export type { EffectSource } from "callbag-effect-ts/Source"
// export type { EffectSink } from "callbag-effect-ts/Sink"
// export type { HashMap } from "@fp-ts/data/HashMap"
// export type { Ref } from "@effect/io/Ref"
//
// export * as Config from "dfx/DiscordConfig/index"
// export * as Discord from "dfx/types"
// export * as Http from "dfx/Http/index"
// export * as Log from "dfx/Log/index"
// export * as RateLimitStore from "dfx/RateLimitStore/index"
// export * as Rest from "dfx/DiscordREST/index"
// export * as Ix from "dfx/Interactions/index"
//
// export * as Flags from "./Helpers/flags.js"
// export * as IxHelpers from "./Helpers/interactions.js"
// export * as Intents from "./Helpers/intents.js"
// export * as Members from "./Helpers/members.js"
// export * as Perms from "./Helpers/permissions.js"
// export * as UI from "./Helpers/ui.js"
25 changes: 25 additions & 0 deletions src/gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export * as WS from "./DiscordGateway/WS/index.js"
export * as DiscordWS from "./DiscordGateway/DiscordWS/index.js"
export * as Shard from "./DiscordGateway/Shard/index.js"
export * as ShardStore from "./DiscordGateway/ShardStore/index.js"
export * as Gateway from "./DiscordGateway/index.js"
export { run as runIx } from "./Interactions/gateway.js"

export const LiveRateLimit =
RateLimit.LiveMemoryRateLimitStore > RateLimit.LiveRateLimiter

export const LiveREST = LiveRateLimit > LiveDiscordREST

export const LiveGateway =
ShardStore.LiveMemoryShardStore + DiscordWS.LiveJsonDiscordWSCodec >
Gateway.LiveDiscordGateway

export const LiveBot = LiveREST > LiveGateway

export const make = (config: Config.MakeOpts, debug = false) => {
const LiveLog = debug ? Log.LiveLogDebug : Log.LiveLog
const LiveConfig = Config.makeLayer(config)
const LiveEnv = LiveLog + LiveConfig > LiveBot

return LiveEnv
}
125 changes: 97 additions & 28 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,116 @@
/**
* @tsplus global
*/
import type {
Effect,
Layer,
Either,
Maybe,
Tag,
Context,
Cause,
Chunk,
Duration,
Equal,
Exit,
HashMap,
Schedule,
EffectSource,
EffectSink,
Ref,
Config,
import type { Effect } from "@effect/io/Effect"
export type { Effect } from "@effect/io/Effect"

/**
* @tsplus global
*/
import type { Layer } from "@effect/io/Layer"
export type { Layer } from "@effect/io/Layer"

/**
* @tsplus global
*/
import type { Schedule } from "@effect/io/Schedule"

/**
* @tsplus global
*/
import type { Cause } from "@effect/io/Cause"

/**
* @tsplus global
*/
import type { Chunk } from "@fp-ts/data/Chunk"

/**
* @tsplus global
*/
import { Context, Tag } from "@fp-ts/data/Context"
export { Context, Tag } from "@fp-ts/data/Context"

/**
* @tsplus global
*/
import type { Duration } from "@fp-ts/data/Duration"
export type { Duration } from "@fp-ts/data/Duration"

/**
* @tsplus global
*/
import type { Equal } from "@fp-ts/data/Equal"

/**
* @tsplus global
*/
import type { Option as Maybe } from "@fp-ts/data/Option"
export type { Option as Maybe } from "@fp-ts/data/Option"

/**
* @tsplus global
*/
import type { Either } from "@fp-ts/data/Either"

/**
* @tsplus global
*/
import type { Exit } from "@effect/io/Exit"

/**
* @tsplus global
*/
import type { EffectSource } from "callbag-effect-ts/Source"

/**
* @tsplus global
*/
import type { EffectSink } from "callbag-effect-ts/Sink"

/**
* @tsplus global
*/
import type { HashMap } from "@fp-ts/data/HashMap"

/**
* @tsplus global
*/
import type { Ref } from "@effect/io/Ref"
export type { Ref } from "@effect/io/Ref"

/**
* @tsplus global
*/
import type { HashSet } from "@fp-ts/data/HashSet"

/**
* @tsplus global
*/
import {
RateLimit,
rest,
Discord,
Http,
Log,
RateLimitStore,
Rest,
DiscordREST,
LiveDiscordREST,
Ix,
Flags,
Intents,
Config,
Log,
IxHelpers,
Flags,
Members,
Perms,
UI,
} from "dfx/common"
} from "dfx"

/**
* @tsplus global
*/
import type { HashSet } from "@fp-ts/data/HashSet"
import { Gateway, WS, DiscordWS, Shard, ShardStore } from "dfx/gateway"

/**
* @tsplus global
*/
import type { Gateway, WS, DWS, Shard, ShardStore } from "dfx/common-gateway"
import {} from "dfx/webhooks"

/**
* @tsplus global
Expand Down
Loading

0 comments on commit 0f3712c

Please sign in to comment.