ras.js 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function(){
  2. /**
  3. Radix AsyncStorage adapter
  4. make sure to pass AsyncStorage instance in opt.AsyncStorage
  5. example:
  6. import AsyncStorage from 'react-native'
  7. const store = Store({AsyncStorage})
  8. const gun = new Gun({store,peers:[...]})
  9. **/
  10. function Store(opt){
  11. opt = opt || {};
  12. const store = function(){}
  13. const as = opt.AsyncStorage;
  14. store.put = function(key, data, cb)
  15. {
  16. as.setItem(''+key,data)
  17. .then(_ => cb(null,1))
  18. .then(_ => console.log("ok put"))
  19. .catch(_ => {
  20. console.error(`failed saving to asyncstorage`,{key, data})
  21. cb(null,0)
  22. })
  23. }
  24. store.get = (key,cb) => {
  25. as.getItem(''+key)
  26. .then(data => cb(null,data))
  27. .then(_ => console.log("ok get"))
  28. .catch(_ => {
  29. console.error(`failed fetching from asyncstorage`,{key})
  30. cb(null,0)
  31. })
  32. }
  33. return store;
  34. }
  35. module.exports = Store
  36. }());