-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGunContext.js
51 lines (46 loc) · 1.18 KB
/
GunContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { createContext, useContext, useMemo } from 'react';
import * as SQLite from 'expo-sqlite';
import 'gun/lib/mobile';
import Gun from 'gun/gun';
import SEA from 'gun/sea';
import 'gun/lib/promise';
import 'gun/lib/radix';
import 'gun/lib/radisk';
import 'gun/lib/store';
import { makeStoreAdapter } from '@altrx/gundb-expo-sqlite-adapter';
makeStoreAdapter(Gun);
export const GunContext = createContext();
GunContext.displayName = 'GunContext';
export const GunProvider = (props) => {
const value = useMemo(
() => ({
gun: new Gun({
localStorage: false,
radisk: true,
sqlite: {
SQLite,
databaseName: 'todo.db',
onOpen: () => {
console.log('DB OPENED');
},
onError: (err) => {
console.log('ERROR');
},
onReady: (err) => {
console.log('READY');
},
},
}),
SEA,
}),
[]
);
return <GunContext.Provider value={value} {...props} />;
};
export function useGun() {
const context = useContext(GunContext);
if (context === undefined) {
throw new Error(`useGun must be used within a GunProvider`);
}
return context;
}