Skip to content

Commit 5d0c2f4

Browse files
committed
feat: updated to try and fix issues with query rendering and status messages from replicator
1 parent 487eeae commit 5d0c2f4

File tree

10 files changed

+214
-96
lines changed

10 files changed

+214
-96
lines changed

expo-example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ allprojects {
3939
maven { url 'https://www.jitpack.io' }
4040
}
4141
}
42-
apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"
42+
apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"

expo-example/app/replication/status.tsx

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@ import React, { useContext, useState } from 'react';
22
import { Replicator } from 'cbl-reactnative';
33
import ReplicatorStatusChangeContext from '@/providers/ReplicatorStatusChangeContext';
44
import 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';
67
import ReplicatorIdActionForm from '@/components/ReplicatorIdActionForm/ReplicatorIdActionForm';
78
import { useStyleScheme } from '@/components/Themed/Themed';
8-
import { SafeAreaView } from 'react-native';
9+
import { NativeEventEmitter, NativeModules, SafeAreaView } from 'react-native';
910
import ResultListView from '@/components/ResultsListView/ResultsListView';
11+
//debug the message queue
12+
import MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue';
1013

1114
export 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} />

expo-example/components/CBLDatabaseQueryActionContainer/CBLDatabaseQueryActionContainer.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default function CBLDatabaseQueryActionContainer({
118118
/>
119119
{children && children}
120120
</View>
121-
<ResultListView messages={resultMessage} />
121+
<ResultListView messages={resultMessage} style={localStyles.results} />
122122
</SafeAreaView>
123123
);
124124
}
@@ -130,4 +130,7 @@ const localStyles = StyleSheet.create({
130130
marginTop: 5,
131131
marginBottom: 15,
132132
},
133+
results: {
134+
marginBottom: 280,
135+
},
133136
});

expo-example/components/ReplicatorIdActionForm/ReplicatorIdActionForm.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function ReplicatorIdActionForm({
1111
screenTitle,
1212
handleUpdatePressed,
1313
handleResetPressed,
14+
handleStopPressed,
1415
}: ReplicatorIdActionFormProps) {
1516
const { replicatorIds } = useContext(ReplicatorContext)!;
1617
const [replicatorId, setReplicatorId] = useState<string>('');
@@ -27,6 +28,12 @@ export default function ReplicatorIdActionForm({
2728
onPress: update,
2829
},
2930
];
31+
if (handleStopPressed !== undefined) {
32+
icons.push({
33+
iconName: 'stop',
34+
onPress: stop,
35+
});
36+
}
3037

3138
async function update() {
3239
if (replicatorId in replicatorIds) {
@@ -39,6 +46,17 @@ export default function ReplicatorIdActionForm({
3946
}
4047
}
4148

49+
async function stop() {
50+
if (replicatorId in replicatorIds && handleStopPressed !== undefined) {
51+
const replicator = replicatorIds[replicatorId];
52+
await handleStopPressed(replicator);
53+
} else {
54+
throw new Error(
55+
`Error: Replicator <${replicatorId}> not found in context or no handle to stop the replicator. Make sure replicator was created first prior to trying to stop it.`
56+
);
57+
}
58+
}
59+
4260
function reset() {
4361
setReplicatorId('');
4462
handleResetPressed();

expo-example/components/ReplicatorIdActionForm/ReplicatorIdActionFormProps.type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export type ReplicatorIdActionFormProps = {
44
screenTitle: string;
55
handleUpdatePressed: (replicator: Replicator) => Promise<void>;
66
handleResetPressed: () => void;
7+
handleStopPressed?: (replicator: Replicator) => Promise<void>;
78
};

expo-example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,9 +2143,9 @@ SPEC CHECKSUMS:
21432143
EXUpdatesInterface: 996527fd7d1a5d271eb523258d603f8f92038f24
21442144
FBLazyVector: 38bb611218305c3bc61803e287b8a81c6f63b619
21452145
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
2146-
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
2146+
glog: 69ef571f3de08433d766d614c73a9838a06bf7eb
21472147
hermes-engine: 3b6e0717ca847e2fc90a201e59db36caf04dee88
2148-
RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47
2148+
RCT-Folly: 4464f4d875961fce86008d45f4ecf6cef6de0740
21492149
RCTDeprecation: 34cbf122b623037ea9facad2e92e53434c5c7422
21502150
RCTRequired: 24c446d7bcd0f517d516b6265d8df04dc3eb1219
21512151
RCTTypeSafety: ef5e91bd791abd3a99b2c75fd565791102a66352

0 commit comments

Comments
 (0)