Extending a Service with Effect Typescript Library

Hey, I was wondering how extending a Service would look like?

e.g. if i have a SharedFooService and a SpecificFooService i would like the SpecificFooService to build on top of SharedFooService and also provide all the methods that SharedFooService provides.

Here is my solution - but I was wondering if there might be any issues with this approach or if there is actually some built in method to do that?

Thanks for your help

export class SomeDep extends Effect.Service<SomeDep>()("SomeDep", {
  sync: () => ({
    doSomething: () => console.log("I am doing something"),
  }),
}) {}

export class SharedFooService extends Effect.Service<SharedFooService>()(
  "shared/FooService",
  {
    accessors: true,
    dependencies: [SomeDep.Default],

    effect: Effect.gen(function* () {
      const someDep = yield* SomeDep;

      return {
        foo: someDep.doSomething,
      };
    }),
  },
) {}

export class SpecificFooService extends Effect.Service<SpecificFooService>()(
  "specific/FooService",
  {
    accessors: true,
    dependencies: [SharedFooService.Default],

    effect: Effect.gen(function* () {
      const { _tag, ...sharedFooService } = yield* SharedFooService;

      return {
        ...sharedFooService,
        bar: () => console.log("Doing bar"),
      };
    }),
  },
) {}
Was this page helpful?