Skip to content

Instantly share code, notes, and snippets.

@amark
Created August 25, 2016 19:55
Show Gist options
  • Save amark/41d31de8911e6d5ebe13127180cf9588 to your computer and use it in GitHub Desktop.
Save amark/41d31de8911e6d5ebe13127180cf9588 to your computer and use it in GitHub Desktop.
This takes the internal functionality of `.put` and RIPS IT OUT, separating it from the chaining API and the event listeners that cause problems - but NOTE, this does not support the chaining API or async behavior! Plus it has a couple extra hidden gems like auto-Array-to-Object conversion and UUID hinting.
function importGUN3 (data, options){
options = options || {};
options.prefix = 'gun/';
if(options.arrays !== false){
options.arrays = true;
data = array2object(data);
}
var drift = options.state || Gun.time.now();
var ctx = {}, at = {};
Gun.ify(data, function(env, cb, map){
var eat;
if(!env || !(eat = env.at) || !env.at.node){ return }
if(!eat.node._){ eat.node._ = {} }
if(!eat.node._[Gun._.state]){ eat.node._[Gun._.state] = {} }
if(!Gun.is.node.soul(eat.node)){
if(data === eat.obj){
Gun.obj.as(env.graph, eat.soul = Gun.obj.as(eat.node._, Gun._.soul, Gun.is.node.soul(eat.obj) || (options.soul || options.id || (data._ && data._['#']) || data.soul || data._id || data._ID || data.id || data.ID || Gun.text.random()).toString()), eat.node);
options.soul = eat.soul;
cb(eat, eat.soul);
} else {
var path = function(err, node){
if(path.opt && path.opt.on && path.opt.on.off){ path.opt.on.off() }
if(path.opt.done){ return }
path.opt.done = true;
if(err){ env.err = err }
eat.soul = Gun.is.node.soul(node) || Gun.is.node.soul(eat.obj) || Gun.is.node.soul(eat.node) || ((eat.obj._ && eat.obj._['#']) || eat.obj.soul || eat.obj._id || eat.obj._ID || eat.obj.id || eat.obj.ID || Gun.text.random()).toString();
Gun.obj.as(env.graph, Gun.obj.as(eat.node._, Gun._.soul, eat.soul), eat.node);
cb(eat, eat.soul);
}; path.opt = {put: true};
if(ctx.not){
path();
} else {
if(at.field || at.at){
} else {
load(eat.path || [], path, options);
}
}
}
}
if(!eat.field){ return }
eat.node._[Gun._.state][eat.field] = drift;
}, {pure: true})(function(err, ify){
save(ify.graph, options);
});
}
function load(arr, cb, opt){
var soul = opt.prefix + opt.soul;
var node = store.get(soul);
if(!node){
return cb(null, node);
}
var field = arr[0];
var val = node[field];
if(!val){
return cb(null);
}
var rel = Gun.is.rel(val) || Gun.is.node.soul(val);
if(rel){
return cb(null, Gun.is.node.ify({}, rel));
}
return cb(null);
}
function save(graph, opt){
var err = Gun.is.graph(graph, function(node, soul){
store.put(opt.prefix + soul, node, function(err){if(err){ console.log({err: err}) }});
});
}
function array2object(arr){
var obj = {};
Gun.list.map(arr, function(v,f,t){
if(Gun.list.is(v) || Gun.obj.is(v)){
obj[f] = array2object(v);
return;
}
obj[f] = v;
});
return obj;
}
var LS = localStorage;
var store = {
get: function(key){
var obj;
try{obj = Gun.obj.ify(LS.getItem(key) || null)
}catch(e){obj = null}
return obj;
},
put: function(key, val, cb){
try{ LS.setItem(key, Gun.text.ify(val)) }catch(e){if(cb)cb(e)}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment