@@ -2,13 +2,27 @@ import React, { useContext, useState } from 'react';
22import { Replicator } from 'cbl-reactnative' ;
33import ReplicatorStatusChangeContext from '@/providers/ReplicatorStatusChangeContext' ;
44import ReplicatorStatusTokenContext from '@/providers/ReplicatorStatusTokenContext' ;
5- import replicationStatusChange from '@/service/replicator/replicationStatusChange' ;
5+ import start from '@/service/replicator/start' ;
6+ import stop from '@/service/replicator/stop' ;
67import ReplicatorIdActionForm from '@/components/ReplicatorIdActionForm/ReplicatorIdActionForm' ;
78import { useStyleScheme } from '@/components/Themed/Themed' ;
8- import { SafeAreaView } from 'react-native' ;
9+ import { NativeEventEmitter , NativeModules , SafeAreaView } from 'react-native' ;
910import ResultListView from '@/components/ResultsListView/ResultsListView' ;
11+ //debug the message queue
12+ import MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue' ;
1013
1114export default function ReplicatorStatusScreen ( ) {
15+ //debug the message queue
16+ const spyMessageQueue = ( message : any ) => {
17+ if (
18+ message . type === 0 &&
19+ message . method === 'emit' &&
20+ message . module === 'RCTDeviceEventEmitter'
21+ ) {
22+ console . log ( `::MESSAGE-QUEUE:: ${ message } ` ) ;
23+ }
24+ } ;
25+
1226 const styles = useStyleScheme ( ) ;
1327 const { statusChangeMessages, setStatusChangeMessages } = useContext (
1428 ReplicatorStatusChangeContext
@@ -27,17 +41,37 @@ export default function ReplicatorStatusScreen() {
2741 const replicatorId = replId . toString ( ) ;
2842 setSelectedReplicatorId ( replicatorId ) ;
2943 try {
44+ //debug the message queue
45+ //MessageQueue.spy(spyMessageQueue);
3046 const token = statusToken [ replicatorId ] ;
3147 if ( token === undefined ) {
3248 setInformationMessages ( ( prev ) => [
3349 ...prev ,
3450 `::Information: Replicator <${ replicatorId } > Starting Status Change listener...` ,
3551 ] ) ;
36- await replicationStatusChange (
37- replicator ,
38- setStatusChangeMessages ,
39- setStatusToken
40- ) ;
52+ const date = new Date ( ) . toISOString ( ) ;
53+ const changeToken = await replicator . addChangeListener ( ( change ) => {
54+ const newMessage = [
55+ `${ date } ::Status:: Replicator <${ replicator . getId ( ) } > status changed: ${ change . status } ` ,
56+ ] ;
57+ setStatusChangeMessages ( ( prev ) => {
58+ return {
59+ ...prev ,
60+ [ replicatorId ] : [ ...( prev [ replicatorId ] || [ ] ) , ...newMessage ] ,
61+ } ;
62+ } ) ;
63+ } ) ;
64+ setStatusToken ( ( prev ) => {
65+ return {
66+ ...prev ,
67+ [ replicatorId ] : changeToken ,
68+ } ;
69+ } ) ;
70+ setInformationMessages ( ( prev ) => [
71+ ...prev ,
72+ `::Information: Replicator <${ replicatorId } > Starting Replicator...` ,
73+ ] ) ;
74+ await start ( replicator , false ) ;
4175 } else {
4276 setInformationMessages ( ( prev ) => [
4377 ...prev ,
@@ -58,6 +92,52 @@ export default function ReplicatorStatusScreen() {
5892 ] ) ;
5993 }
6094 }
95+
96+ async function stopReplicator ( replicator : Replicator ) : Promise < void > {
97+ try {
98+ const replId = replicator . getId ( ) ;
99+ if ( replId !== undefined ) {
100+ const replicatorId = replId . toString ( ) ;
101+ setInformationMessages ( ( prev ) => [
102+ ...prev ,
103+ `::Information: Stopping Replicator with replicatorId: <${ replicatorId } >.` ,
104+ ] ) ;
105+ await stop ( replicator ) ;
106+ setStatusToken ( ( prev ) => {
107+ const newStatusToken = { ...prev } ;
108+ delete newStatusToken [ replicatorId ] ;
109+ return newStatusToken ;
110+ } ) ;
111+ setInformationMessages ( ( prev ) => [
112+ ...prev ,
113+ `::Information: Stopped Replicator with replicatorId: <${ replicatorId } >.` ,
114+ ] ) ;
115+
116+ const token = statusToken [ replicatorId ] ;
117+ setInformationMessages ( ( prev ) => [
118+ ...prev ,
119+ `::Information: Removing change listener with token <${ token } > from Replicator with replicatorId: <${ replicatorId } >.` ,
120+ ] ) ;
121+ await replicator . removeChangeListener ( token ) ;
122+ setInformationMessages ( ( prev ) => [
123+ ...prev ,
124+ `::Information: Removed change listener with token <${ token } > from Replicator with replicatorId: <${ replicatorId } >.` ,
125+ ] ) ;
126+ } else {
127+ setInformationMessages ( ( prev ) => [
128+ ...prev ,
129+ `::Error: Couldn't get replicatorId from replicator.` ,
130+ ] ) ;
131+ }
132+ } catch ( error ) {
133+ setInformationMessages ( ( prev ) => [
134+ ...prev ,
135+ // @ts -ignore
136+ `::ERROR: ${ error . message } ` ,
137+ ] ) ;
138+ }
139+ }
140+
61141 const filteredStatusChangeMessages =
62142 statusChangeMessages [ selectedReplicatorId ] || [ ] ;
63143 const combinedMessages = [
@@ -69,6 +149,7 @@ export default function ReplicatorStatusScreen() {
69149 < ReplicatorIdActionForm
70150 handleUpdatePressed = { update }
71151 handleResetPressed = { reset }
152+ handleStopPressed = { stopReplicator }
72153 screenTitle = "Replicator Status"
73154 />
74155 < ResultListView useScrollView = { true } messages = { combinedMessages } />
0 commit comments