Sanity Check for Effect-ifying Cloudflare Durable Object Storage API with Effect Typescript Library

I am trying to "effect-ify" a cloudflare durable object storage api that looks like this:


storage.transaction(async (txn) => ... )

This is what I have come up with,

import {
  DurableObjectStorage,
  DurableObjectTransaction,
} from "@cloudflare/workers-types";
import { Context, Deferred, Effect, Runtime } from "effect";

class DurableObjectStorageToken extends Context.Tag("DurableObjectStorage")<
  DurableObjectStorageToken,
  DurableObjectStorage
>() {}

class DurableObjectTransactionToken extends Context.Tag(
  "DurableObjectTransaction"
)<DurableObjectTransactionToken, DurableObjectTransaction>() {}

class StorageService extends Effect.Service<StorageService>()(
  "StorageService",
  {
    effect: Effect.gen(function* () {
      const storage = yield* DurableObjectStorageToken;
      const runtime = yield* Effect.runtime();
      const run = Runtime.runPromise(runtime);

      return {
        transaction: <A, E>(
          closure: Effect.Effect<A, E, DurableObjectTransactionToken | never>
        ) =>
          Effect.tryPromise(() =>
            storage.transaction((ctx) => {
              const provided = closure.pipe(
                Effect.provideService(DurableObjectTransactionToken, ctx)
              );
              return run(provided);
            })
          ),
      };
    }),
    dependencies: [],
  }
) {}


Just want to sanity check if this is sensible or not?
Was this page helpful?