Created
February 11, 2021 15:26
-
-
Save MartinMuzatko/ceabd7f9048ac66c08fe0b94705d2d70 to your computer and use it in GitHub Desktop.
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 * as fs from 'fs/promises' | |
interface Habit { | |
name: string | |
process: string[] | |
} | |
interface HabitsPageDependencies { | |
load: (amount: number) => Promise<Habit[]> | |
} | |
const loadByFs = (amount: number): Promise<Habit[]> => | |
fs.readFile('my-habits.json') | |
.then(f => f.toString()) | |
.then(JSON.parse) | |
.then((h: Habit[]) => h.slice(0, amount)) | |
const HabitsPage = (dependencies: HabitsPageDependencies) => ({ | |
getAllHabits: () => dependencies.load(5000) | |
}) | |
const slowHabitsPage = HabitsPage({ | |
load: loadByFs | |
}) | |
slowHabitsPage.getAllHabits() // always loads file | |
import * as R from 'ramda' | |
const memoizedHabitsPage = HabitsPage({ | |
load: R.memoizeWith((amount: number) => amount + '', loadByFs) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment