//services/weatherApi.ts
export const WeatherApiLive = Layer.effect(
WeatherService,
Effect.gen(function* () {
return {
getWeather: (city: string) =>
pipe(
getCityCoords(city),
Effect.flatMap(({ lat, lon }) =>
pipe(fetchWeatherData(city, lat, lon)),
),
Effect.provide(FetchHttpClient.layer),
),
};
}),
);
//index.ts
const program = Effect.gen(function* () {
const weatherApi = yield* WeatherService;
const updateStream = Stream.repeat(
weatherApi.getWeather("Calgary"),
Schedule.fixed("2 seconds"),
);
yield* Stream.runForEach(updateStream, (weather) =>
Console.log(
`Weather update: ${weather.city} - ${weather.temperature}, ${weather.condition}`,
),
);
});
const main = pipe(
program,
Effect.provide(WeatherApiLive),
Effect.provide(NodeHttpClient.layer),
Effect.catchAllCause(Effect.logError),
);
Effect.runPromise(main).catch(console.error);
//services/weatherApi.ts
export const WeatherApiLive = Layer.effect(
WeatherService,
Effect.gen(function* () {
return {
getWeather: (city: string) =>
pipe(
getCityCoords(city),
Effect.flatMap(({ lat, lon }) =>
pipe(fetchWeatherData(city, lat, lon)),
),
Effect.provide(FetchHttpClient.layer),
),
};
}),
);
//index.ts
const program = Effect.gen(function* () {
const weatherApi = yield* WeatherService;
const updateStream = Stream.repeat(
weatherApi.getWeather("Calgary"),
Schedule.fixed("2 seconds"),
);
yield* Stream.runForEach(updateStream, (weather) =>
Console.log(
`Weather update: ${weather.city} - ${weather.temperature}, ${weather.condition}`,
),
);
});
const main = pipe(
program,
Effect.provide(WeatherApiLive),
Effect.provide(NodeHttpClient.layer),
Effect.catchAllCause(Effect.logError),
);
Effect.runPromise(main).catch(console.error);