cb-proxy

1.1.2 • Public • Published

Status

Build Status

Description

Simple JavaScript proxy function factory for the use with libraries like Socket.io.

To import it into your project please input the following code:

var createProxyFunction = require('cb-proxy')
//(...)
var proxyFunction = createProxyFunction(originalFunction, nameIdentifier)

Examples

This library is intended to be used with Socket.io or simillar library, here is an example of usage:

 
var callbackHandler = function(name, args, callback) {
    //do the call server request here eg:
    //client.emit(name, args, function(error, message){
    //    callback(error, message)
    //});
}
 
var proxyFunction = createProxyFunction( callbackHandler, 'remote-function-name') //this name will be passed to callback as first argument
 
var resultPromise = proxyFunction('my first argument', 'my second argument') //both arguments will be passed to callback
 
resultPromise.then((result)=>{
    //do something with result
},(error)=>{
    //or handle error
})

Designed for currying

Reason for having first argument as callback is the possibility to use curry function to bind first argument.

var R = require('ramda')
 
var originalFunction = function(name, args, callback) {}
 
var proxyFunction = R.curry(createProxyFunction)(originalFunction)
 
var myPositionFunction = proxyFunction('position-service')
var myAttackFunction = proxyFunction('attack-service')

Package Sidebar

Install

npm i cb-proxy

Weekly Downloads

2

Version

1.1.2

License

WTFPL

Last publish

Collaborators

  • odrinwhite