Shallow copy constructor options

Previously constructor options were deeply copied, which caused
issues with 3rd party plugins when they pass "class" instances as
options (since instances were copied into POJOs, breaking the prototype
chain). Now it's shallow copied. Special treatment is still given for
the `peers` property.
This commit is contained in:
Jesse Gibson 2016-12-15 15:35:11 -07:00
parent 90f0d7f687
commit a566572a2d

54
gun.js
View File

@ -903,32 +903,38 @@
;(function(){ ;(function(){
Gun.chain.opt = function(opt){ Gun.chain.opt = function(opt){
opt = opt || {}; opt = opt || {};
var gun = this, at = gun._, tmp, u; var peers = obj_is(opt) ? opt.peers : opt;
at.root = at.root || gun; if (text_is(peers)) {
peers = [peers];
}
if (list_is(peers)) {
peers = obj_map(peers, function (url, field, m) {
m(url, {});
});
}
if (!obj_is(opt)) {
opt = {};
}
opt.peers = peers;
var gun = this, at = gun._;
at.root = at.root || gun;
at.graph = at.graph || {}; at.graph = at.graph || {};
at.dedup = new Dedup(); at.dedup = new Dedup();
at.opt = at.opt || {}; at.opt = at.opt || {};
if(text_is(opt)){ opt = {peers: opt} }
else if(list_is(opt)){ opt = {peers: opt} } at.opt.peers = Gun.obj.to(at.opt.peers || {}, peers);
if(text_is(opt.peers)){ opt.peers = [opt.peers] } Gun.obj.to(opt, at.opt);
if(list_is(opt.peers)){ opt.peers = obj_map(opt.peers, function(n,f,m){m(n,{})}) }
obj_map(opt, function map(v,f){ Gun.on('opt', at);
if(obj_is(v)){ if(!at.once){
obj_map(v, map, this[f] || (this[f] = {})); // TODO: Bug? Be careful of falsey values getting overwritten? gun.on('in', input, at);
return; gun.on('out', output, at);
} }
this[f] = v; at.once = true;
}, at.opt); return gun;
Gun.on('opt', at); }
if(!at.once){
gun.on('in', input, at);
gun.on('out', output, at);
}
at.once = true;
return gun;
}
function output(at){ function output(at){
var cat = this, gun = cat.gun, tmp; var cat = this, gun = cat.gun, tmp;
// TODO: BUG! Outgoing `get` to read from in memory!!! // TODO: BUG! Outgoing `get` to read from in memory!!!