Created
September 13, 2022 11:17
-
-
Save celsopalmeiraneto/b6672c028a7067d008384b8a7b932beb to your computer and use it in GitHub Desktop.
Our discussion on dependency injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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