Skip to content

Commit

Permalink
Merge pull request #700 from amark/dev
Browse files Browse the repository at this point in the history
fix sea set
  • Loading branch information
amark authored Jan 31, 2019
2 parents ff28c16 + 2aae735 commit c5b1906
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 73 deletions.
2 changes: 1 addition & 1 deletion examples/basic/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h1>User</h1>
<input id="pass" type="password" placeholder="passphrase">
<input id="in" type="submit" value="sign in">
<input id="up" type="button" value="sign up">
<input id="mask" type="button" value="MetaMask Login">
<input id="mask" type="button" value="Identifi Login">
</form>

<ul></ul>
Expand Down
160 changes: 97 additions & 63 deletions examples/party.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
;(() => {
function S(){};
window.S = S;
try{localStorage.clear();//sessionStorage.clear();
}catch(e){}
S.gun = Gun(location.host? location.origin+'/gun' : 'http://localhost:8765/gun');
//S.gun = Gun('http://localhost:8765/gun');
//S.gun = Gun();
S.app = S.gun.get('examples/social/1');
S.app = S.gun.get('examples/social/2');
S.user = S.gun.user();
S.tell = (what, n) => {
var e = $('#tell').find('p');
Expand Down Expand Up @@ -530,7 +532,7 @@
</ul>
<div class="model">
<li class="spoke tint sap gully">
<div class="gap"><span class="sort none"></span><img class="face act none"><b class="name"></b><span class="what"></span></div>
<div class="gap"><span class="sort none"></span><img class="face act none"><b class="name"></b><div class="what"></div></div>
</li>
</div>
<div style="height: 10%;"></div>
Expand All @@ -547,23 +549,33 @@
});
window.user = S.user;
$('#speak').on('submit', (e) => {
/*var say = normalize($('#speak .draft'));
console.log(1, say.html());
return;*/
var say = $('#speak .draft').text();
var say = $('#speak .draft').text(); //.text(); // NO NO NO NO NO
if(!say){ return }
var ref = S.user.get('who').get('all').set({what: say});
ref.get('by').put(S.user.get('who'));
S.user.get('who').get('said').time(ref);
S.gun.get('@').time(ref);
console.log('save!', say);
var ref = S.user.get('who').get('all').set({what: say, when: Gun.state()});
//ref.get('by').put(S.user.get('who'));
//S.user.get('who').get('said').time(ref);
S.user.get('who').get('said').set(ref);
//S.gun.get('@').time(ref);
$('#speak .draft').text('');
});
S.gun.get('@').time(async (data, key, time) => {
var ref = S.gun.get(data), tmp;
var said = await ref.then();
//S.gun.get('@').time(async (data, key, time) => {
S.user.get('who').get('said').map().once(async (data, key, time) => {
//var ref = S.gun.get(data), tmp;
//var said = await ref.then();
key = key.replace(/[^A-Za-z]/ig,'');
var tmp, said = data, time = said.when;
var $li = $($('#'+key)[0] || $('#draft .model .spoke').clone(true,true).attr('id', key)[(tmp = $.as.sort(time, $('#draft ul').children('li').first()))[0]?'insertBefore':'appendTo'](tmp[0] || '#draft ul'));
$li.find('.what').text(said.what);
var by = ref.get('by');
tmp = said.what;
if(tmp && tmp.ct){
tmp = JSON.stringify(tmp);
setTimeout(async function(){
tmp = await SEA.decrypt(said.what, S.user._.sea);
$li.find('.what').text(tmp);
}, 750);
}
$li.find('.what').text(tmp); // NORMALIAZE!!!
var by = S.user.get('who');// ref.get('by');
by.get('face').get('small').on(data => {
$li.find('.face').attr('src', data).removeClass('none');
});
Expand All @@ -579,69 +591,88 @@
if(face){
$li.find('.face').attr('src', face).removeClass('none');
}
}, 10);
});
$(document).on('click', '#speak .act.face', (eve) => {

});
});
</script>
</div>

<div id="create" class="white center act">
<div id="contacts" class="hue2 page">
<style>
#create {
position: fixed;
bottom: 2em;
right: 2em;
font-size: 1em;
font-family: Tahoma, arial;
border-radius: 1em;
z-index: 99999;
width: 2em;
height: 2em;
opacity: 0.8;
overflow: visible;
transition: all 0.2s ease-in;
}
#create:hover {
opacity: 1;
}
#create span {
line-height: 2em;
#contacts ul .face {
float: left;
border-radius: 100%;
vertical-align: middle;
height: 2.5em;
margin-right: 5%;
}
#create .menu {
display: none;
height: 10em;
width: 10em;
bottom: 0em;
right: 0em;
#contacts ul, #contacts li {
overflow: visible;
position: absolute;
border-bottom-right-radius: 1em;
text-align: left;
}
#create:hover .menu {
display: block;
}
#create li {
padding: 0.3em 0.5em;
#contacts .who {
display: inline-block;
margin-right: 2%;
min-width: 250px;
width: 15em;
text-align: left;
padding: 2%;
vertical-align: middle;
}
#create a {
color: black;
#contacts .what {
}
#create a:hover {
color: #4D79D8;
#contacts .spoke {
}
</style>
<span class="huet act">+</span>
<div class="white sap menu" style="width: 7em;">
<ul class="left blackt gap">
<a href="#out" class="act"><li>Sign Out</li></a>
<a href="#settings" class="act"><li>Settings</li></a>
<li>Profile</li>
<li>Help</li>
</ul>
<p class="pad">Contacts</p>
<ul class="mid row col center">
</ul>
<div class="model">
<li class="who tint sap gully">
<div class="gap">
<span class="sort none"></span>
<img class="face act none" crossOrigin="Anonymous">
<big class="name"></big><br/>
<i class="alias"></i><br/>
<small>FB ID:</small> <span class="fbid"></span>
</div>
</li>
</div>
<div style="height: 10%;"></div>
<script>
$.as.route.page('contacts', () => {
if(!S.user.is){ return $.as.route('sign') }
//S.gun.get('@').time(async (data, key, time) => {

// TODO: BUG!! switch from `on` to `once` to get Martti's {_} empty object bug.
var $ul = $('#contacts ul');
S.user.get('old').get('fb').map().on(async function(data){
console.log('contact:', data);
var key = data.fbid, tmp;
if(!key){ return }
var $li = $($('#fbid'+key)[0] || $('#contacts .model .who').clone(true,true).attr('id', 'fbid'+key)[(tmp = $.as.sort(data.name||1, $ul.children('li').first()))[0]?'insertBefore':'appendTo'](tmp[0] || $ul));

$li.find('.name').text(data.name);
$li.find('.alias').text(data.alias);
$li.find('.fbid').text(data.fbid);
tmp = $li.find('img').attr('src', data.face || data.tmp);
if(!data.face){
var ref = this;
$.fn.upload.shrink(data.tmp, function(b64){
ref.get('face').put(b64);
}, 100);
}
$('html, body').stop(true, true).animate({scrollTop: $ul.height()});
});
function img2b64(img, cb){
var c = document.createElement('canvas');
var ctx = c.getContext("2d");
ctx.drawImage(img, 10, 10);
cb(c.toDataURL());
}
});
</script>
</div>

<div id="tell" class="center">
Expand Down Expand Up @@ -673,6 +704,7 @@
</style>
<p class="mid black">Hello world!</p>
</div>
<!-- textarea id='debug' class="no-ne" style="position: fixed; bottom: 0; left: 0; width: 25%; height: 30%"></textarea -->

<script>
$.as.route.page('person', () => {
Expand All @@ -695,7 +727,9 @@
});
});
</script>
<script async src="../../gun/lib/fun.js"></script>
<!-- script async src="../../gun/lib/fun.js"></script -->
<script async src="../../gun/lib/normalize.js"></script>
<script async src="../../gun/lib/monotype.js"></script>
<script async src="../../gun/lib/meta.js"></script>
</body>
</html>
11 changes: 7 additions & 4 deletions gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,10 @@
if(!(at.has || at.soul)){ return }
var tmp = at.map, root = at.root;
at.map = null;
if(at.has){ at.link = null }
if(at.has){
if(at.dub && at.root.stop){ at.dub = null }
at.link = null;
}
//if(!root.now || !root.now[at.id]){
if(!at.pass){
if((!msg['@']) && null === tmp){ return }
Expand Down Expand Up @@ -1286,8 +1289,7 @@
}
function soul(gun, cb, opt, as){
var cat = gun._, acks = 0, tmp;
if(tmp = cat.soul){ return cb(tmp, as, cat), gun }
if(tmp = cat.link){ return cb(tmp, as, cat), gun }
if(tmp = cat.soul || cat.link || cat.dub){ return cb(tmp, as, cat), gun }
gun.get(function(msg, ev){
if(u === msg.put && (tmp = (obj_map(cat.root.opt.peers, function(v,k,t){t(k)})||[]).length) && ++acks < tmp){
return;
Expand Down Expand Up @@ -1397,6 +1399,7 @@
}, true);
return gun;
}
if(at.has && (tmp = Gun.val.link.is(data))){ at.dub = tmp }
as.ref = as.ref || (root._ === (tmp = at.back))? gun : tmp.$;
if(as.ref._.soul && Gun.val.is(as.data) && at.get){
as.data = obj_put({}, at.get, as.data);
Expand Down Expand Up @@ -1551,7 +1554,7 @@
if(node_ == at.get){
as.soul = (at.put||empty)['#'] || at.dub;
}
as.soul = as.soul || at.soul || at.soul || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
}
if(!as.soul){ // polyfill async uuid for SEA
as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
Expand Down
3 changes: 2 additions & 1 deletion lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
if(!e){ return cb && cb({err: "No file!"}) }
if(e.err){ return }
var file = (((e.event || e).target || e).result || e), img = new Image();
img.crossOrigin = "Anonymous";
img.src = file;
img.onload = function(){
if(img.width < w && img.height < (h||Infinity)){
if(img.width < (w = w || 1000) && img.height < (h||Infinity) && "data:" == file.slice(0,5)){
e.base64 = file;
return cb(e || file);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.9.999999",
"version": "0.9.9999991",
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
"main": "index.js",
"browser": "gun.min.js",
Expand Down
20 changes: 17 additions & 3 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,13 +1265,14 @@ describe('Gun', function(){
describe('API', function(){
var gopt = {wire:{put:function(n,cb){cb()},get:function(k,cb){cb()}}};
if(Gun.window && location.search){
console.log("LOCALHOST PEER MUST BE ON!");
/*console.log("LOCALHOST PEER MUST BE ON!");
var peer = {url: 'http://localhost:8765/gun'};
Gun.on('opt', function(root){
if(root.opt.test_no_peer){ return this.to.next(root) }
root.opt.peers = root.opt.peers || {};
root.opt.peers['http://localhost:8765/gun'] = {url: 'http://localhost:8765/gun'};
root.opt.peers['http://localhost:8765/gun'] = peer;
this.to.next(root);
});
});*/
}
var gun = Gun();

Expand Down Expand Up @@ -3755,6 +3756,19 @@ describe('Gun', function(){
});
//});
});

it('Set a ref should be found', function(done){
var gun = Gun();
var msg = {what: 'hello world'};
//var ref = user.get('who').get('all').set(msg);
//user.get('who').get('said').set(ref);
var ref = gun.get('who').get('all').set(msg);
gun.get('who').get('said').set(ref);
gun.get('who').get('said').map().once(function(data){
expect(data.what).to.be.ok();
done();
})
});
return;
it('Nested listener should be called', function(done){

Expand Down
40 changes: 40 additions & 0 deletions test/sea/sea.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,46 @@ describe('SEA', function(){
done();
});
})

it('set user ref should be found', function(done){
var gun = Gun();
var user = gun.user();
var msg = {what: 'hello world'};
user.create('zach', 'password');
gun.on('auth', function(){
var ref = user.get('who').get('all').set(msg);
user.get('who').get('said').set(ref);
user.get('who').get('said').map().once(function(data){
expect(data.what).to.be.ok();
done();
})
})
});

it('set user ref null override', function(done){
this.timeout(9000);
var gun = Gun();
var user = gun.user();
var msg = {what: 'hello world'};
user.create('xavier', 'password');
gun.on('auth', function(){
var ref = user.get('who').get('all').set(msg);
var tmp = ref._.dub || ref._.link;
setTimeout(function(){
user.get('who').put(1);
setTimeout(function(){
user.get('who').get('all').get(tmp).put({boom: 'ah'});
setTimeout(function(){
user.get('who').get('all').map().once(function(data){
expect(data).to.be.ok();
expect(data.what).to.not.be.ok();
done();
});
},9);
},9);
},9);
});
});
});

})
Expand Down

0 comments on commit c5b1906

Please sign in to comment.