Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #235 from nateevans/master
Browse files Browse the repository at this point in the history
Set function context in easyXDM.Rpc
  • Loading branch information
oyvindkinsey committed Feb 10, 2018
2 parents 6f274d5 + fbb3022 commit a32ef15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build.number
src/.*
*.prefs
build.secret.properties
src/tests/easyXDM.debug.js
src/tests/easyXDM.debug.js
.idea
8 changes: 5 additions & 3 deletions src/Rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* Creates a proxy object that can be used to call methods implemented on the remote end of the channel, and also to provide the implementation
* of methods to be called from the remote end.<br/>
* The instantiated object will have methods matching those specified in <code>config.remote</code>.<br/>
* This requires the JSON object present in the document, either natively, using json.org's json2 or as a wrapper around library spesific methods.
* This requires the JSON object present in the document, either natively, using json.org's json2 or as a wrapper around library specific methods.
* <h2>How to set up</h2>
* <pre><code>
* var rpc = new easyXDM.Rpc({
Expand Down Expand Up @@ -102,7 +102,7 @@
* &nbsp; alert("error: " + message + );
* });
* </code></pre>
* Both the <code>success</code> and <code>errror</code> callbacks are optional.<br/>
* Both the <code>success</code> and <code>error</code> callbacks are optional.<br/>
* When called with no callback a JSON-RPC 2.0 notification will be executed.
* Be aware that you will not be notified of any errors with this method.
* <br/>
Expand Down Expand Up @@ -158,7 +158,9 @@ easyXDM.Rpc = function(config, jsonRpcConfig){

// set the origin
this.origin = getLocation(config.remote);


// set context for this object's local rpc functions
this.context = config.context || null;

/**
* Initiates the destruction of the stack.
Expand Down
23 changes: 12 additions & 11 deletions src/stack/RpcBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
// #endif
var pub, serializer = config.serializer || getJSON();
var _callbackCounter = 0, _callbacks = {};

/**
* Serializes and sends the message
* @private
Expand All @@ -52,17 +52,17 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
data.jsonrpc = "2.0";
pub.down.outgoing(serializer.stringify(data));
}

/**
* Creates a method that implements the given definition
* @private
* @param {Object} The method configuration
* @param {Object} definition The method configuration
* @param {String} method The name of the method
* @return {Function} A stub capable of proxying the requested method call
*/
function _createMethod(definition, method){
var slice = Array.prototype.slice;

// #ifdef debug
trace("creating method " + method);
// #endif
Expand All @@ -73,7 +73,7 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
var l = arguments.length, callback, message = {
method: method
};

if (l > 0 && typeof arguments[l - 1] === "function") {
//with callback, procedure
if (l > 1 && typeof arguments[l - 2] === "function") {
Expand Down Expand Up @@ -105,13 +105,13 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
_send(message);
};
}

/**
* Executes the exposed method
* @private
* @param {String} method The name of the method
* @param {Number} id The callback id to use
* @param {Function} method The exposed implementation
* @param {Function} fn The exposed implementation
* @param {Array} params The parameters supplied by the remote end
*/
function _executeMethod(method, id, fn, params){
Expand All @@ -130,7 +130,7 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
}
return;
}

// #ifdef debug
trace("requested to execute procedure " + method);
// #endif
Expand Down Expand Up @@ -166,16 +166,17 @@ easyXDM.stack.RpcBehavior = function(proxy, config){
params = [params];
}
try {
var result = fn.method.apply(fn.scope, params.concat([success, error]));
var context = proxy.context || fn.scope;
var result = fn.method.apply(context, params.concat([success, error]));
if (!undef(result)) {
success(result);
}
}
}
catch (ex1) {
error(ex1.message);
}
}

return (pub = {
incoming: function(message, origin){
var data = serializer.parse(message);
Expand Down

0 comments on commit a32ef15

Please sign in to comment.