Skip to content

Instantly share code, notes, and snippets.

@jpstrikesback
Last active April 20, 2019 03:15
Show Gist options
  • Save jpstrikesback/0c89ac35b7e2421687b14f268dd0f6a3 to your computer and use it in GitHub Desktop.
Save jpstrikesback/0c89ac35b7e2421687b14f268dd0f6a3 to your computer and use it in GitHub Desktop.
RAD AsyncStorage Adapter
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>
);
}
}
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