Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed May 4, 2024
1 parent 5b9152f commit 7f27ec1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
49 changes: 26 additions & 23 deletions packages/light-client-effect-bindings/src/smoldot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const start = (
options?: ClientOptions,
): Effect.Effect<Client, never, Scope.Scope> => {
return Effect.gen(function* (_) {
const Client = Brand.nominal<Client>()

const runtime = yield* Effect.runtime<never>()
const runSync = Runtime.runSync(runtime)
const label = "smoldot/client"
Expand All @@ -37,33 +39,47 @@ export const start = (
logCallback: (...args) => logCallback(...args).pipe(runSync),
}),
),
Effect.andThen(make),
Effect.tap(() => Effect.log("started")),
Effect.withLogSpan(label),
)

const release = (client: Client) =>
const terminate = (client: smoldot.Client) =>
Effect.tryPromise({
try: () => client.terminate(),
catch: (err) => {
if (err instanceof smoldot.AlreadyDestroyedError) {
return new AlreadyDestroyedError()
}
if (err instanceof smoldot.CrashError) {
return new CrashError()
}

return new Cause.UnknownException(err)
},
})

const release = (client: smoldot.Client) =>
Effect.gen(function* (_) {
yield* Effect.addFinalizer(() =>
client._terminate.pipe(Effect.catchAll(Effect.logError)),
terminate(client).pipe(Effect.catchAll(Effect.logError)),
)
yield* Effect.log("terminating")
}).pipe(
Effect.tap(() => Effect.log("terminated")),
Effect.withLogSpan(label),
)

const client = yield* Effect.acquireRelease(acquire, release)
const innerClient = yield* Effect.acquireRelease(acquire, release)
const realClient = Client(yield* make(innerClient))

return client
return realClient
})
}

/** @internal */
const make = (client: smoldot.Client) => {
return Effect.gen(function* (_) {
const scope = yield* Effect.scope
const Client = Brand.nominal<Client>()
const ChainRefId = Brand.nominal<ChainRefId>()
const nextChainRefId = yield* Ref.make<ChainRefId>(ChainRefId(0))
const chainsMap = yield* SynchronizedRef.make(
Expand Down Expand Up @@ -146,24 +162,11 @@ const make = (client: smoldot.Client) => {
Scope.extend(scope),
)

const terminate = Effect.tryPromise({
try: () => client.terminate(),
catch: (err) => {
if (err instanceof smoldot.AlreadyDestroyedError) {
return new AlreadyDestroyedError()
}
if (err instanceof smoldot.CrashError) {
return new CrashError()
}

return new Cause.UnknownException(err)
},
})

return Client({
return {
nextChainRefId,
chainsMap,
addChain,
_terminate: terminate,
})
}
})
}

Expand Down
23 changes: 15 additions & 8 deletions packages/light-client-effect-bindings/src/smoldot/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import * as smoldot from "smoldot"

import { Brand, Cause, Effect, Scope } from "effect"
import {
Brand,
Cause,
Effect,
HashMap,
Ref,
Scope,
SynchronizedRef,
} from "effect"
import {
AddChainError,
AlreadyDestroyedError,
Expand All @@ -17,6 +25,12 @@ export type AddChainOptions = Readonly<

export type Client = Brand.Brand<"Client"> &
Readonly<{
/** @internal */
nextChainRefId: Ref.Ref<ChainRefId>
/** @internal */
chainsMap: SynchronizedRef.SynchronizedRef<
HashMap.HashMap<ChainRefId, smoldot.Chain>
>
addChain(
options: AddChainOptions,
): Effect.Effect<
Expand All @@ -27,13 +41,6 @@ export type Client = Brand.Brand<"Client"> &
| Cause.UnknownException,
Scope.Scope
>

/** @internal */
_terminate: Effect.Effect<
void,
Cause.UnknownException | AlreadyDestroyedError | CrashError,
never
>
}>
export type ClientOptions = Readonly<Omit<smoldot.ClientOptions, "logCallback">>

Expand Down

0 comments on commit 7f27ec1

Please sign in to comment.