Skip to content

Commit

Permalink
set?
Browse files Browse the repository at this point in the history
  • Loading branch information
amark committed Jun 19, 2015
1 parent b3fbf01 commit ec8bdad
Show file tree
Hide file tree
Showing 20 changed files with 11,696 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/*
npm-debug.log
*data.json
*.db
33 changes: 26 additions & 7 deletions gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
if(!data){
if(ctx.soul){ return }
cb.call(gun, null, null);
return gun._.at('null').emit();
return gun._.at('null').emit({key: ctx.key});
}
if(ctx.soul = Gun.is.soul.on(data)){
gun._.at('soul').emit({soul: ctx.soul});
Expand All @@ -314,7 +314,7 @@
} else {
console.Log("Warning! You have no persistence layer to get from!");
cb.call(gun, null, null); // Technically no error, but no way we can get data.
gun._.at('null').emit();
gun._.at('null').emit({key: ctx.key});
}
}
return gun;
Expand Down Expand Up @@ -601,15 +601,34 @@

return gun;
}
Chain.chain.set = function(val, cb, opt){ // TODO: NEEDS TESTS!!!!!! WARNING: Do we keep?
var gun = this, ctx = {}, drift = Gun.time.now();
cb = cb || function(){};
opt = opt || {};

if(!gun.back){ gun = gun.put({}) }
gun = gun.not(function(next, key){
return key? this.put({}).key(key) : this.put({});
});
if(!val && !Gun.is.value(val)){ return gun }

var obj = {};
obj['I' + drift + 'R' + Gun.text.random(5)] = val;
return gun.put(obj, cb);
}
Chain.not = function(cb){
var gun = this, ctx = {};
cb = cb || function(){};

gun._.at('null').once(function(){
var chain = gun.chain(), next = cb.call(chain);
next._.at('soul').event(function($){ gun._.on('soul').emit($) });
next._.at('node').event(function($){ gun._.on('node').emit($) });
chain._.on('soul').emit({soul: Gun.roulette.call(chain), empty: true});
gun._.at('null').once(function($){
function kick(next){
if(++c){ return Gun.log("Warning! Multiple `not` resumes!"); }
next._.at('soul').event(function($){ gun._.on('soul').emit($) });
next._.at('node').event(function($){ gun._.on('node').emit($) });
}
var $ = $ || {}, chain = gun.chain(), next = cb.call(chain, kick, $.key), c = -1;
if(Gun.is(next)){ kick(next) }
chain._.on('soul').emit({soul: Gun.roulette.call(chain), empty: true, key: $.key});
});

return gun;
Expand Down
19 changes: 18 additions & 1 deletion lib/set.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Gun = Gun || require('../gun');

/*
Gun.chain.set = function(obj, cb, opt){
var set = this;
opt = opt || {};
Expand All @@ -15,4 +16,20 @@ Gun.chain.set = function(obj, cb, opt){
set.put(add, cb); // merge with the graph node.
});
return item;
};
};*/

Gun.chain.set = function(val, cb, opt){
var gun = this, ctx = {}, drift = Gun.time.now();
cb = cb || function(){};
opt = opt || {};

if(!gun.back){ gun = gun.put({}) }
gun = gun.not(function(next, key){
return key? this.put({}).key(key) : this.put({});
});
if(!val && !Gun.is.value(val)){ return gun }

var obj = {};
obj['I' + drift + 'R' + Gun.text.random(5)] = val;
return gun.put(obj, cb);
}
2 changes: 2 additions & 0 deletions test/abc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var expect = global.expect = require("./expect");
require('./common');
82 changes: 78 additions & 4 deletions test/all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,78 @@
console.log("MAKE SURE TO DELETE `data.json` BEFORE RUNNING TESTS!");

var expect = global.expect = require("./expect");
require('./common');
describe('All', function(){ return;
var expect = global.expect = require("./expect");

var Gun = Gun || require('../gun');
(typeof window === 'undefined') && require('../lib/file');

var gun = Gun({file: 'data.json'});

var keys = {
'emails/aquiva@gmail.com': 'asdf',
'emails/mark@gunDB.io': 'asdf',
'user/marknadal': 'asdf',
'emails/amber@cazzell.com': 'fdsa',
'user/ambernadal': 'fdsa',
'user/forrest': 'abcd',
'emails/banana@gmail.com': 'qwert',
'user/marknadal/messages/asdf': 'rti',
'user/marknadal/messages/fobar': 'yuoi',
'user/marknadal/messages/lol': 'hjkl',
'user/marknadal/messages/nano': 'vbnm',
'user/marknadal/messages/sweet': 'xcvb',
'user/marknadal/posts': 'qvtxz',
'emails/for@rest.com': 'abcd'
};

it('from', function() {
var r = gun.__.opt.hooks.all(keys, {from: 'user/'});
//console.log(r);
expect(r).to.be.eql({
'user/marknadal': { '#': 'asdf' },
'user/ambernadal': { '#': 'fdsa' },
'user/forrest': { '#': 'abcd' },
'user/marknadal/messages/asdf': { '#': 'rti' },
'user/marknadal/messages/fobar': { '#': 'yuoi' },
'user/marknadal/messages/lol': { '#': 'hjkl' },
'user/marknadal/messages/nano': { '#': 'vbnm' },
'user/marknadal/messages/sweet': { '#': 'xcvb' },
'user/marknadal/posts': { '#': 'qvtxz' }
});
});

it('from and upto', function() {
var r = gun.__.opt.hooks.all(keys, {from: 'user/', upto: '/'});
//console.log('upto', r);
expect(r).to.be.eql({
'user/marknadal': { '#': 'asdf' },
'user/ambernadal': { '#': 'fdsa' },
'user/forrest': { '#': 'abcd' }
});
});

it('from and upto and start and end', function() {
var r = gun.__.opt.hooks.all(keys, {from: 'user/', upto: '/', start: "c", end: "f"});
//console.log('upto and start and end', r);
expect(r).to.be.eql({
'user/forrest': { '#': 'abcd' }
});
});

it('map', function(done) { return done();
var users = gun.put({
a: {name: "Mark Nadal"},
b: {name: "Amber Nadal"},
c: {name: "Charlie Chapman"},
d: {name: "Johnny Depp"},
e: {name: "Santa Clause"}
});
//console.log("map:");
users.map().val(function(user){
//console.log("each user:", user);
}).path("ohboy");
return;
users.map(function(){

});
});

});
80 changes: 0 additions & 80 deletions test/scan-all.js

This file was deleted.

20 changes: 15 additions & 5 deletions test/set.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
(function(){ return; // group test
(function(){

var Gun = require('../index');
require('../lib/set');
var gun = Gun({file: 'data.json'});

var gun = Gun();

var list = gun.get('thoughts');
list.set('a');
list.set('b');
list.set('c');
list.set('d').val(function(val){
console.log('what', val, '\n\n');
console.log(gun.__.graph);
})
return;
gun.set({
name: "Mark Nadal",
age: 23,
Expand All @@ -17,8 +26,9 @@
age: 4,
type: "kitten"
}).back.val(function(g){
//console.log("GOT", g, this.__.graph);
console.log("GOT", g, this.__.graph);
}).map(function(val, id){
//console.log("map", id, val);
console.log("map", id, val);
});

}());
Loading

0 comments on commit ec8bdad

Please sign in to comment.