fix reads

This commit is contained in:
Mark Nadal
2016-12-26 22:46:27 -08:00
committed by GitHub
parent 096e224af5
commit 2e826ca9d8

View File

@@ -13,6 +13,7 @@ Gun.on('put', function(at){
file.disk.graph[soul] = Graph[soul] || graph[soul];
});
graph = JSON.stringify(file.disk, null, 2);
// TODO: Allow for a `fs.writeFile` compatible module, that is more reliable/safe, to be passed in through the options.
fs.writeFile(opt.file || file.file, graph, function(err){
file.gun.on('in', {
'@': at['#'],
@@ -40,7 +41,7 @@ function field(node, field){
if(!node){ return }
var tmp = node[field];
node = {_: node._};
if(u !== tmp){
if(undefined !== tmp){
node[field] = tmp;
}
tmp = node._;
@@ -62,25 +63,33 @@ Gun.on('opt', function(at){
);
file.use = true;
file.file = String(opt.file || file.file || 'data.json');
file.raw = file.raw || (fs.existsSync || require('path').existsSync)(opt.file) ? fs.readFileSync(opt.file).toString() : null;
file.raw = file.raw || (fs.existsSync || require('path').existsSync)(file.file) ? fs.readFileSync(file.file).toString() : null;
file.disk = file.disk || Gun.obj.ify(file.raw || {graph: {}});
file.disk.graph = file.disk.graph || {};
file.gun = gun;
});
function test(){
var gun = Gun(), i = 2000, expect = 0;
while(--i){
expect += i;
gun.get('test/' + i).put({ index: i });
}
setTimeout(function(){
(function test(){
try{
var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph;
var count = 0;
Gun.obj.map(graph, function(node){
count += node.index;
})
console.log(expect === count? "SUCCESS!" : "FAIL!");
},100);
}
//test();
var read;
Gun().get('test/5').path('index').val(function(data){
read = data;
});
console.log((5 === read)? "READ SUCCESS" : "FAIL");
}catch(e){
var gun = Gun(), i = 100, expect = 0;
while(--i){
expect += i;
gun.get('test/' + i).put({ index: i, test: true });
}
setTimeout(function(){
var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph;
var count = 0;
Gun.obj.map(graph, function(node){
count += node.index;
});
console.log((expect && expect === count)? "WRITE SUCCESS! - RUN AGAIN" : "FAIL!");
},100);
};
}());