Issue with Optional Service Always Using Default Value
I am trying to use optional services but I'm failing.
I haave it setup like this:
Then I try to use it like this:
But it always reaches the default value branch
I haave it setup like this:
export class CheckSchedule extends Context.Tag('CheckSchedule')<CheckSchedule, Schedule.Schedule<number>>() {}
export class ReminderService extends Effect.Service<ReminderService>()('ReminderService', {
dependencies: [DbClient.Live],
accessors: true,
scoped: Effect.gen(function*(_) {
// Setup requirements
const events = yield* PubSub.unbounded<SelectReminder>();
const client = yield* DbClient;
const checkSchedule = yield* pipe(
Effect.serviceOptional(CheckSchedule),
Effect.catchTag('NoSuchElementException', () => {
console.info('No check schedule provided, using default');
return Effect.succeed(Schedule.spaced('1 minute'));
}),
);
// ...
}) {
static provideSchedule = (input: DurationInput) => Effect.provideService(CheckSchedule, Schedule.spaced(input));
}export class CheckSchedule extends Context.Tag('CheckSchedule')<CheckSchedule, Schedule.Schedule<number>>() {}
export class ReminderService extends Effect.Service<ReminderService>()('ReminderService', {
dependencies: [DbClient.Live],
accessors: true,
scoped: Effect.gen(function*(_) {
// Setup requirements
const events = yield* PubSub.unbounded<SelectReminder>();
const client = yield* DbClient;
const checkSchedule = yield* pipe(
Effect.serviceOptional(CheckSchedule),
Effect.catchTag('NoSuchElementException', () => {
console.info('No check schedule provided, using default');
return Effect.succeed(Schedule.spaced('1 minute'));
}),
);
// ...
}) {
static provideSchedule = (input: DurationInput) => Effect.provideService(CheckSchedule, Schedule.spaced(input));
}Then I try to use it like this:
const program = Effect.gen(function*() {
const { events, createReminder, setReminderAsSent } = yield* ReminderService;
yield* Stream.fromPubSub(events).pipe(
Stream.runForEach((x) =>
Effect.gen(function*() {
yield* setReminderAsSent(x.id);
})
),
Effect.fork,
);
yield* createReminder({
deadline: new Date(),
message: '....',
source: '....',
});
});
BotRuntime.runFork(program.pipe(
ReminderService.provideSchedule('10 seconds'),
));const program = Effect.gen(function*() {
const { events, createReminder, setReminderAsSent } = yield* ReminderService;
yield* Stream.fromPubSub(events).pipe(
Stream.runForEach((x) =>
Effect.gen(function*() {
yield* setReminderAsSent(x.id);
})
),
Effect.fork,
);
yield* createReminder({
deadline: new Date(),
message: '....',
source: '....',
});
});
BotRuntime.runFork(program.pipe(
ReminderService.provideSchedule('10 seconds'),
));But it always reaches the default value branch
