Skip to content

Instantly share code, notes, and snippets.

@celsopalmeiraneto
Created September 13, 2022 11:17
Show Gist options
  • Save celsopalmeiraneto/b6672c028a7067d008384b8a7b932beb to your computer and use it in GitHub Desktop.
Save celsopalmeiraneto/b6672c028a7067d008384b8a7b932beb to your computer and use it in GitHub Desktop.
Our discussion on dependency injection
import { SoilAssertion, assertSoilFertilized, assertSoilPloughed } from 'testB';
interface PlantPotatoes {
injected: (seeds: string[], soil: string) => Promise<string>;
dependencies: {
assertPloughed: SoilAssertion;
assertFertilized: SoilAssertion;
};
}
const plantPotatoesInjected = async (
{ assertFertilized, assertPloughed }: PlantPotatoes['dependencies'],
seeds: string[],
soil: string,
): Promise<string> => {
await assertPloughed(soil);
await assertFertilized(soil);
return seeds.reduce((acc, seed) => acc + seed, soil);
};
export const plantPotatoes: PlantPotatoes['injected'] = (seeds, soil) => plantPotatoesInjected(
{
assertFertilized: assertSoilFertilized,
assertPloughed: assertSoilPloughed,
},
seeds,
soil,
);
export const plantPotatoesSkippingChecks: PlantPotatoes['injected'] = (seeds, soil) => plantPotatoesInjected(
{
assertFertilized: () => Promise.resolve(),
assertPloughed: () => Promise.resolve(),
},
seeds,
soil,
);
export interface SoilAssertion {
(soil: string): Promise<void>;
}
export const assertSoilPloughed: SoilAssertion = () => Promise.resolve();
export const assertSoilFertilized: SoilAssertion = () => Promise.resolve();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment