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