2015-02-10 19:41:24 +00:00
|
|
|
define(function(require, exports, module) {
|
|
|
|
"use strict";
|
|
|
|
|
2015-04-09 08:20:32 +00:00
|
|
|
main.consumes = ["Plugin", "ext", "c9", "vfs"];
|
2015-02-10 19:41:24 +00:00
|
|
|
main.provides = ["vfs.ping"];
|
|
|
|
return main;
|
|
|
|
|
|
|
|
function main(options, imports, register) {
|
|
|
|
var Plugin = imports.Plugin;
|
|
|
|
var c9 = imports.c9;
|
|
|
|
var ext = imports.ext;
|
|
|
|
|
|
|
|
/***** Initialization *****/
|
|
|
|
|
|
|
|
var plugin = new Plugin("Ajax.org", main.consumes);
|
|
|
|
var api;
|
|
|
|
|
|
|
|
var loaded = false;
|
|
|
|
function load(){
|
|
|
|
if (loaded) return;
|
|
|
|
loaded = true;
|
|
|
|
|
|
|
|
ext.loadRemotePlugin("ping", {
|
|
|
|
code: require("text!./ping-service.js"),
|
|
|
|
redefine: true
|
|
|
|
}, function(err, remote) {
|
|
|
|
if (err)
|
|
|
|
return console.error(err);
|
|
|
|
|
|
|
|
api = remote;
|
|
|
|
});
|
|
|
|
|
|
|
|
c9.on("stateChange", function(e) {
|
|
|
|
if (e.state & c9.NETWORK) {
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
loaded = false;
|
|
|
|
api = null;
|
|
|
|
}
|
|
|
|
}, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Lifecycle *****/
|
|
|
|
|
|
|
|
plugin.on("load", function(){
|
|
|
|
load();
|
|
|
|
});
|
|
|
|
|
|
|
|
/***** Register and define API *****/
|
|
|
|
function ping(callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = function(err, result) {
|
|
|
|
if (err)
|
|
|
|
return console.error(err);
|
|
|
|
console.log("ping took", result, "ms");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!api) return callback(new Error("Client is offline"));
|
|
|
|
|
|
|
|
var start = Date.now();
|
2015-02-28 14:03:57 +00:00
|
|
|
api.ping("serverTime", function(err, response) {
|
2015-02-10 19:41:24 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
|
2015-02-28 09:18:45 +00:00
|
|
|
callback(null, {
|
|
|
|
serverTime: response.serverTime,
|
2015-02-28 14:03:57 +00:00
|
|
|
total: Date.now() - start
|
2015-02-28 09:18:45 +00:00
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ping = ping;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
plugin.freezePublicAPI({
|
|
|
|
ping: ping
|
|
|
|
});
|
|
|
|
|
|
|
|
register(null, { "vfs.ping" : plugin });
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|