Mannish the Manly Mediator
This is a fairly specific implementation of the mediator pattern - given a string name, it calls a function and returns a promise with the function's response.
It accomplishes the same goals as many dependency injection libraries (loose coupling, better testability), but focuses only on functions, and always returning asynchronous responses.
Mannish focuses on decoupling the calling of functions and getting a response back.
mediator = mannish()
This library exports a single function that returns a new 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.
You can return either a plain value, or a promise/thenable.
promise = mediator.call(name, ...arguments)
Call whatever function is registered with the string
name, with all other arguments being passed straight through.
No matter what the other function returns,
call returns a
Promise containing the value it resolves to.
const recipe = type: 'recipe' contents: 'Step 1: find a real cook book'mediator
removeProvider = mediator.provideSync(name, function)
provide, except the function is expected to return a value instead of a promise.
value = mediator.callSync(name, ...arguments)
Calls the synchronous function that was provided via
provideSync, and returns whatever value it returns. Thrown errors are not swallowed.
const grapeJuiceRecipe = type: 'recipe' contents: 'Fresh grapes'mediator // => 'Smushed Fresh grapes'
- Is it ever necessary to remove all providers?