mannish
Mannish, the Manly Mediator
Last updated 2 years ago by tehshrike .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mannish 
SYNC missed versions from official npm registry.

Mannish the Manly Mediator

Adventure Time!

changelog

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 = mannish()

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.provide('pie', recipe => {
	// whatever you return is provided to the caller
	return 'I am a sweet pie'
})

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.call('pie', recipe) // => '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.

License

WTFPL

Current Tags

  • 4.0.1                                ...           latest (2 years ago)

12 Versions

  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.0                                ...           4 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.4                                ...           5 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |