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[]>([])
useEffect(() => {
let aborted = false
store.api.app.bsky
.getUserFollows({
user: store.me.did || '',
})
.then(res => {
if (aborted) return
setFollowedUsers(res.data.follows)
})
return () => {
aborted = true
}
})
const onChangeText = (newText: string) => {

View File

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

View File

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

View File

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

View File

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

View File

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