interface TestService {
readonly instance: number;
}
const TestService = Context.Tag<TestService>();
const testServiceImpl = TestService.of({ instance: 1 });
const context = Context.make(TestService, testServiceImpl);
const testEffectWithContext = (data: number[]) =>
TestService.pipe(
Effect.flatMap((testService) => Effect.logInfo(data.length))
);
const testEffectWithoutContext = (data: number[]) => Effect.succeed(data);
const foo = <TData, TRequirements, TError, TValue>(
data: TData[],
f: (items: TData[]) => Effect.Effect<TRequirements, TError, TValue>,
context?: Context.Context<TRequirements>
) =>
pipe(
Effect.if(context !== undefined, {
onFalse: f(data),
onTrue: f(data).pipe(Effect.provideContext(context!)),
}),
Effect.flatMap((fResult) => Effect.succeed(fResult))
);
const testData = [1, 2, 3, 4, 5, 6];
const bar1 = foo(testData, testEffectWithContext, context);
const bar2 = foo(testData, testEffectWithoutContext);
interface TestService {
readonly instance: number;
}
const TestService = Context.Tag<TestService>();
const testServiceImpl = TestService.of({ instance: 1 });
const context = Context.make(TestService, testServiceImpl);
const testEffectWithContext = (data: number[]) =>
TestService.pipe(
Effect.flatMap((testService) => Effect.logInfo(data.length))
);
const testEffectWithoutContext = (data: number[]) => Effect.succeed(data);
const foo = <TData, TRequirements, TError, TValue>(
data: TData[],
f: (items: TData[]) => Effect.Effect<TRequirements, TError, TValue>,
context?: Context.Context<TRequirements>
) =>
pipe(
Effect.if(context !== undefined, {
onFalse: f(data),
onTrue: f(data).pipe(Effect.provideContext(context!)),
}),
Effect.flatMap((fResult) => Effect.succeed(fResult))
);
const testData = [1, 2, 3, 4, 5, 6];
const bar1 = foo(testData, testEffectWithContext, context);
const bar2 = foo(testData, testEffectWithoutContext);