Mannish the Manly Mediator
This is a fairly specific implementation of the mediator pattern - given a string name, it calls a function and returns that function's response.
It accomplishes the same goals as many dependency injection libraries (loose coupling, better testability), but focuses only on functions.
Originally inspired by Nicholas Zakas's talk on "Creating A Scalable JavaScript Application Architecture", this library fills the role of what he called the "sandbox" more closely than it serves the purpose he had for the mediator pattern.
Mannish focuses on decoupling the calling of functions and getting a response back.
API
const mannish = require('mannish')
// or
import * as mannish from 'mannish'
mediator = mannish()
This library exports a single function that returns a new mediator.
const mediator =
removeProvider = mediator.provide(name, function)
Supplies a function to handle all calls to the given name.
removeProvider
is a function that, if you call it, will cause the provider to stop handling calls to that name.
mediator
response = mediator.call(name, ...arguments)
Call whatever function is registered with the string name
, with all other arguments being passed straight through.
call
will return whatever value the provider returns.
const recipe = type: 'recipe' contents: 'Step 1: find a real cook book' mediator // => 'I am a sweet pie'
Usage tips
Some of your providers will probably be asynchronous - I would recommend making your provider function an async function expression so that you don't have to worry about some edge case that causes your ostensibly-promise-returning providers to respond with a synchronous value or error.