Created
April 22, 2018 21:55
-
-
Save dewey92/5ef7bcdb50956662270931c91c3c1974 to your computer and use it in GitHub Desktop.
Reader example (TODO: improve simplicity)
This file contains hidden or 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
| interface User { | |
| name: string | |
| } | |
| const generateId = () => (Math.random() * 10).toString(); | |
| const depsTest = { | |
| env: 'testing', | |
| db: { | |
| selectAll: (tableName: string) => [], | |
| select: (x: { id: string, name: string }) => x, // do nothing | |
| insert: (id: string, user: User) => ({ ...user, id }), | |
| }, | |
| tracking: { | |
| toAnalytics(id: string) { | |
| console.log(id); | |
| return id; | |
| } | |
| }, | |
| generateId | |
| } | |
| type Env = typeof depsTest; | |
| const save = (user: User) => ReaderClass(({ db, generateId }: Env) => | |
| db.select(db.insert(generateId(), user)) | |
| ) // { id: string, name: string } | |
| const track = (userId: string) => ReaderClass(({ tracking }: Env) => tracking.toAnalytics(userId)) | |
| const saveUserToDB = (user: User) => | |
| save(user) // save to db | |
| .flatMap(user => track(user.id)) // after saving, hit Analytics | |
| .map(parseInt) // additional logic | |
| .map(Math.ceil) // additional logic | |
| console.log('YHA ', saveUserToDB({ name: 'Jihad' }).run(depsTest)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment