Creating a Service with Initial Data Using `Effect.Service`

I have a service made via
Context.Tag
that allows creation with some initial data. I want to try the same thing with
Effect.Service
. Is this the right way to do that?

import { Effect, Context, MutableHashMap } from 'effect'

type InitialData = [string, number][]
const make = (initial: InitialData = []) => MutableHashMap.make(...initial)
export class MyService extends Context.Tag("MyService2")<MyService, ReturnType<typeof make>>() {
  static readonly Live = (data: InitialData) => MyService.of(make(data))
}

Effect.provideService(MyService, MyService.Live([['a', 1]]))

class MyService2 extends Effect.Service<MyService2>()("MyService2", {
  sync: () => make(), 
  dependencies: [],
}) {
  static readonly Live = (data: InitialData) => MyService2.make(make(data))
}
Was this page helpful?