forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBracketsToNodeInterface.js
More file actions
1 lines (1 loc) · 1.83 KB
/
BracketsToNodeInterface.js
File metadata and controls
1 lines (1 loc) · 1.83 KB
1
define(function(require,exports,module){function BracketsToNodeInterface(domain){this.domain=domain,this.bracketsFn={},this._registerDataEvent()}BracketsToNodeInterface.prototype._messageHandler=function(evt,params){var methodName=params.method,self=this;function _getErrorString(err){return"string"==typeof err?err:err&&err.name&&"Error"===err.name?err.message:"Error in executing "+methodName}function _sendResponse(response){var responseParams={requestId:params.requestId,params:response};self.domain.exec("response",responseParams)}function _sendError(err){var responseParams={requestId:params.requestId,error:_getErrorString(err)};self.domain.exec("response",responseParams)}if(self.bracketsFn[methodName]){var method=self.bracketsFn[methodName];try{var response=method.call(null,params.params);params.respond&¶ms.requestId&&(response.promise?response.done(function(result){_sendResponse(result)}).fail(function(err){_sendError(err)}):_sendResponse(response))}catch(err){params.respond&¶ms.requestId&&_sendError(err)}}},BracketsToNodeInterface.prototype._registerDataEvent=function(){this.domain.on("data",this._messageHandler.bind(this))},BracketsToNodeInterface.prototype.createInterface=function(methodName,isAsync){var self=this;return function(params){var execEvent=isAsync?"asyncData":"data",callObject={method:methodName,params:params};return self.domain.exec(execEvent,callObject)}},BracketsToNodeInterface.prototype.registerMethod=function(methodName,methodHandle){methodName&&methodHandle&&"string"==typeof methodName&&"function"==typeof methodHandle&&(this.bracketsFn[methodName]=methodHandle)},BracketsToNodeInterface.prototype.registerMethods=function(methodList){var self=this;methodList.forEach(function(methodObj){self.registerMethod(methodObj.methodName,methodObj.methodHandle)})},exports.BracketsToNodeInterface=BracketsToNodeInterface});