Work for callbacks in Javascript Bridge.

Provide a generic mechanism for allowing an instance method to be
called in a static context, allowing the bridge to be given a string
naming a function, which it can call in a global context.
This commit is contained in:
Jonathon Wilson 2012-12-08 10:12:00 -07:00
parent 9ea9d54139
commit b4fcd0fdee
7 changed files with 81 additions and 8 deletions

View File

@ -0,0 +1,15 @@
(function(context) {
"use strict";
context.JK = context.JK || {};
context.JK.Callbacks = {
makeStatic: function(staticName, instanceFunction, thisObject) {
context.JK.Callbacks[staticName] = function() {
var _this = thisObject || context.JK.Callback;
return instanceFunction.apply(_this, arguments);
};
}
};
})(window);

View File

@ -7,12 +7,13 @@
var logger = context.JK.logger;
logger.info("*** Fake JamClient instance initialized. ***");
function testLatency(client, callback) {
function testLatency(client, callbackFunctionName) {
var response = {
id: client.id,
latency: 50
};
callback(response);
var js = callbackFunctionName + "(" + response + ");";
eval(js);
}
function joinSession(sessionId) {}

View File

@ -15,7 +15,6 @@
}
function afterShow(data) {
// Move joinSession function to this file for ease of finding it?
context.JK.joinMusicSession(sessionId, app);
$.ajax({

View File

@ -54,7 +54,8 @@
sessionPingsOut[session.id] = 0;
}
sessionPingsOut[session.id]++;
jamClient.TestLatency(client, clientPingResponse);
var jsFunction = "JK.Callbacks.clientPingResponse";
jamClient.TestLatency(client, jsFunction);
});
}
@ -112,6 +113,11 @@
this.sessionInfo = sessionInfo;
this.getSortScore = getSortScore;
this.subscribe = subscribe;
this.clientPingResponse = clientPingResponse;
// Register clientPingResponse on this instance as a static function
// so that the JavascriptBridge native code can invoke it with a string.
context.JK.Callbacks.makeStatic("clientPingResponse", this.clientPingResponse, this);
return this;
};

View File

@ -0,0 +1,48 @@
(function(context, $) {
describe("Callbacks", function() {
describe("makeStatic", function() {
it("should create static function which invokes instance function", function() {
var MyObj = function() {
this.hey = function() { return "hey"; };
};
var myInst = new MyObj();
context.JK.Callbacks.makeStatic("callHey", myInst.hey);
var result = context.JK.Callbacks.callHey();
expect(result).toEqual("hey");
});
});
describe("invocation", function() {
it("should pass arguments", function() {
var MyObj = function() {
this.addTwo = function(input) { return input+2; };
};
var myInst = new MyObj();
context.JK.Callbacks.makeStatic("addTwo", myInst.addTwo);
var result = context.JK.Callbacks.addTwo(5);
expect(result).toEqual(7);
});
});
describe("context", function() {
it("should set 'this' to provided context", function() {
var MyObj = function() {
this.counter = 0;
this.addToCounter = function(incAmount) {
this.counter += incAmount;
};
};
var myInst = new MyObj();
context.JK.Callbacks.makeStatic("incInstanceCounter", myInst.addToCounter, myInst);
context.JK.Callbacks.incInstanceCounter(2);
context.JK.Callbacks.incInstanceCounter(3);
expect(myInst.counter).toEqual(5);
});
});
});
})(window, jQuery);

View File

@ -12,8 +12,9 @@
};
var jamClientFake = {
TestLatency: function(client, cb) {
cb({id: client.id, latency: 50});
TestLatency: function(client, fnName) {
var js = fnName + '(' + JSON.stringify({id: client.id, latency: 50}) + ');';
eval(js);
}
};

View File

@ -33,8 +33,11 @@
beforeEach(function() {
callCount = 0;
sessionLatency = new context.JK.SessionLatency(fakeJamClient);
spyOn(fakeJamClient, "TestLatency").andCallFake(function(client, callback) {
callback(testLatencyResponses[client.id]);
spyOn(fakeJamClient, "TestLatency").andCallFake(function(client, callbackName) {
var js = callbackName + '(' + JSON.stringify(testLatencyResponses[client.id]) + ');';
eval(js);
//callback(testLatencyResponses[client.id]);
callCount++;
});
});