Created
October 14, 2018 00:12
-
-
Save JustinDFuller/0aea46495d06912acacc1508c3ecbcfe to your computer and use it in GitHub Desktop.
Chain of dependency inversion
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
/* json.js */ | |
export default function ({ readFileAsync, writeFileAsync }) { | |
return { | |
readJsonFile(fileName) { | |
return readFileAsync(`${fileName}.json`).then(JSON.parse) | |
}, | |
writeJsonFile(filePath, fileContent) { | |
return writeFileAsync(filePath, JSON.stringify(fileContent)) | |
} | |
} | |
} | |
/* content.js */ | |
export default function ({ readJsonFile, writeJsonFile }) { | |
return { | |
getContent(contentName) { | |
// business logic goes here. | |
return readJsonFile(contentName) | |
}, | |
writeContent(contentName, contentText) { | |
// business logic goes here | |
return writeJsonFile(contentName, contentText) | |
} | |
} | |
} | |
/* index.js where the app starts */ | |
import fs from 'fs-extra-promise' | |
import jsonInterface from './json' | |
import contentInterface from './content' | |
const json = jsonInterface(fs) | |
const content = contentInterface(json) | |
// content can be used by an http server | |
// or just exported if this is a library | |
export default content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment