Skip to content
Browse files

adding rpc

  • Loading branch information...
1 parent 2ef5c90 commit 7d9016ebb9336198b1ff5d7a97fb2f83ed281962 Pinhook committed
Showing with 214 additions and 0 deletions.
  1. +1 −0 rpc/fixtures/Event.findAll.json
  2. +1 −0 rpc/fixtures/Event.findOne.json
  3. +22 −0 rpc/qunit.html
  4. +148 −0 rpc/rpc.js
  5. +42 −0 rpc/test/qunit/rpc_test.js
View
1 rpc/fixtures/Event.findAll.json
@@ -0,0 +1 @@
+[{"id": 1},{"id": 2}]
View
1 rpc/fixtures/Event.findOne.json
@@ -0,0 +1 @@
+{"id": 1}
View
22 rpc/qunit.html
@@ -0,0 +1,22 @@
+<html>
+ <head>
+ <link rel="stylesheet" type="text/css" href="../../funcunit/qunit/qunit.css" />
+ <title>QUnit Test</title>
+ <style>
+ body {
+ margin: 0px; padding: 0px;
+ }
+ </style>
+ <script type='text/javascript' src='../../steal/steal.js?mxutil/rpc/test/qunit/rpc_test.js'></script>
+ </head>
+ <body>
+
+ <h1 id="qunit-header">rpchub Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <div id="test-content"></div>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-test-area"></div>
+ </body>
+</html>
View
148 rpc/rpc.js
@@ -0,0 +1,148 @@
+steal.plugins('jquery','jquery/lang/json').then(function($){
+
+ var waiting = [],
+ timeout,
+ id = 0;
+ /**
+ * Makes a rpc batch request
+ * @param {String} method The JSON RPC method: ex "Task.get"
+ * @param {Object} params The params passed to the function. Can be an object or an arry.
+ * @param {Function} success The success handler callback
+ * @param {Function} error The error handler callback
+ * @param {String} fixture A fixture to use
+ */
+ $.rpc = function(method, params, success, error, fixture){
+ //if no url
+ if(typeof method !== 'string'){
+ fixture = error;
+ error = success;
+ success = params;
+ params = method;
+ method = url;
+ }
+ //if no params
+ if(typeof params == 'function'){
+ fixture = error;
+ error = success;
+ success = params;
+ params = {};
+ }
+
+ var request = {
+ method: method,
+ params: params,
+ success: success,
+ fixture: fixture,
+ error: error
+ }
+
+ waiting.push(request);
+ if(!timeout){
+ timeout = setTimeout(function(){
+ //clear waiting and move to sent
+ clearTimeout(timeout);
+ timeout = null;
+ var sent = waiting.slice(0), //the waiting
+ batch =[],
+ fixtures = [],
+ results = new Array(sent.length);
+ waiting = [];
+
+
+ for(var i=0; i < sent.length;i++ ){
+ var request = sent[i],
+ rpc = {
+ "jsonrpc" : "2.0",
+ "method" : request.method,
+ "params" : request.params,
+ "id": (request.id = id++)
+ }
+ request.order = i;
+ if(request.fixture && $.fixture){
+ //rpc.fixture = ;
+ fixtures.push(request)
+ }else
+ batch.push(rpc)
+ }
+ //we have to get each fixture ...
+ //lets get our fixtures ...
+ var getNextFixture,
+ getBatch,
+ runResults = function(){
+ $.rpc.numberOfRequests ++;
+ for(var i=0; i < results.length;i++){
+ if(results[i].error && sent[i].error){
+ sent[i].error(results[i].error)
+ }else{
+ sent[i].success(results[i].result)
+ }
+ }
+
+ },
+ processFixtureAjax = function(data){
+ //find the first empty fixture slot
+ for (var i = 0; i < results.length; i++) {
+ if(sent[i].fixture && !results[i]){
+ results[i] = {"jsonrpc" : "2.0", result: data, id: sent[i].id};
+ break;
+ }
+ }
+ if(fixtures.length){
+ getNextFixture();
+ }else{
+ getBatch();
+ }
+ }
+ getNextFixture =function(){
+ var settings = $.extend({},fixtures.shift());;
+ settings.success= processFixtureAjax;
+ settings.dataType = 'json'
+ if($.fixture["-handleFunction"](settings)){
+ return;
+ }else{
+ var url = settings.fixture;
+ if(/^\/\//.test(url)){
+ url = steal.root.join(settings.fixture.substr(2))
+ }
+ if(console.log){
+ console.log("Getting ",settings.method," from ", url)
+ }
+ $.get(url,null,processFixtureAjax,"json" )
+ }
+ }
+ getBatch = function(){
+ if(batch.length){
+ $.post($.rpc.url,$.toJSON(batch),function(data){
+ //start filling in results
+ for (var i = 0; i < results.length; i++) {
+ if(!results[i]){
+ results[i] = data.shift();
+ }
+ }
+ runResults()
+ });
+ }else{
+ runResults()
+ }
+
+
+ }
+
+ if(fixtures.length){
+ getNextFixture();
+ }else{
+ getBatch();
+ }
+ },$.rpc.timeout)
+ }
+ }
+
+
+
+ $.extend($.rpc,{
+ timeout : 20, //a blink of the eye
+ url: "/rpc",
+ numberOfRequests : 0 //for testsing
+ })
+
+})
View
42 rpc/test/qunit/rpc_test.js
@@ -0,0 +1,42 @@
+steal.plugins("funcunit/qunit",'jquery/dom/fixture','mxutil/rpc').then(function(){
+
+ module("rpchub")
+ test("rpchub works", function(){
+ stop(3000)
+ $.rpc("Event.findAll",{thing: 5},function(events){
+
+ ok(events.length,"we got events")
+ setTimeout(function(){start()},1000) //lets do the next test in a second
+ }, null,
+ "//mxutil/rpc/fixtures/Event.findAll.json")
+ setTimeout(function(){
+ $.rpc("Event.findOne",{thing: 5},function(event){
+ ok(event.id,"only 1 thing");
+ equals($.rpc.numberOfRequests,1, "right number of requests made");
+ }, null,
+ "//mxutil/rpc/fixtures/Event.findOne.json")
+ },3)
+
+
+ })
+
+ test("rpchub works twice", function(){
+ stop(1000)
+ $.rpc("Event.findAll",{thing: 5},function(events){
+ start();
+ ok(events.length,"we got events")
+
+ }, null,
+ "//mxutil/rpc/fixtures/Event.findAll.json")
+ setTimeout(function(){
+ $.rpc("Event.findOne",{thing: 5},function(event){
+ ok(event.id,"only 1 thing");
+ equals($.rpc.numberOfRequests,2,"right number of requests");
+ }, null,
+ "//mxutil/rpc/fixtures/Event.findOne.json")
+ },3)
+
+
+ })
+
+});

0 comments on commit 7d9016e

Please sign in to comment.
Something went wrong with that request. Please try again.