Skip to content

Commit

Permalink
remove ScopeRef
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Feb 14, 2024
1 parent 4a756d3 commit 99f1b49
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as Option from "effect/Option"
import * as Queue from "effect/Queue"
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as Ref from "effect/Ref"
import * as ScopedRef from "effect/ScopedRef"
import * as Stream from "effect/Stream"
import * as SubscriptionRef from "effect/SubscriptionRef"
import {
Expand All @@ -20,6 +19,7 @@ import {
executeCommand,
registerCommand,
} from "./VsCode"
import * as FiberMap from "effect/FiberMap"

export interface Client {
readonly id: number
Expand Down Expand Up @@ -142,7 +142,6 @@ const runServer = Effect.gen(function* (_) {
)
}).pipe(Effect.scoped)

const serverRef = yield* _(ScopedRef.make(() => {}))
const run = server.run(makeClient).pipe(
Effect.catchAllCause(cause =>
SubscriptionRef.update(
Expand All @@ -153,11 +152,16 @@ const runServer = Effect.gen(function* (_) {
Effect.forkScoped,
)

const serverFiber = yield* _(FiberMap.make<"server">())
yield* _(
running.changes,
Stream.runForEach(({ running }) =>
Effect.gen(function* (_) {
yield* _(ScopedRef.set(serverRef, running ? run : Effect.unit))
yield* _(
running
? FiberMap.run(serverFiber, "server", run)
: FiberMap.remove(serverFiber, "server"),
)
yield* _(executeCommand("setContext", "effect:running", running))
}),
),
Expand All @@ -174,11 +178,11 @@ const make = Effect.gen(function* (_) {
SocketServer.SocketServer,
SocketServer.makeWebSocket({ port }),
)
const server = yield* _(ScopedRef.make(() => {}))
const server = yield* _(FiberMap.make<"server">())
yield* _(
port.changes,
Stream.tap(port => SubscriptionRef.update(running, _ => _.setPort(port))),
Stream.runForEach(port => ScopedRef.set(server, makeServer(port))),
Stream.runForEach(port => FiberMap.run(server, "server", makeServer(port))),
Effect.forkScoped,
)

Expand Down

0 comments on commit 99f1b49

Please sign in to comment.