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
getMessages.js
)
definition (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
$init
method is guaranteed to be executed aftermadul
has fully loaded, but before it's available for use; so you know that thedb
dependency will be properly setup and connected to asusername
sdk
is a collection of helpful functions. The iterable sdk wraps the async library.sdk
is easily configurable/customizable per madul.
getMessagesFromAda.js
)
usage (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
username
used for connecting to thedb
tobootstrap
- We don't call
$init
directly; that's handled for us as part ofbootstrap
- We don't pass the
db
dependency togetMessagesFrom
; that's handled for us main
is necessary here because Node.js doesn't support top levelawait
in non-ES modules