home *** CD-ROM | disk | FTP | other *** search
- function DigsbyClass() {
- this.callbacks = {};
- }
-
- DigsbyClass.prototype = {
- requestOut: function(method, params, id, success, error, specifier) {
- var self = this;
- //var cmd = new Command();
- //cmd.go = function(){
-
- if (id) {
- //need to store error/success callbacks - if they exist
- self.callbacks[id] = {success:success, error:error};
- }
- // console.log(['requestOut', method, params, id]);
- self.jsonOut({'method':method, 'params':params, 'id':id, 'specifier':specifier});
-
- //};
- //cmd.syncGo();
- },
-
- requestIn: function(args) {
- //var cmd = new Command();
- //cmd.go = function(){
- window[args.method](args.params, args.id);
- //};
- //cmd.syncGo();
- },
-
- resultOut: function(result, error, id) {
- //var cmd = new Command();
- var self = this;
- //cmd.go = function(){
- self.jsonOut({'result':result, 'error':error, 'id':id});
- //};
- //cmd.syncGo();
- },
-
- successOut: function(result, id) {
- this.resultOut(result, null, id);
- },
-
- errorOut: function(error, id) {
- this.resultOut(null, error, id);
- },
-
- jsonOut: function(json_obj) {
- var url = "digsby://digsbyjsonrpc/json=" + encodeURIComponent(JSON.stringify(json_obj));
- document.title = url;
- document.title = "digsby://digsbyjsonrpc/clear";
- },
-
- resultIn: function(res) {
- var self = this;
- //var cmd = new Command();
- //cmd.go = function(){
-
- var result = res.result;
- var error = res.error;
- var id = res.id;
- // console.log('resultIn: ' + id);
- if (result !== null) {
- self.successIn(result, id);
- if (error !== null) {
- console.log("got a result and error:" + result + "," + error + "," + id);
- }
- return;
- }
- if (error !== null) {
- self.errorIn(error, id);
- return;
- }
- console.log("got no result or error:" + id);
-
- //};
- //cmd.syncGo();
- },
-
- errorIn: function(error, id) {
- if (this.callbacks[id]){
- this.callbacks[id].error(error);
- delete this.callbacks[id];
- }
- },
-
- successIn: function(result, id) {
- if (this.callbacks[id]) {
- this.callbacks[id].success(result);
- delete this.callbacks[id];
- }
- }
- };
-
- var Digsby = new DigsbyClass();
-
- function DCallbacks(specifier) {
- this.id_count = 0;
- this.specifier = specifier === undefined ? null : specifier;
- }
-
- function callbackCall(params, id) {
- params = params[0];
- var method = params.method;
- var args = params.args;
-
- function success(result) { Digsby.successOut(result, id); }
- function error(error_obj) { Digsby.errorOut(error_obj, id); }
-
- args.success = success;
- args.error = error;
-
- window[method](args);
- }
-
- //convenience functions
- DCallbacks.prototype = {
-
- notify: function(method, params){
- Digsby.requestOut(method, [params], null, null, null, this.specifier);
- },
-
- rpc: function(method, params, success, error) {
- var id = (new Date()).getTime() + '_' + this.specifier + '_' + this.id_count++;
- //call digsby, wraps w/ an id and puts params in a list
- //also, unwraps the args dict from it's parent list for success
- Digsby.requestOut(method, [params], id, function(args) { success(args[0]); }, error, this.specifier);
- },
-
- ajax: function(args) {
- if (args.type != "JSON")
- console.log("ERROR: Digsby only does JSON");
- else
- this.rpc('ajax', {url:args.url, type:args.type}, args.success, args.error);
- }
- };
-
- var D = new DCallbacks();
-
-