Fix some useEffect() cleanup issues

zio/stable
Paul Frazee 2022-10-26 14:48:15 -05:00
parent 1983512fef
commit 1ab8285ad3
6 changed files with 38 additions and 2 deletions

View File

@ -33,13 +33,18 @@ export function Component({
const [autocompleteOptions, setAutocompleteOptions] = useState<string[]>([]) const [autocompleteOptions, setAutocompleteOptions] = useState<string[]>([])
useEffect(() => { useEffect(() => {
let aborted = false
store.api.app.bsky store.api.app.bsky
.getUserFollows({ .getUserFollows({
user: store.me.did || '', user: store.me.did || '',
}) })
.then(res => { .then(res => {
if (aborted) return
setFollowedUsers(res.data.follows) setFollowedUsers(res.data.follows)
}) })
return () => {
aborted = true
}
}) })
const onChangeText = (newText: string) => { const onChangeText = (newText: string) => {

View File

@ -29,15 +29,21 @@ export function UserInfoText({
const [didFail, setFailed] = useState<boolean>(false) const [didFail, setFailed] = useState<boolean>(false)
useEffect(() => { useEffect(() => {
let aborted = false
// TODO use caching to reduce loads // TODO use caching to reduce loads
store.api.app.bsky.getProfile({user: did}).then( store.api.app.bsky.getProfile({user: did}).then(
v => { v => {
if (aborted) return
setProfile(v.data) setProfile(v.data)
}, },
_err => { _err => {
if (aborted) return
setFailed(true) setFailed(true)
}, },
) )
return () => {
aborted = true
}
}, [did, store.api.app.bsky]) }, [did, store.api.app.bsky])
return ( return (

View File

@ -24,6 +24,7 @@ export const Home = observer(function Home({
) )
useEffect(() => { useEffect(() => {
let aborted = false
if (!visible) { if (!visible) {
return return
} }
@ -33,7 +34,13 @@ export const Home = observer(function Home({
} else { } else {
store.nav.setTitle('Home') store.nav.setTitle('Home')
console.log('Fetching home feed') console.log('Fetching home feed')
defaultFeedView.setup().then(() => setHasSetup(true)) defaultFeedView.setup().then(() => {
if (aborted) return
setHasSetup(true)
})
}
return () => {
aborted = true
} }
}, [visible, store]) }, [visible, store])

View File

@ -214,21 +214,27 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
const [username, setUsername] = useState<string>('') const [username, setUsername] = useState<string>('')
useEffect(() => { useEffect(() => {
let aborted = false
if (serviceDescription || error) { if (serviceDescription || error) {
return return
} }
store.session.describeService('http://localhost:2583/').then( store.session.describeService('http://localhost:2583/').then(
desc => { desc => {
if (aborted) return
setServiceDescription(desc) setServiceDescription(desc)
setUserDomain(desc.availableUserDomains[0]) setUserDomain(desc.availableUserDomains[0])
}, },
err => { err => {
if (aborted) return
console.error(err) console.error(err)
setError( setError(
'Unable to contact your service. Please check your Internet connection.', 'Unable to contact your service. Please check your Internet connection.',
) )
}, },
) )
return () => {
aborted = true
}
}, []) }, [])
const onPressNext = async () => { const onPressNext = async () => {

View File

@ -13,6 +13,7 @@ export const Notifications = ({visible}: ScreenParams) => {
const store = useStores() const store = useStores()
useEffect(() => { useEffect(() => {
let aborted = false
if (!visible) { if (!visible) {
return return
} }
@ -24,7 +25,13 @@ export const Notifications = ({visible}: ScreenParams) => {
console.log('Fetching notifications feed') console.log('Fetching notifications feed')
const newNotesView = new NotificationsViewModel(store, {}) const newNotesView = new NotificationsViewModel(store, {})
setNotesView(newNotesView) setNotesView(newNotesView)
newNotesView.setup().then(() => setHasSetup(true)) newNotesView.setup().then(() => {
if (aborted) return
setHasSetup(true)
})
}
return () => {
aborted = true
} }
}, [visible, store]) }, [visible, store])

View File

@ -23,6 +23,7 @@ export const Profile = observer(({visible, params}: ScreenParams) => {
>() >()
useEffect(() => { useEffect(() => {
let aborted = false
if (!visible) { if (!visible) {
return return
} }
@ -36,9 +37,13 @@ export const Profile = observer(({visible, params}: ScreenParams) => {
const newProfileUiState = new ProfileUiModel(store, {user}) const newProfileUiState = new ProfileUiModel(store, {user})
setProfileUiState(newProfileUiState) setProfileUiState(newProfileUiState)
newProfileUiState.setup().then(() => { newProfileUiState.setup().then(() => {
if (aborted) return
setHasSetup(true) setHasSetup(true)
}) })
} }
return () => {
aborted = true
}
}, [visible, params.name, store]) }, [visible, params.name, store])
// events // events