Skip to content

Commit

Permalink
perf(example): add log sink and some new cases
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed Nov 29, 2023
1 parent 0764942 commit 77a5ba0
Show file tree
Hide file tree
Showing 13 changed files with 1,708 additions and 262 deletions.
5 changes: 3 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Advanced from './advanced';
import Basic from './basic';
import Client from './components/Client';
const RootStack = createStackNavigator<any>();
// setDebuggable(!isDebuggable());
const DATA = [Basic, Advanced];

export default function App() {
Expand Down Expand Up @@ -70,7 +69,9 @@ export default function App() {
onPress={() => {
setDebuggable(!isDebuggable());
}}
/>
>
<Text style={styles.version}>Powered by Agora RTM SDK</Text>
</TouchableOpacity>
</SafeAreaView>
</NavigationContainer>
);
Expand Down
91 changes: 24 additions & 67 deletions example/src/advanced/ChannelMetadata/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
IStreamChannel,
JoinChannelOptions,
MetadataItem,
MetadataOptions,
RTM_CHANNEL_TYPE,
Expand All @@ -22,8 +20,7 @@ import * as log from '../../utils/log';

export default function ChannelMetadata() {
const [loginSuccess, setLoginSuccess] = useState(false);
const [joinSuccess, setJoinSuccess] = useState(false);
const [streamChannel, setStreamChannel] = useState<IStreamChannel>();
const [subscribeSuccess, setSubscribeSuccess] = useState(false);
const [cName, setCName] = useState<string>(Config.channelName);
const getChannelMetadataRequestId = useRef<number>();
const setChannelMetadataRequestId = useRef<number>();
Expand All @@ -41,25 +38,18 @@ export default function ChannelMetadata() {
})
);

const onJoinResult = useCallback(
(
requestId: number,
channelName: string,
userId: string,
errorCode: RTM_ERROR_CODE
) => {
log.info(
'onJoinResult',
const onSubscribeResult = useCallback(
(requestId: number, channelName: string, errorCode: RTM_ERROR_CODE) => {
log.log(
'onSubscribeResult',
'requestId',
requestId,
'channelName',
channelName,
'userId',
userId,
'errorCode',
errorCode
);
setJoinSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
setSubscribeSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
},
[]
);
Expand Down Expand Up @@ -196,47 +186,24 @@ export default function ChannelMetadata() {
const client = useRtmClient();

/**
* Step 1-1 : createStreamChannel
*/
const createStreamChannel = () => {
if (joinSuccess) {
log.error('already joined channel');
return;
}
let result = client.createStreamChannel(cName);
setStreamChannel(result);
};

/**
* Step 1-2 : join message channel
* Step 1-1(optional) : subscribe message channel
*/
const join = () => {
if (!streamChannel) {
log.error('please create streamChannel first');
return;
}
streamChannel.join(
new JoinChannelOptions({ token: Config.appId, withMetadata: true })
);
const subscribe = () => {
client.subscribe(Config.channelName, {
withMessage: true,
withMetadata: true,
withPresence: true,
});
};

/**
* Step 1-3 : leave message channel
* Step 1-2 : unsubscribe message channel
*/
const leave = () => {
if (streamChannel) {
streamChannel.leave(0);
}
const unsubscribe = () => {
client.unsubscribe(Config.channelName);
setSubscribeSuccess(false);
};

/**
* Step 1-4 : destroyStreamChannel
*/
const destroyStreamChannel = useCallback(() => {
streamChannel?.release();
setStreamChannel(undefined);
}, [streamChannel]);

/**
* Step 2 : setChannelMetadata
*/
Expand Down Expand Up @@ -316,7 +283,7 @@ export default function ChannelMetadata() {
};

useEffect(() => {
client.addEventListener('onJoinResult', onJoinResult);
client.addEventListener('onSubscribeResult', onSubscribeResult);
client.addEventListener(
'onSetChannelMetadataResult',
onSetChannelMetadataResult
Expand All @@ -336,7 +303,7 @@ export default function ChannelMetadata() {
client?.addEventListener('onStorageEvent', onStorageEvent);

return () => {
client.removeEventListener('onJoinResult', onJoinResult);
client.removeEventListener('onSubscribeResult', onSubscribeResult);
client.removeEventListener(
'onSetChannelMetadataResult',
onSetChannelMetadataResult
Expand All @@ -358,7 +325,7 @@ export default function ChannelMetadata() {
}, [
client,
uid,
onJoinResult,
onSubscribeResult,
onSetChannelMetadataResult,
onGetChannelMetadataResult,
onRemoveChannelMetadataResult,
Expand Down Expand Up @@ -391,13 +358,12 @@ export default function ChannelMetadata() {
RTM_CONNECTION_CHANGE_REASON.RTM_CONNECTION_CHANGED_LOGOUT
) {
setLoginSuccess(false);
destroyStreamChannel();
}
setJoinSuccess(false);
setSubscribeSuccess(false);
break;
}
},
[destroyStreamChannel]
[]
);
useEffect(() => {
client?.addEventListener(
Expand All @@ -422,18 +388,9 @@ export default function ChannelMetadata() {
/>
<AgoraButton
disabled={!loginSuccess}
title={`${
streamChannel ? 'destroyStreamChannel' : 'createStreamChannel'
}`}
onPress={() => {
streamChannel ? destroyStreamChannel() : createStreamChannel();
}}
/>
<AgoraButton
disabled={!loginSuccess || !streamChannel}
title={`${joinSuccess ? 'leaveChannel' : 'joinChannel'}`}
title={`${subscribeSuccess ? 'unsubscribe' : 'subscribe'}`}
onPress={() => {
joinSuccess ? leave() : join();
subscribeSuccess ? unsubscribe() : subscribe();
}}
/>
<AgoraTextInput
Expand Down
101 changes: 29 additions & 72 deletions example/src/advanced/Lock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
IStreamChannel,
JoinChannelOptions,
LockDetail,
RTM_CHANNEL_TYPE,
RTM_CONNECTION_CHANGE_REASON,
Expand All @@ -24,8 +22,7 @@ import * as log from '../../utils/log';

export default function Lock() {
const [loginSuccess, setLoginSuccess] = useState(false);
const [joinSuccess, setJoinSuccess] = useState(false);
const [streamChannel, setStreamChannel] = useState<IStreamChannel>();
const [subscribeSuccess, setSubscribeSuccess] = useState(false);
const [cName, setCName] = useState<string>(Config.channelName);
const acquireLockRequestId = useRef<number>();
const setLockRequestId = useRef<number>();
Expand All @@ -39,25 +36,18 @@ export default function Lock() {
const [lockName, setLockName] = useState<string>('lock-test');
const [ttl, setTtl] = useState<number>(10);

const onJoinResult = useCallback(
(
requestId: number,
channelName: string,
userId: string,
errorCode: RTM_ERROR_CODE
) => {
log.info(
'onJoinResult',
const onSubscribeResult = useCallback(
(requestId: number, channelName: string, errorCode: RTM_ERROR_CODE) => {
log.log(
'onSubscribeResult',
'requestId',
requestId,
'channelName',
channelName,
'userId',
userId,
'errorCode',
errorCode
);
setJoinSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
setSubscribeSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
},
[]
);
Expand Down Expand Up @@ -87,7 +77,7 @@ export default function Lock() {
errorDetails
);
if (errorCode !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error(`acquire lock failed`, `errorCode: ${errorCode}`);
log.error(`acquire lock failed: errorCode: ${errorCode}`);
}
},
[]
Expand Down Expand Up @@ -141,7 +131,7 @@ export default function Lock() {
errorCode
);
if (errorCode !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error(`setLock failed`, `errorCode: ${errorCode}`);
log.error(`setLock failed: errorCode: ${errorCode}`);
}
},
[]
Expand Down Expand Up @@ -169,7 +159,7 @@ export default function Lock() {
errorCode
);
if (errorCode !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error(`revokeLock failed`, `errorCode: ${errorCode}`);
log.error(`revokeLock failed: errorCode: ${errorCode}`);
}
},
[]
Expand Down Expand Up @@ -197,7 +187,7 @@ export default function Lock() {
errorCode
);
if (errorCode !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error(`removeLock failed`, `errorCode: ${errorCode}`);
log.error(`removeLock failed: errorCode: ${errorCode}`);
}
},
[]
Expand Down Expand Up @@ -243,49 +233,26 @@ export default function Lock() {
const client = useRtmClient();

/**
* Step 1-1 : createStreamChannel
*/
const createStreamChannel = () => {
if (joinSuccess) {
log.error('already joined channel');
return;
}
let result = client.createStreamChannel(cName);
setStreamChannel(result);
};

/**
* Step 1-2 : join message channel
* Step 1-1(optional) : subscribe message channel
*/
const join = () => {
if (!streamChannel) {
log.error('please create streamChannel first');
return;
}
streamChannel.join(
new JoinChannelOptions({ token: Config.appId, withMetadata: true })
);
const subscribe = () => {
client.subscribe(Config.channelName, {
withMessage: true,
withMetadata: true,
withPresence: true,
});
};

/**
* Step 1-3 : leave message channel
* Step 1-2 : unsubscribe message channel
*/
const leave = () => {
if (streamChannel) {
streamChannel.leave(0);
}
const unsubscribe = () => {
client.unsubscribe(Config.channelName);
setSubscribeSuccess(false);
};

/**
* Step 1-4 : destroyStreamChannel
*/
const destroyStreamChannel = useCallback(() => {
streamChannel?.release();
setStreamChannel(undefined);
}, [streamChannel]);

/**
* Step 1-4 : getLocks
* Step 1-3 : getLocks
*/
const getLocks = () => {
getLocksRequestId.current = client
Expand Down Expand Up @@ -349,7 +316,7 @@ export default function Lock() {
};

useEffect(() => {
client.addEventListener('onJoinResult', onJoinResult);
client.addEventListener('onSubscribeResult', onSubscribeResult);
client.addEventListener('onSetLockResult', onSetLockResult);
client?.addEventListener('onAcquireLockResult', onAcquireLockResult);
client?.addEventListener('onReleaseLockResult', onReleaseLockResult);
Expand All @@ -358,7 +325,7 @@ export default function Lock() {
client?.addEventListener('onGetLocksResult', onGetLocksResult);

return () => {
client.removeEventListener('onJoinResult', onJoinResult);
client.removeEventListener('onSubscribeResult', onSubscribeResult);
client.removeEventListener('onSetLockResult', onSetLockResult);
client?.removeEventListener('onAcquireLockResult', onAcquireLockResult);
client?.removeEventListener('onReleaseLockResult', onReleaseLockResult);
Expand All @@ -369,7 +336,7 @@ export default function Lock() {
}, [
client,
uid,
onJoinResult,
onSubscribeResult,
onSetLockResult,
onAcquireLockResult,
onReleaseLockResult,
Expand Down Expand Up @@ -403,13 +370,12 @@ export default function Lock() {
RTM_CONNECTION_CHANGE_REASON.RTM_CONNECTION_CHANGED_LOGOUT
) {
setLoginSuccess(false);
destroyStreamChannel();
}
setJoinSuccess(false);
setSubscribeSuccess(false);
break;
}
},
[destroyStreamChannel]
[]
);
useEffect(() => {
client?.addEventListener(
Expand All @@ -434,18 +400,9 @@ export default function Lock() {
/>
<AgoraButton
disabled={!loginSuccess}
title={`${
streamChannel ? 'destroyStreamChannel' : 'createStreamChannel'
}`}
onPress={() => {
streamChannel ? destroyStreamChannel() : createStreamChannel();
}}
/>
<AgoraButton
disabled={!loginSuccess || !streamChannel}
title={`${joinSuccess ? 'leaveChannel' : 'joinChannel'}`}
title={`${subscribeSuccess ? 'unsubscribe' : 'subscribe'}`}
onPress={() => {
joinSuccess ? leave() : join();
subscribeSuccess ? unsubscribe() : subscribe();
}}
/>
<AgoraTextInput
Expand Down
Loading

0 comments on commit 77a5ba0

Please sign in to comment.