Files
panic-server/lib/patch.js
Jesse Gibson be781b5d8d Tether to UI, upgrade to gun@0.3, organize files, clean code, capture errors, add progress events, better type checking for patch.js, more polyfills, more reliable checking for finish events, server improvements
You can now boot up tests from the homepage using the UI. It's powered by jQuery.

The tests are now using the latest, greatest version of gun, v0.3.

Files now organized into folder "lib" for clarity and cleanliness.

Numerous code cleanliness improvements.

Now, if errors are passed to the acknowledgement listener, they're pushed to an array of errors on that request.

Now each time there's a response,  the statistics are recalculated and are passed to an optional progress callback.

Patch.js now has stronger type checking against the data it's been given.

Added more polyfills to aid in development, like wrapping console.log in a closure to bypass the console interface error.

The timeout accuracy and code clarity has been improved.

Server is now upgraded to use gun@0.3, and the level options have been better tailored to the application.
2016-01-07 14:20:10 -07:00

110 lines
2.1 KiB
JavaScript

/*globals Gun, type, console*/
/*
Return an object like this:
return {
expect: Object,
done: Function,
progress: Function,
interval: Number,
peers: Array,
packets: Number,
id: String,
key: String,
path: String,
packet: Function
};
*/
var patch;
(function () {
'use strict';
var random = Gun.text.random;
function is(data, expected) {
var string, error, actual = type(data);
if (actual !== expected) {
string = JSON.stringify(data);
error = 'Expected ' + expected + ', was ' + actual + ': ' + string;
throw new TypeError(error);
}
return true;
}
function or(obj, prop, val) {
return (obj[prop] = obj[prop] || val);
}
function done(opt) {
or(opt, 'done', {});
or(opt.done, 'cb', function () {
console.log('No finishing callback');
});
or(opt.done, 'timeout', 2000);
is(opt.done, 'object');
is(opt.done.timeout, 'number');
is(opt.done.cb, 'function');
}
function packet(opt) {
// wrap the packet function
// set time and id for
// every returned object
opt.packet = (function () {
var makePacket = opt.packet || function () {
return random();
};
return function () {
return {
data: makePacket(),
time: Gun.time.is(),
id: opt.id
};
};
}());
is(opt.packet(), 'object');
}
patch = function (opt) {
var url = location.protocol + '//';
url += location.host;
url += '/gun';
opt = opt || {};
opt.interval = opt.interval || 0;
opt.peers = opt.peers || [url];
opt.packets = opt.packets || 50;
opt.id = opt.id || random(10);
opt.key = opt.key || random(15);
opt.path = opt.path || random(5);
opt.requests = [];
opt.requests.start = Gun.time.is();
or(opt, 'progress', function (opt, num) {
console.log('Saved', num);
});
done(opt);
packet(opt);
is(opt.key, 'string');
is(opt.interval, 'number');
is(opt.peers, 'array');
is(opt.packets, 'number');
is(opt.id, 'string');
is(opt.path, 'string');
is(opt.progress, 'function');
if (opt.packets < 0) {
throw new RangeError("Negative packets? Are you crazy?");
}
return opt;
};
}());