fix(mobile): trigger timeline twice if scroll to change page view

- Added EventBus integration to handle timeline selection in `atoms.ts` and `PageList.tsx`.
- Updated `selectTimeline` function to dispatch events for better state management.
- Specified `react-native` as a peer dependency in `package.json` to ensure compatibility.
- Updated `pnpm-lock.yaml` to reflect the new version of `react-native`.

These changes enhance the responsiveness of the UI and improve the overall architecture of the event handling system.
This commit is contained in:
Innei
2025-04-01 18:48:07 +08:00
parent 93393f19fc
commit f3fee3676f
6 changed files with 60 additions and 7 deletions
View File
+9 -4
View File
@@ -1,4 +1,5 @@
import type { FeedViewType } from "@follow/constants"
import { EventBus } from "@follow/utils/src/event-bus"
import * as Haptics from "expo-haptics"
import { useEffect, useMemo } from "react"
import type { StyleProp, ViewStyle } from "react-native"
@@ -24,18 +25,22 @@ export function PagerList({
const viewId = selectedFeed?.type === "view" ? selectedFeed.viewId : undefined
const activeViews = useViewWithSubscription()
const viewIdIndex = activeViews.findIndex((view) => view.view === viewId)
const { page, pagerRef, ...rest } = usePagerView({
initialPage: viewIdIndex,
onIndexChange: (index) => {
selectTimeline({ type: "view", viewId: activeViews[index]!.view })
selectTimeline({ type: "view", viewId: activeViews[index]!.view }, false)
},
})
useEffect(() => {
if (page === viewIdIndex) return
pagerRef.current?.setPage(viewIdIndex)
}, [page, pagerRef, viewIdIndex])
return EventBus.subscribe("SELECT_TIMELINE", (data) => {
if (data.manual) {
pagerRef.current?.setPage(data.view.viewId)
}
})
}, [pagerRef])
return (
<AnimatedPagerView
+15 -2
View File
@@ -1,5 +1,6 @@
import { FeedViewType } from "@follow/constants"
import { jotaiStore } from "@follow/utils"
import { EventBus } from "@follow/utils/src/event-bus"
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai"
import { createContext, useCallback, useContext, useMemo } from "react"
@@ -12,7 +13,6 @@ import { useInbox } from "@/src/store/inbox/hooks"
import { useList } from "@/src/store/list/hooks"
import { getSubscriptionByCategory } from "@/src/store/subscription/getter"
import { useSubscription } from "@/src/store/subscription/hooks"
// drawer open state
const drawerOpenAtom = atom<boolean>(false)
@@ -215,8 +215,21 @@ export const useSelectedFeedTitle = () => {
}
}
export const selectTimeline = (state: SelectedTimeline) => {
declare module "@follow/utils/src/event-bus" {
export interface CustomEvent {
SELECT_TIMELINE: {
view: SelectedTimeline
manual: boolean
}
}
}
export const selectTimeline = (state: SelectedTimeline, manual = true) => {
jotaiStore.set(selectedTimelineAtom, state)
EventBus.dispatch("SELECT_TIMELINE", {
view: state,
manual,
})
}
export const selectFeed = (state: SelectedFeed) => {
+4 -1
View File
@@ -27,6 +27,9 @@
"test": "vitest",
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
"react-native": ">=0.74.0"
},
"dependencies": {
"@follow/shared": "workspace:*",
"@follow/types": "workspace:*",
@@ -42,4 +45,4 @@
"tldts": "6.1.85",
"uniqolor": "1.1.1"
}
}
}
+29
View File
@@ -0,0 +1,29 @@
import { DeviceEventEmitter } from "react-native"
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface CustomEvent {}
export interface EventBusMap extends CustomEvent {}
type AnyObject = Record<string, any>
class EventBusStatic<E extends AnyObject> {
dispatch<T extends keyof E>(event: T, data: E[T]): void
dispatch<T extends keyof E>(event: T): void
dispatch<T extends keyof E>(event: T, data?: E[T]) {
DeviceEventEmitter.emit(event as string, data)
}
subscribe<T extends keyof E>(event: T, callback: (data: E[T]) => void) {
const subscription = DeviceEventEmitter.addListener(event as string, callback)
return () => subscription.remove()
}
unsubscribe(_event: string, _callback: (data: any) => void) {
// DeviceEventEmitter doesn't have a direct method to remove a specific listener
// This is handled by the subscription.remove() returned by subscribe
// This method is kept for API compatibility
}
}
export const EventBus = new EventBusStatic<EventBusMap>()
export const createEventBus = <E extends AnyObject>() => new EventBusStatic<E>()
+3
View File
@@ -1581,6 +1581,9 @@ importers:
path-to-regexp:
specifier: 8.2.0
version: 8.2.0
react-native:
specifier: '>=0.74.0'
version: 0.77.2(@babel/core@7.26.10)(@babel/preset-env@7.26.9(@babel/core@7.26.10))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)
tailwind-merge:
specifier: 3.0.2
version: 3.0.2