Giter Site home page Giter Site logo

mannish's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.