mirror of
https://github.com/amark/gun.git
synced 2025-07-10 14:52:34 +00:00
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:
parent
90f0d7f687
commit
a566572a2d
30
gun.js
30
gun.js
@ -905,22 +905,28 @@
|
|||||||
|
|
||||||
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;
|
||||||
|
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.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){
|
|
||||||
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);
|
Gun.on('opt', at);
|
||||||
if(!at.once){
|
if(!at.once){
|
||||||
gun.on('in', input, at);
|
gun.on('in', input, at);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user