Skip to content

Instantly share code, notes, and snippets.

@MartinMuzatko
Created February 11, 2021 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinMuzatko/ceabd7f9048ac66c08fe0b94705d2d70 to your computer and use it in GitHub Desktop.
Save MartinMuzatko/ceabd7f9048ac66c08fe0b94705d2d70 to your computer and use it in GitHub Desktop.
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