1 /** 2 * Emily.js - http://flams.github.com/emily/ 3 * Copyright(c) 2012-2014 Olivier Scherrer <pode.fr@gmail.com> 4 * MIT Licensed 5 */ 6 "use strict"; 7 8 /** 9 * @class 10 * Transport hides and centralizes the logic behind requests. 11 * It can issue requests to request handlers, which in turn can issue requests 12 * to anything your node.js server has access to (HTTP, FileSystem, SIP...) 13 * @param {Emily Store} [optionanl] $reqHandlers an object containing the request handlers 14 * @returns 15 */ 16 module.exports = function TransportConstructor($reqHandlers) { 17 18 /** 19 * The request handlers 20 * @private 21 */ 22 var _reqHandlers = null; 23 24 /** 25 * Set the requests handlers object 26 * @param {Emily Store} reqHandlers an object containing the requests handlers 27 * @returns 28 */ 29 this.setReqHandlers = function setReqHandlers(reqHandlers) { 30 if (reqHandlers instanceof Object) { 31 _reqHandlers = reqHandlers; 32 return true; 33 } else { 34 return false; 35 } 36 }; 37 38 /** 39 * Get the requests handlers 40 * @returns{ Emily Store} reqHandlers the object containing the requests handlers 41 */ 42 this.getReqHandlers = function getReqHandlers() { 43 return _reqHandlers; 44 }; 45 46 /** 47 * Issue a request to a request handler 48 * @param {String} reqHandler the name of the request handler to issue the request to 49 * @param {Object} data the data, or payload, to send to the request handler 50 * @param {Function} callback the function to execute with the result 51 * @param {Object} scope the scope in which to execute the callback 52 * @returns 53 */ 54 this.request = function request(reqHandler, data, callback, scope) { 55 if (_reqHandlers.has(reqHandler) && 56 typeof data != "undefined") { 57 58 _reqHandlers.get(reqHandler)(data, function () { 59 if (callback) { 60 callback.apply(scope, arguments); 61 } 62 }); 63 return true; 64 } else { 65 return false; 66 } 67 }; 68 69 /** 70 * Issue a request to a reqHandler but keep listening for the response as it can be sent in several chunks 71 * or remain open as long as the abort funciton is not called 72 * @param {String} reqHandler the name of the request handler to issue the request to 73 * @param {Object} data the data, or payload, to send to the request handler 74 * @param {Function} callback the function to execute with the result 75 * @param {Object} scope the scope in which to execute the callback 76 * @returns {Function} the abort function to call to stop listening 77 */ 78 this.listen = function listen(reqHandler, data, callback, scope) { 79 if (_reqHandlers.has(reqHandler) && 80 typeof data != "undefined" && 81 typeof callback == "function") { 82 83 var func = function () { 84 callback.apply(scope, arguments); 85 }, 86 abort; 87 88 abort = _reqHandlers.get(reqHandler)(data, func, func); 89 return function () { 90 if (typeof abort == "function") { 91 abort(); 92 } else if (typeof abort == "object" && typeof abort.func == "function") { 93 abort.func.call(abort.scope); 94 } 95 }; 96 } else { 97 return false; 98 } 99 }; 100 101 this.setReqHandlers($reqHandlers); 102 103 };