add: lates ras.js

This commit is contained in:
Hadar Rottenberg 2020-05-21 22:10:07 +03:00
parent d681c5ca91
commit 99cb0e92df

View File

@ -1,40 +1,41 @@
;(function(){ (function(){
/** /**
Radix AsyncStorage adapter Radix AsyncStorage adapter
make sure to pass AsyncStorage instance in opt.AsyncStorage make sure to pass AsyncStorage instance in opt.AsyncStorage
example:
import AsyncStorage from 'react-native'
const store = Store({AsyncStorage})
const gun = new Gun({store,peers:[...]})
**/ **/
function Store(opt){ function Store(opt){
opt = opt || {}; opt = opt || {};
const store = () => { const store = function(){}
const as = opt.AsyncStorage; const as = opt.AsyncStorage;
const put = (key, data, cb) => store.put = function(key, data, cb)
{ {
as.setItem(''+key,data) as.setItem(''+key,data)
.then(_ => cb(null,1)) .then(_ => cb(null,1))
.then(_ => console.log("ok put"))
.catch(_ => { .catch(_ => {
console.error(`failed saving to asyncstorage`,{key, data}) console.error(`failed saving to asyncstorage`,{key, data})
cb(null,0) cb(null,0)
}) })
} }
const get = (key,cb) => { store.get = (key,cb) => {
as.getItem(''+key) as.getItem(''+key)
.then(data => cb(null,data)) .then(data => cb(null,data))
.then(_ => console.log("ok get"))
.catch(_ => { .catch(_ => {
console.error(`failed fetching from asyncstorage`,{key}) console.error(`failed fetching from asyncstorage`,{key})
cb(null,0) cb(null,0)
}) })
} }
}
return store; return store;
} }
if(typeof window !== "undefined"){ module.exports = Store
(Store.window = window).RasyncStorage = Store;
} else {
try{ module.exports = Store }catch(e){}
}
}()); }());