|
| 1 | +import React, { useContext, useState } from 'react'; |
| 2 | +import { Replicator } from 'cbl-reactnative'; |
| 3 | +import ReplicatorDocumentChangeContext from '@/providers/ReplicationDocumentChangeContext'; |
| 4 | +import ReplicatorStatusTokenContext from '@/providers/ReplicatorStatusTokenContext'; |
| 5 | +import start from '@/service/replicator/start'; |
| 6 | +import stop from '@/service/replicator/stop'; |
| 7 | +import ReplicatorIdActionForm from '@/components/ReplicatorIdActionForm/ReplicatorIdActionForm'; |
| 8 | +import { useStyleScheme } from '@/components/Themed/Themed'; |
| 9 | +import { SafeAreaView } from 'react-native'; |
| 10 | +import ResultListView from '@/components/ResultsListView/ResultsListView'; |
| 11 | + |
| 12 | +export default function DocumentReplicationScreen() { |
| 13 | + const styles = useStyleScheme(); |
| 14 | + const { documentChangeMessages, setDocumentChangeMessages } = useContext( |
| 15 | + ReplicatorDocumentChangeContext |
| 16 | + )!; |
| 17 | + const { statusToken, setStatusToken } = useContext( |
| 18 | + ReplicatorStatusTokenContext |
| 19 | + )!; |
| 20 | + const [informationMessages, setInformationMessages] = useState<string[]>([]); |
| 21 | + const [selectedReplicatorId, setSelectedReplicatorId] = useState<string>(''); |
| 22 | + const [documentTokens, setDocumentTokens] = useState<Record<string, string>>({}); |
| 23 | + |
| 24 | + function reset() {} |
| 25 | + |
| 26 | + async function update(replicator: Replicator): Promise<void> { |
| 27 | + const replId = replicator.getId(); |
| 28 | + if (replId !== undefined) { |
| 29 | + const replicatorId = replId.toString(); |
| 30 | + setSelectedReplicatorId(replicatorId); |
| 31 | + try { |
| 32 | + const token = documentTokens[replicatorId]; |
| 33 | + if (token === undefined) { |
| 34 | + setInformationMessages((prev) => [ |
| 35 | + ...prev, |
| 36 | + `::Information: Replicator <${replicatorId}> Starting Document Change listener...`, |
| 37 | + ]); |
| 38 | + const changeToken = await replicator.addDocumentChangeListener((documentReplication) => { |
| 39 | + const date = new Date().toISOString(); |
| 40 | + const docs = documentReplication.documents; |
| 41 | + const direction = documentReplication.isPush ? 'PUSH' : 'PULL'; |
| 42 | + |
| 43 | + const newMessages = docs.map(doc => { |
| 44 | + const flags = doc.flags ? doc.flags.join(', ') : 'none'; |
| 45 | + const error = doc.error ? `, Error: ${doc.error}` : ''; |
| 46 | + return `${date}::Doc:: ${direction} - Scope: ${doc.scopeName}, Collection: ${doc.collectionName}, ID: ${doc.id}, Flags: ${flags}${error}`; |
| 47 | + }); |
| 48 | + |
| 49 | + setInformationMessages((prev) => [...prev, ...newMessages]); |
| 50 | + }); |
| 51 | + |
| 52 | + setDocumentTokens((prev) => { |
| 53 | + return { |
| 54 | + ...prev, |
| 55 | + [replicatorId]: changeToken, |
| 56 | + }; |
| 57 | + }); |
| 58 | + |
| 59 | + setInformationMessages((prev) => [ |
| 60 | + ...prev, |
| 61 | + `::Information: Replicator <${replicatorId}> Document listener registered, starting replicator...`, |
| 62 | + ]); |
| 63 | + |
| 64 | + await start(replicator, false); |
| 65 | + } else { |
| 66 | + setInformationMessages((prev) => [ |
| 67 | + ...prev, |
| 68 | + `::Information: Replicator <${replicatorId}> Document Change already running with token: <${token}>.`, |
| 69 | + ]); |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + setInformationMessages((prev) => [ |
| 73 | + ...prev, |
| 74 | + // @ts-ignore |
| 75 | + `::ERROR: ${error.message}`, |
| 76 | + ]); |
| 77 | + } |
| 78 | + } else { |
| 79 | + setInformationMessages((prev) => [ |
| 80 | + ...prev, |
| 81 | + `::ERROR: ReplicatorId is undefined`, |
| 82 | + ]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + async function stopReplicator(replicator: Replicator): Promise<void> { |
| 87 | + try { |
| 88 | + const replId = replicator.getId(); |
| 89 | + if (replId !== undefined) { |
| 90 | + const replicatorId = replId.toString(); |
| 91 | + setInformationMessages((prev) => [ |
| 92 | + ...prev, |
| 93 | + `::Information: Stopping Replicator with replicatorId: <${replicatorId}>.`, |
| 94 | + ]); |
| 95 | + await stop(replicator); |
| 96 | + |
| 97 | + const token = documentTokens[replicatorId]; |
| 98 | + if (token) { |
| 99 | + setInformationMessages((prev) => [ |
| 100 | + ...prev, |
| 101 | + `::Information: Removing document change listener with token <${token}> from Replicator with replicatorId: <${replicatorId}>.`, |
| 102 | + ]); |
| 103 | + await replicator.removeChangeListener(token); |
| 104 | + |
| 105 | + setDocumentTokens((prev) => { |
| 106 | + const newTokens = { ...prev }; |
| 107 | + delete newTokens[replicatorId]; |
| 108 | + return newTokens; |
| 109 | + }); |
| 110 | + |
| 111 | + setInformationMessages((prev) => [ |
| 112 | + ...prev, |
| 113 | + `::Information: Removed document change listener with token <${token}> from Replicator with replicatorId: <${replicatorId}>.`, |
| 114 | + ]); |
| 115 | + } |
| 116 | + } else { |
| 117 | + setInformationMessages((prev) => [ |
| 118 | + ...prev, |
| 119 | + `::Error: Couldn't get replicatorId from replicator.`, |
| 120 | + ]); |
| 121 | + } |
| 122 | + } catch (error) { |
| 123 | + setInformationMessages((prev) => [ |
| 124 | + ...prev, |
| 125 | + // @ts-ignore |
| 126 | + `::ERROR: ${error.message}`, |
| 127 | + ]); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + const filteredDocumentChangeMessages = |
| 132 | + documentChangeMessages[selectedReplicatorId] || []; |
| 133 | + const combinedMessages = [ |
| 134 | + ...informationMessages, |
| 135 | + ...filteredDocumentChangeMessages, |
| 136 | + ]; |
| 137 | + |
| 138 | + return ( |
| 139 | + <SafeAreaView style={styles.container}> |
| 140 | + <ReplicatorIdActionForm |
| 141 | + handleUpdatePressed={update} |
| 142 | + handleResetPressed={reset} |
| 143 | + handleStopPressed={stopReplicator} |
| 144 | + screenTitle="Document Replication" |
| 145 | + /> |
| 146 | + <ResultListView useScrollView={true} messages={combinedMessages} /> |
| 147 | + </SafeAreaView> |
| 148 | + ); |
| 149 | +} |
0 commit comments