Comparing version 0.0.2 to 0.0.3
@@ -5,3 +5,3 @@ { | ||
"description": "Tiny JavaScript for a Big World", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/jymin.js", | ||
@@ -31,4 +31,3 @@ "homepage": "http://jymin.com", | ||
"devDependencies": { | ||
"grunt": "0.4.2", | ||
"uglify-js": "1.3.5" | ||
"uglify-js": "2.4.12" | ||
}, | ||
@@ -35,0 +34,0 @@ "readmeFilename": "README.md", |
@@ -0,6 +1,9 @@ | ||
var SOCKET_IO_PATH = '/socket.io/1/'; | ||
var SOCKET_EMIT_RETRY_COUNT = 5; | ||
var SOCKET_EMIT_RETRY_TIMEOUT = 500; | ||
// Initially, the socket is just a holder for handlers. | ||
var socket = {H: {}}; | ||
var SOCKET_IO_PATH = '/socket.io/1/'; | ||
var socketEmissionId = 0; | ||
var socketEmissionId = 1; | ||
@@ -62,5 +65,38 @@ /** | ||
/** | ||
* Set a new handler for a named event. | ||
*/ | ||
var socketOn = function (name, callback) { | ||
var handlers = socket.H; | ||
var callbacks = handlers[name] = handlers[name] || []; | ||
callbacks.push(callback); | ||
}; | ||
/** | ||
* Trigger handlers for a named event. | ||
*/ | ||
var socketTrigger = function (name, data) { | ||
// Retry-enabled emissions have an emission ID in their responses. | ||
var emissionId = (data || {}).EID; | ||
if (emissionId) { | ||
// The emission time is stored until a response is received. | ||
var emissionTime = socketEmit['E' + emissionId]; | ||
if (emissionTime) { | ||
// Cancel retries. | ||
clearTimeout(socketEmit['T' + emissionId]); | ||
delete socketEmit['E' + emissionId]; | ||
// TODO: Track latencies for adaptive retry and heartbeat delays. | ||
var elapsed = new Date() - emissionTime; | ||
} | ||
// If the emission time is cleared, callbacks have already run. | ||
else { | ||
return; | ||
} | ||
} | ||
// Run all of the handlers that have been bound to this name. | ||
var handlers = socket.H; | ||
@@ -74,15 +110,39 @@ var callbacks = handlers[name] = handlers[name] || []; | ||
/** | ||
* Set a new handler for a named event. | ||
* Emit data over the socket, retrying if necessary | ||
*/ | ||
var socketOn = function (name, callback) { | ||
var handlers = socket.H; | ||
var callbacks = handlers[name] = handlers[name] || []; | ||
callbacks.push(callback); | ||
var socketEmit = function (name, data, retries, onFailure) { | ||
// If this is the first time sending, set everything up. | ||
if (retries === true) { | ||
retries = SOCKET_EMIT_RETRY_COUNT; | ||
// Clone the data so that the EID won't be copied. | ||
data = JSON.parse(JSON.stringify(data)); | ||
data.EID = socketEmissionId++; | ||
// Keep track of the emit time so we can track latency. | ||
socketEmit['E' + data.EID] = new Date(); | ||
} | ||
// Send the data. | ||
socket.send('5:::' + JSON.stringify({ | ||
name: name, | ||
args: [data] | ||
})); | ||
// Set up retry timeouts if necessary. | ||
if (retries) { | ||
socketEmit['T' + data.EID] = setTimeout(function () { | ||
socketEmit(tag, data, retries - 1, onFailure); | ||
}, SOCKET_EMIT_RETRY_TIMEOUT); | ||
} | ||
// If we're out of retries, fail. | ||
else if (retries === 0) { | ||
if (onFailure) { | ||
onFailure(); | ||
} | ||
} | ||
}; | ||
// Set up a new connection. | ||
socketConnect(); | ||
socketOn('refresh', function (changed) { | ||
location.reload(); | ||
}); | ||
socketConnect(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112763
1
29
3783