Skip to content

Commit

Permalink
convert label watcher to layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kristojorg committed Jan 14, 2025
1 parent ecc8908 commit 50047cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
35 changes: 15 additions & 20 deletions src/LabelWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { AtpAgent } from "@/AtpAgent"
import { Env } from "@/Environment"
import { decodeFirst } from "@atcute/cbor"
import { Context, Data, Effect, Layer, Schema, Stream } from "effect"
import { Data, Effect, Layer, Schema, Stream } from "effect"
import { Cursor } from "./Cursor"
import { RetryingSocket } from "./RetryingSocket"
import { MessageLabels, parseSubscribeLabelsMessage } from "./schema"

const handleMessageError = (e: unknown) =>
Effect.gen(function*() {
yield* Effect.logError(e)
return Effect.succeed(undefined)
return yield* Effect.succeed(undefined).pipe(Effect.asVoid)
})

const makeRun = Effect.gen(function*() {
const run = Effect.gen(function*() {
const connect = yield* RetryingSocket
const agent = yield* AtpAgent
const cursor = yield* Cursor
Expand All @@ -31,31 +31,26 @@ const makeRun = Effect.gen(function*() {
)

// run the stream and cursor concurrently
const run = Effect.all(
const start = Effect.all(
{
stream: runStream,
cursor: cursor.start,
},
{ concurrency: 2 },
)
return { run }
).pipe(Effect.asVoid)
return yield* start
})

interface ILabelWatcher {
run: Effect.Effect<void>
}
const LabelWatcherDeps = Layer.mergeAll(
RetryingSocket.Default,
AtpAgent.Logging,
Cursor.Default,
Env.Default,
)

export class LabelWatcher extends Context.Tag("LabelWatcher")<
LabelWatcher,
ILabelWatcher
>() {
static Default = Layer.effect(LabelWatcher, makeRun).pipe(
Layer.provide(RetryingSocket.Default),
Layer.provide(AtpAgent.Logging),
Layer.provide(Cursor.Default),
Layer.provide(Env.Default),
)
}
export const LabelWatcherLive = Layer.scopedDiscard(run).pipe(
Layer.provide(LabelWatcherDeps),
)

/**
* Handle each by adding or removing users from lists. Note we do not
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiLive } from "@/HttpApi"
import { LabelWatcher } from "@/LabelWatcher"
import { LabelWatcherLive } from "@/LabelWatcher"
import { LoggerLive } from "@/logger"
import { Layer } from "effect"
import "dotenv/config"
Expand Down Expand Up @@ -28,7 +28,7 @@ import { BunRuntime } from "@effect/platform-bun"
*/

export const MainLiveLayer = Layer.mergeAll(
LabelWatcher.Default,
LabelWatcherLive,
ApiLive,
).pipe(
Layer.provide(LoggerLive),
Expand Down

0 comments on commit 50047cc

Please sign in to comment.