Skip to content

Commit

Permalink
add KeyValueStore impl for ShardStateStore
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 29, 2024
1 parent b1c54e9 commit 87d6215
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
69 changes: 45 additions & 24 deletions src/DiscordGateway/Shard/StateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,66 @@ import * as Effect from "effect/Effect"
import * as Option from "effect/Option"
import * as Context from "effect/Context"
import * as Layer from "effect/Layer"
import * as KVS from "@effect/platform/KeyValueStore"

export interface ShardState {
readonly resumeUrl: string
readonly sequence: number | null
readonly sessionId: string
}

export interface ShardStateStore {
readonly _: unique symbol
}

export interface StateStore {
readonly get: Effect.Effect<Option.Option<ShardState>>
readonly set: (state: ShardState) => Effect.Effect<void>
readonly clear: Effect.Effect<void>
}

export const ShardStateStore = Context.GenericTag<
export class ShardStateStore extends Context.Tag("dfx/Shard/StateStore")<
ShardStateStore,
{ readonly forShard: (id: [id: number, count: number]) => StateStore }
>("dfx/Shard/StateStore")

export const MemoryShardStateStoreLive = Layer.sync(ShardStateStore, () => {
const store = new Map<string, ShardState>()
>() {
static MemoryLive: Layer.Layer<ShardStateStore> = Layer.sync(
ShardStateStore,
() => {
const store = new Map<string, ShardState>()

return ShardStateStore.of({
forShard: ([id, count]) => {
const key = `${id}-${count}`
return {
get: Effect.sync(() => Option.fromNullable(store.get(key))),
set(state) {
return Effect.sync(() => {
store.set(key, state)
})
return ShardStateStore.of({
forShard: ([id, count]) => {
const key = `${id}-${count}`
return {
get: Effect.sync(() => Option.fromNullable(store.get(key))),
set(state) {
return Effect.sync(() => {
store.set(key, state)
})
},
clear: Effect.sync(() => {
store.delete(key)
}),
}
},
clear: Effect.sync(() => {
store.delete(key)
}),
}
})
},
})
})
)

static KVSLive: Layer.Layer<ShardStateStore, never, KVS.KeyValueStore> =
Layer.effect(
ShardStateStore,
Effect.gen(function* (_) {
const store = yield* KVS.KeyValueStore
return ShardStateStore.of({
forShard([id, count]) {
const key = `dfx-shard-state-${id}-${count}`
return {
get: Effect.map(
Effect.orDie(store.get(key)),
Option.map(JSON.parse),
),
set: state => Effect.orDie(store.set(key, JSON.stringify(state))),
clear: Effect.orDie(store.remove(key)),
}
},
})
}),
)
}
4 changes: 2 additions & 2 deletions src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DiscordGatewayLive } from "dfx/DiscordGateway"
import * as DiscordWS from "dfx/DiscordGateway/DiscordWS"
import { JsonDiscordWSCodecLive } from "dfx/DiscordGateway/DiscordWS"
import * as Shard from "dfx/DiscordGateway/Shard"
import { MemoryShardStateStoreLive } from "dfx/DiscordGateway/Shard/StateStore"
import { ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore"
import * as SendEvent from "dfx/DiscordGateway/Shard/sendEvents"
import * as ShardStore from "dfx/DiscordGateway/ShardStore"
import { MemoryShardStoreLive } from "dfx/DiscordGateway/ShardStore"
Expand Down Expand Up @@ -32,7 +32,7 @@ export const DiscordLive = Layer.mergeAll(
Layer.provide(JsonDiscordWSCodecLive),
Layer.provide(MemoryRateLimitStoreLive),
Layer.provide(MemoryShardStoreLive),
Layer.provide(MemoryShardStateStoreLive),
Layer.provide(ShardStateStore.MemoryLive),
)

export const DiscordIxLive = InteractionsRegistryLive.pipe(
Expand Down

0 comments on commit 87d6215

Please sign in to comment.