-
Notifications
You must be signed in to change notification settings - Fork 5
/
use-replicache.ts
60 lines (53 loc) · 1.8 KB
/
use-replicache.ts
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
52
53
54
55
56
57
58
59
60
import { mutators } from "@react-native-replicache/example-shared";
// import { createReplicacheExpoSQLiteKVStore } from "@react-native-replicache/react-native-expo-sqlite";
import { createReplicacheReactNativeOPSQLiteKVStore } from "@react-native-replicache/react-native-op-sqlite";
import React from "react";
import EventSource from "react-native-sse";
import { Replicache, TEST_LICENSE_KEY, dropAllDatabases } from "replicache";
export function useReplicache(listID: string) {
// See https://doc.replicache.dev/licensing for how to get a license key.
const licenseKey = TEST_LICENSE_KEY;
if (!licenseKey) {
throw new Error("Missing VITE_REPLICACHE_LICENSE_KEY");
}
const rep = React.useMemo(
() =>
new Replicache({
licenseKey,
pushURL: `http://127.0.0.1:8080/api/replicache/push?spaceID=${listID}`,
pullURL: `http://127.0.0.1:8080/api/replicache/pull?spaceID=${listID}`,
kvStore: createReplicacheReactNativeOPSQLiteKVStore,
name: listID,
mutators,
}),
[listID],
);
const close = React.useCallback(async () => {
await rep.close();
await dropAllDatabases({
kvStore: createReplicacheReactNativeOPSQLiteKVStore,
});
}, []);
React.useEffect(() => {
// Note: React Native doesn't support SSE; this is just a polyfill.
// You probably want to setup a WebSocket connection via Pusher.
const ev = new EventSource(
`http://127.0.0.1:8080/api/replicache/poke?spaceID=${listID}`,
{
headers: {
withCredentials: true,
},
},
);
ev.addEventListener("message", async (evt) => {
if (evt.type !== "message") return;
if (evt.data === "poke") {
await rep.pull();
}
});
return () => {
ev.close();
};
}, [listID]);
return { rep, close };
}