README
Mädūl
Madul is a simple set of tools that help you craft clean async code that scales ridiculously well, is fun to write & maintain, and is super simple to instrument
Super-quick overview
definition (getMessages.js)
const madul = {
deps: ['/db'],
$init: async ({ db, username }) =>
await db.connectAs({ username })
,
getMessagesFrom: async ({ friend, sentBefore, db }) => {
const allMessages = await db.getAllMessagesBefore({ timestamp: sentBefore })
const fromMyFriend = await db.getMessagesFrom({ friend })
const messagesFromMyFriend = sdk.
iterable(allMessages).
filter(m => fromMyFriend.includes(m))
return messagesFromMyFriend
}
}
In the above code:
- A madul is just an object literal
- The
db(a file local to the project, denoted by the/) dependency is loaded asynchronously - Dependencies are passed as named parameters to methods
- The
$initmethod is guaranteed to be executed aftermadulhas fully loaded, but before it's available for use; so you know that thedbdependency will be properly setup and connected to asusername sdkis a collection of helpful functions. The iterable sdk wraps the async library.sdkis easily configurable/customizable per madul.
usage (getMessagesFromAda.js)
const bootstrap = require('@bsgbryan/madul/bootstrap')
const main = async () => {
const messageGetter = await bootstrap('/getMessages', { username: 'KatherineJohnson' })
const oneHour = 1000 * 60 * 60
const sentBefore = Date.now() - oneHour
const messages = await messageGetter.getMessagesFrom({ friend: 'Ada', sentBefore })
console.log('My messages from Ada!', messages)
}
main()
In the above code:
- We pass the
usernameused for connecting to thedbtobootstrap - We don't call
$initdirectly; that's handled for us as part ofbootstrap - We don't pass the
dbdependency togetMessagesFrom; that's handled for us mainis necessary here because Node.js doesn't support top levelawaitin non-ES modules