Last active
April 20, 2019 03:15
-
-
Save jpstrikesback/0c89ac35b7e2421687b14f268dd0f6a3 to your computer and use it in GitHub Desktop.
RAD AsyncStorage Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { | |
View, | |
Text, | |
} from 'react-native'; | |
import Gun from 'gun/gun'; | |
import 'gun/lib/open'; | |
import '../extensions/sea'; | |
import GunStore from './RasyncStorage.js'; | |
const Store = GunStore(Gun); | |
Gun.on('create', function(root) { | |
this.to.next(root); | |
root.opt.store = root.opt.store || Store(root.opt); | |
}); | |
export class Demo extends React.Component { | |
constructor() { | |
super(); | |
this.gun = new Gun(); | |
this.user = this.gun.user(); | |
window.gun = this.gun; | |
window.user = this.user; | |
this.state = { | |
authenticated: false, | |
list: [], | |
listText: '', | |
username: '', | |
password: '', | |
}; | |
} | |
// Store and retrieve things with gun | |
render() { | |
return ( | |
<View> | |
<Text>render things from GUN</Text> | |
</View> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AsyncStorage } from 'react-native'; | |
export default (GunStore = Gun => | |
function Store(opt = {}) { | |
opt.file = String(opt.file || 'radata'); | |
const store = function Store() {}; | |
store.put = function(key, data, cb) { | |
AsyncStorage.setItem('' + key, data, err => { | |
if (err) cb(err); | |
cb(null, 1); | |
}); | |
}; | |
store.get = function(key, cb) { | |
AsyncStorage.getItem('' + key, (err, res) => { | |
if (err) cb(err); | |
cb(null, res); | |
}); | |
}; | |
return store; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment