mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
PTSD.js
This commit is contained in:
parent
2d3572fe82
commit
bdb61924b5
52
function GUN(){
Normal file
52
function GUN(){
Normal file
@ -0,0 +1,52 @@
|
||||
function GUN(){
|
||||
if(!(this instanceof GUN)){ return new GUN() }
|
||||
}
|
||||
|
||||
GUN.chain = GUN.prototype;
|
||||
|
||||
GUN.chain.get = function(soul){
|
||||
var gun = GUN();
|
||||
gun._ = {soul: soul};
|
||||
gun.back = function(lex, fn, cb){
|
||||
var at = {node: localStorage[lex.soul], lex: gun._};
|
||||
cb(at); fn(at);
|
||||
}
|
||||
return gun;
|
||||
}
|
||||
|
||||
GUN.chain.path = function(field){
|
||||
var gun = GUN(), from = this;
|
||||
gun._ = {field: field};
|
||||
gun.back = function(lex, fn, cb){
|
||||
from.back({field: field, soul: from._.soul}, function(at){
|
||||
at.lex = gun._;
|
||||
cb(at); fn(at);
|
||||
}, cb);
|
||||
}
|
||||
return gun;
|
||||
}
|
||||
|
||||
GUN.chain.put = function(data){
|
||||
var gun = GUN(), from = this, put = {data: data};
|
||||
from.back(gun._ = {}, function(at){
|
||||
if(!at.node){
|
||||
put.root = at.put.root || {};
|
||||
if(at.lex.field){
|
||||
(at.put.node || put.root)[at.lex.field] = put.data;
|
||||
}
|
||||
}
|
||||
localStorage[put.root['#']] = JSON.stringify(put.root);
|
||||
}, function(at){
|
||||
at.put = at.put || {};
|
||||
if(!at.put.root){ at.put.root = at.put.at = {'#': at.lex.soul} }
|
||||
if(at.lex.field){
|
||||
at.put.at = (at.put.node = at.put.at)[at.lex.field] = {};
|
||||
}
|
||||
});
|
||||
return gun;
|
||||
}
|
||||
|
||||
var i = 1, gun = GUN();
|
||||
gun.get('users').path(i).path('where').put({
|
||||
lat: Math.random(), lng: Math.random(), i: i
|
||||
})
|
3919
test/benchmark.js
Normal file
3919
test/benchmark.js
Normal file
File diff suppressed because it is too large
Load Diff
@ -727,7 +727,7 @@ describe('Gun', function(){
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('ify', function(){
|
||||
describe.only('ify', function(){
|
||||
var test, gun = Gun();
|
||||
|
||||
it('null', function(done){
|
||||
@ -810,6 +810,23 @@ describe('Gun', function(){
|
||||
});
|
||||
});
|
||||
|
||||
it('document', function(done){
|
||||
var data = {users: {1: {where: {lat: Math.random(), lng: Math.random(), i: 1}}}};
|
||||
Gun.ify(data, function(err, ctx){
|
||||
var soul, node;
|
||||
expect(soul = Gun.is.rel(ctx.root.users)).to.be.ok();
|
||||
node = ctx.graph[soul];
|
||||
expect(soul = Gun.is.rel(node[1])).to.be.ok();
|
||||
node = ctx.graph[soul];
|
||||
expect(soul = Gun.is.rel(node.where)).to.be.ok();
|
||||
node = ctx.graph[soul];
|
||||
expect(node.lat).to.be.ok();
|
||||
expect(node.lng).to.be.ok();
|
||||
expect(node.i).to.be(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
return; // TODO! Fix GUN to handle this!
|
||||
data = {};
|
||||
data.sneak = false;
|
||||
|
1501
test/perf.js
Normal file
1501
test/perf.js
Normal file
File diff suppressed because it is too large
Load Diff
64
test/stool.css
Normal file
64
test/stool.css
Normal file
@ -0,0 +1,64 @@
|
||||
body {
|
||||
width: 90%;
|
||||
max-width: 800px;
|
||||
margin: 4em auto;
|
||||
font: 12pt Helvetia, Arial, sans-serif;
|
||||
}
|
||||
|
||||
section {
|
||||
margin: 0 0 4em;
|
||||
}
|
||||
|
||||
table {
|
||||
border-spacing: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
th, td {
|
||||
vertical-align: top;
|
||||
padding: 0 0 1em 0;
|
||||
}
|
||||
|
||||
th {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
th.th-result {
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
th.th-remove {
|
||||
width: 0;
|
||||
}
|
||||
|
||||
td.inputs {
|
||||
padding-right: 1em;
|
||||
}
|
||||
|
||||
.remove {
|
||||
position: absolute;
|
||||
margin-left: -6em;
|
||||
width: 5em;
|
||||
}
|
||||
|
||||
input, textarea {
|
||||
margin: 0 0 1em 0;
|
||||
width: 100%;
|
||||
display: block;
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
.result.running {
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
#abort {
|
||||
display: none;
|
||||
}
|
||||
.running #abort {
|
||||
display: initial;
|
||||
}
|
||||
|
||||
.running #run {
|
||||
display: none;
|
||||
}
|
34
test/stool.html
Normal file
34
test/stool.html
Normal file
@ -0,0 +1,34 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>stool</title>
|
||||
<meta name="Description" content="stool: a JavaScript benchmarking utility">
|
||||
<link rel="stylesheet" href="stool.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="inject"></div>
|
||||
<h1>stool</h1>
|
||||
<section>
|
||||
<h3>Setup code</h3>
|
||||
<textarea rows="6" id="common" class="common input" placeholder="Common Code"></textarea>
|
||||
</section>
|
||||
<section>
|
||||
<h3>Test cases</h3>
|
||||
<table id="suite">
|
||||
<tr>
|
||||
<th class="th-remove"></th>
|
||||
<th class="th-case">Case</th>
|
||||
<th class="th-result">Result</th>
|
||||
</tr>
|
||||
</table>
|
||||
<a href="#" id="add">+ Add test case</a>
|
||||
</section>
|
||||
<button id="run">Run!</button>
|
||||
<button id="abort">Abort</button>
|
||||
<button id="share">Share</button>
|
||||
<a id="share-result"></a>
|
||||
<script src="benchmark.js"></script>
|
||||
<script src="stool.js"></script>
|
||||
<script src="../gun.js"></script>
|
||||
<script src="perf.js"></script>
|
||||
</body>
|
||||
</html>
|
196
test/stool.js
Normal file
196
test/stool.js
Normal file
@ -0,0 +1,196 @@
|
||||
(function(){
|
||||
|
||||
var stool = {};
|
||||
var suite;
|
||||
|
||||
function abort() {
|
||||
if (!suite || (suite && !suite.running)) return;
|
||||
|
||||
suite.abort();
|
||||
var results = document.getElementsByClassName('result');
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
if (results[i].classList.contains('running'))
|
||||
results[i].innerText = 'aborted';
|
||||
}
|
||||
return stool;
|
||||
}
|
||||
stool.abort = abort;
|
||||
|
||||
function clear() {
|
||||
if (suite && suite.running) abort();
|
||||
|
||||
var results = document.getElementsByClassName('result');
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
results[i].innerText = '';
|
||||
results[i].classList.remove('running');
|
||||
}
|
||||
return stool;
|
||||
}
|
||||
stool.clear = clear;
|
||||
|
||||
function run() {
|
||||
abort();
|
||||
|
||||
var titles = document.getElementsByClassName('title');
|
||||
var cases = document.getElementsByClassName('case');
|
||||
var results = document.getElementsByClassName('result');
|
||||
var common = document.getElementById('common');
|
||||
|
||||
suite = new Benchmark.Suite;
|
||||
suite.on('complete', function() {
|
||||
console.log(this);
|
||||
document.body.classList.remove('running');
|
||||
});
|
||||
|
||||
document.body.classList.add('running');
|
||||
|
||||
for (var i = 0; i < cases.length; i++) {
|
||||
results[i].classList.add('running');
|
||||
results[i].innerText = 'queued';
|
||||
suite.add(titles[i].value, cases[i].value, {
|
||||
'setup': common.value,
|
||||
'onCycle': (function (result) {
|
||||
return function(event) {
|
||||
if (event.target.aborted) return;
|
||||
result.innerText = Math.round(event.target.hz).toLocaleString() + ' ops/sec';
|
||||
};
|
||||
})(results[i]),
|
||||
'onComplete': (function (result) {
|
||||
return function(event) {
|
||||
if (event.target.aborted) return;
|
||||
result.innerText = Math.round(event.target.hz).toLocaleString() + ' ops/sec';
|
||||
result.classList.remove('running');
|
||||
};
|
||||
})(results[i])
|
||||
});
|
||||
}
|
||||
|
||||
suite.run({ 'async': true });
|
||||
return stool;
|
||||
}
|
||||
stool.run = run;
|
||||
|
||||
function add(title, code) {
|
||||
var html = '<td><button class="remove" tabindex="-1">Remove</button></td><td class="inputs"><input type="text" class="title input" placeholder="Label"><textarea rows="6" class="case input" placeholder="Code"></textarea></td><td class="result"></td>'
|
||||
var s = document.getElementById('suite');
|
||||
var tr = document.createElement('tr');
|
||||
tr.innerHTML = html;
|
||||
tr.getElementsByClassName('remove')[0].onclick = (function (tr) {
|
||||
return function () {
|
||||
tr.remove();
|
||||
clear();
|
||||
};
|
||||
})(tr);
|
||||
|
||||
var inputs = tr.getElementsByClassName('input');
|
||||
for (var i = 0; i < inputs.length; i++) {
|
||||
inputs[i].onkeydown = function () {
|
||||
abort();
|
||||
var shareResult = document.getElementById('share-result');
|
||||
shareResult.href = shareResult.innerText = '';
|
||||
}
|
||||
}
|
||||
if(code){
|
||||
code = stool.text(code);
|
||||
tr.getElementsByClassName('title')[0].value = title;
|
||||
tr.getElementsByClassName('case')[0].value = code;
|
||||
}
|
||||
s.appendChild(tr);
|
||||
|
||||
clear();
|
||||
|
||||
return code? stool : tr;
|
||||
}
|
||||
stool.add = add;
|
||||
|
||||
function setup(code){
|
||||
var common = document.getElementById('common');
|
||||
common.value = stool.text(code);
|
||||
return stool;
|
||||
}
|
||||
stool.setup = setup;
|
||||
|
||||
function share() {
|
||||
var body = {
|
||||
'public': true,
|
||||
'files': {}
|
||||
}
|
||||
|
||||
var titles = document.getElementsByClassName('title');
|
||||
var cases = document.getElementsByClassName('case');
|
||||
var common = document.getElementById('common');
|
||||
|
||||
for (var i = 0; i < cases.length; i++) {
|
||||
if (cases[i].value)
|
||||
body.files[titles[i].value || '__empty' + i] = {'content': cases[i].value};
|
||||
}
|
||||
|
||||
if (common.value)
|
||||
body.files['__common'] = {'content': common.value};
|
||||
|
||||
if (Object.keys(body.files).length == 0)
|
||||
return;
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('post', 'https://api.github.com/gists', true);
|
||||
req.onload = function() {
|
||||
var result = JSON.parse(req.responseText);
|
||||
var shareResult = document.getElementById('share-result');
|
||||
var location = window.location.toString().replace(/#.*$/, '');
|
||||
shareResult.href = shareResult.innerText = location + '#' + result.id;
|
||||
}
|
||||
req.send(JSON.stringify(body));
|
||||
return stool;
|
||||
}
|
||||
stool.share = share;
|
||||
|
||||
function parse(id) {
|
||||
if (!id && !window.location.hash.length) return false;
|
||||
|
||||
id = id || window.location.hash.substring(1);
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('get', 'https://api.github.com/gists/' + id, true);
|
||||
req.onload = function() {
|
||||
var result = JSON.parse(req.responseText);
|
||||
var files = result.files;
|
||||
for (var file in files) {
|
||||
if (file == '__common') {
|
||||
document.getElementById('common').value = files[file].content;
|
||||
} else {
|
||||
var tr = add();
|
||||
tr.getElementsByClassName('title')[0].value = file;
|
||||
tr.getElementsByClassName('case')[0].value = files[file].content;
|
||||
}
|
||||
}
|
||||
}
|
||||
req.send();
|
||||
|
||||
var shareResult = document.getElementById('share-result');
|
||||
shareResult.href = shareResult.innerText = window.location;
|
||||
|
||||
return true;
|
||||
}
|
||||
stool.parse = parse;
|
||||
|
||||
stool.text = function(code){
|
||||
if(typeof code === 'function'){
|
||||
return code.toString().slice(12).slice(0,-1);
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('add').onclick = function() {
|
||||
add();
|
||||
return false;
|
||||
}
|
||||
document.getElementById('run').onclick = run;
|
||||
document.getElementById('abort').onclick = abort;
|
||||
document.getElementById('share').onclick = share;
|
||||
|
||||
if (!parse()) {
|
||||
//add();
|
||||
//add();
|
||||
}
|
||||
window.stool = stool; // export!
|
||||
}());
|
Loading…
x
Reference in New Issue
Block a user