forked from Ivasoft/mattermost-mobile
Add minor fixes and performance improvements on channel switch (#6469)
* Add minor fixes and performance improvements * Add comment
This commit is contained in:
committed by
GitHub
parent
52d5f903f3
commit
3abaf8893d
33
app/hooks/channel_switch.ts
Normal file
33
app/hooks/channel_switch.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import {useEffect, useState} from 'react';
|
||||
import {DeviceEventEmitter} from 'react-native';
|
||||
|
||||
import {Events} from '@constants';
|
||||
|
||||
export const useChannelSwitch = () => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
useEffect(() => {
|
||||
let time: NodeJS.Timeout | undefined;
|
||||
const l = DeviceEventEmitter.addListener(Events.CHANNEL_SWITCH, (switching: boolean) => {
|
||||
if (time) {
|
||||
clearTimeout(time);
|
||||
}
|
||||
if (switching) {
|
||||
setLoading(true);
|
||||
} else {
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
time = setTimeout(() => setLoading(false), 0);
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
l.remove();
|
||||
if (time) {
|
||||
clearTimeout(time);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return loading;
|
||||
};
|
||||
Reference in New Issue
Block a user