var SEA = require('./sea'), Gun, u; if(SEA.window){ Gun = SEA.window.GUN || {chain:{}}; } else { Gun = require((u+'' == typeof MODULE?'.':'')+'./gun', 1); } SEA.GUN = Gun; function User(root){ this._ = {$: this}; } User.prototype = (function(){ function F(){}; F.prototype = Gun.chain; return new F() }()) // Object.create polyfill User.prototype.constructor = User; // let's extend the gun chain with a `user` function. // only one user can be logged in at a time, per gun instance. Gun.chain.user = function(pub){ var gun = this, root = gun.back(-1), user; if(pub){ pub = SEA.opt.pub((pub._||'')['#']) || pub; return root.get('~'+pub); } if(user = root.back('user')){ return user } var root = (root._), at = root, uuid = at.opt.uuid || lex; (at = (user = at.user = gun.chain(new User))._).opt = {}; at.opt.uuid = function(cb){ var id = uuid(), pub = root.user; if(!pub || !(pub = pub.is) || !(pub = pub.pub)){ return id } id = '~' + pub + '/' + id; if(cb && cb.call){ cb(null, id) } return id; } return user; } function lex(){ return Gun.state().toString(36).replace('.','') } Gun.User = User; User.GUN = Gun; User.SEA = Gun.SEA = SEA; module.exports = User;