manage-calls.js
function(obj, method_name, force_call_after = 1000){

		if(typeof obj[method_name] != 'function') throw new TypeError("collateCalls(): Not a method '"+method_name+ "' on "+obj.toString())

		var original_fn		= obj[method_name],
			scheduled_runs 	= {}

		function requestRun(...args){
			var str		=  	JSON.stringify(args)


			if(!scheduled_runs[str] || !scheduled_runs[str].isPending()){
				scheduled_runs[str] = Promise.resolve( original_fn.apply(obj, args) )
				setTimeout( () => scheduled_runs[str] = undefined, force_call_after)
			}

			return scheduled_runs[str]
		}

		function restore(){
			obj[method_name] = original_fn
		}

		obj[method_name] 			= requestRun
		obj[method_name].restore 	= restore
	}
function(obj, method_name, force_call_after = 1000){

		if(typeof obj[method_name] != 'function') throw new TypeError("collateCalls(): Not a method '"+method_name+ "' on "+obj.toString())

		var original_fn		= obj[method_name],
			scheduled_runs 	= {}

		function requestRun(...args){
			var str		=  	JSON.stringify(args)


			if(!scheduled_runs[str] || !scheduled_runs[str].isPending()){
				scheduled_runs[str] = Promise.resolve( original_fn.apply(obj, args) )
				setTimeout( () => scheduled_runs[str] = undefined, force_call_after)
			}

			return scheduled_runs[str]
		}

		function restore(){
			obj[method_name] = original_fn
		}

		obj[method_name] 			= requestRun
		obj[method_name].restore 	= restore
	}
function ( obj , method_name , force_call_after = 1000 ) { if ( typeof obj [ method_name ] != 'function' ) throw new TypeError ( "collateCalls(): Not a method '" + method_name + "' on " + obj . toString ( ) ) var original_fn = obj [ method_name ] , scheduled_runs = { } function requestRun ( ... args ) { var str = JSON . stringify ( args ) if ( ! scheduled_runs [ str ] || ! scheduled_runs [ str ] . isPending ( ) ) { scheduled_runs [ str ] = Promise . resolve ( original_fn . apply ( obj , args ) ) setTimeout ( ( ) => scheduled_runs [ str ] = undefined , force_call_after ) } return scheduled_runs [ str ] } function restore ( ) { obj [ method_name ] = original_fn } obj [ method_name ] = requestRun obj [ method_name ] . restore = restore }
Replaces a method of provided object with a proxy,
that will prevent multiple calls to the original method with the same arguments
while waiting for a response.

The proxy method will allways wrap the original method in a Promise.

If a previous call with the same arguments is still pending the proxy will return the corresponding promise.

This may help to prevent multiple redudant API calls.
(e.g. when you dont expect the result of an API call to change while it's pending)

Two sets of arguments are considered equal if their JSON representaions are equal.

The orginial method can be restored with obj[method_name].restore().

@memberof	module:docloop

@param  {Object}  obj					Target object
@param  {string}  method_name			Name of the method whose calls should be callated
@param  {Number}	[force_call_after=1000] After this time (in milliseconds) the proxy will call the original method, regardless of previous calls still pending.

@throws {TypeError} 						If method_name does not point to a function.

@return {}								undefined
Replaces a method of provided object with a proxy that will prevent multiple calls to the original method with the same arguments while waiting for a response .