Trouble Creating a Layer with Scope Requirement in Effect Typescript

Hi guys, I am having trouble making my first Layer, I read in the docs, it said the requirements of the return signature of the exposed service methods should be never in most cases, but I am not sure how to move the Scope requirement out of into the Layer.effect for the method addUnloadProtection. This is my code so far. Would be grateful for any help or suggestions how to make this better. Thank you!

import { Context, Effect, Layer, type Scope } from "effect";

export class ApiUtils extends Context.Tag("ApiUtils")<
  ApiUtils,
  {
    readonly addUnloadProtection: Effect.Effect<void, never, Scope.Scope>;
  }
>() {}

export const ApiUtilsLive = Layer.effect(
  ApiUtils,
  Effect.gen(function* () {
    return {
      addUnloadProtection: Effect.gen(function* () {
        let unloadHandler: ((e: BeforeUnloadEvent) => void) | null = null;

        unloadHandler = (e: BeforeUnloadEvent) => {
          e.preventDefault();
        };

        yield* Effect.sync(() => {
          if (unloadHandler) {
            window.addEventListener("beforeunload", unloadHandler);
          }
        });

        yield* Effect.addFinalizer(() =>
          Effect.sync(() => {
            if (unloadHandler) {
              window.removeEventListener("beforeunload", unloadHandler);
              unloadHandler = null;
            }
          }),
        );
      }),
    };
  }),
);
Was this page helpful?