Effect CommunityEC
Effect Community8mo ago
5 replies
Janosch

Refreshing Config Layer on File Change Using fs Watch Effect

With this, is it possible to "refresh" this layer when the file changes? fs has a watch Effect but I am not sure how to replace the config on-the-fly so the next time Config is yielded, it has fresh data?
import * as Yaml from 'yaml';

const ConfigProviderLive = Layer.unwrapEffect(
  Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const configProvider = yield* pipe(
      fs.readFileString('/path/to/config.yaml'),
      Effect.flatMap((content) =>
        Effect.try({
          try: () => Yaml.parse(content),
          catch: (e) => `Could not parse config file : ${e}`,
        }),
      ),
      Effect.map((data) => ConfigProvider.fromJson(data)),
    );
    return Layer.setConfigProvider(configProvider);
  }),
);
Was this page helpful?