From eac21844c4aace0e1c2ed3d4e2b216826e7bd16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 30 Nov 2023 00:54:25 +0100 Subject: [PATCH] Moved PopupCC in the dom and Removed react context in hopes to get better perf --- front/components/Play/PartitionMagic.tsx | 7 +- front/views/PlayView.tsx | 112 +++++++++++------------ 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/front/components/Play/PartitionMagic.tsx b/front/components/Play/PartitionMagic.tsx index 7e86ebb..ddebb64 100644 --- a/front/components/Play/PartitionMagic.tsx +++ b/front/components/Play/PartitionMagic.tsx @@ -12,6 +12,7 @@ import LoadingComponent from '../Loading'; // note we are also using timestamp in a context export type ParitionMagicProps = { + timestamp: number; songID: number; onEndReached: () => void; onError: (err: string) => void; @@ -39,13 +40,13 @@ const getCursorToPlay = ( } }; -const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagicProps) => { +const PartitionMagic = ({ timestamp, songID, onEndReached, onError, onReady }: ParitionMagicProps) => { const { data, isLoading, isError } = useQuery(API.getSongCursorInfos(songID)); const currentCurIdx = React.useRef(-1); const [endPartitionReached, setEndPartitionReached] = React.useState(false); const [isPartitionSvgLoaded, setIsPartitionSvgLoaded] = React.useState(false); const partitionOffset = useSharedValue(0); - const pianoCC = React.useContext(PianoCC); + // const pianoCC = React.useContext(PianoCC); const pianoSounds = React.useRef | null>(null); const cursorPaddingVertical = 10; const cursorPaddingHorizontal = 3; @@ -110,7 +111,7 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi getCursorToPlay( data?.cursors ?? [], currentCurIdx.current, - pianoCC.timestamp - transitionDuration, + timestamp - transitionDuration, (cursor, idx) => { currentCurIdx.current = idx; if (pianoSounds.current) { diff --git a/front/views/PlayView.tsx b/front/views/PlayView.tsx index 7c967f9..d0d4455 100644 --- a/front/views/PlayView.tsx +++ b/front/views/PlayView.tsx @@ -21,7 +21,7 @@ import { RootState } from '../state/Store'; import { Translate, translate } from '../i18n/i18n'; import { ColorSchemeType } from 'native-base/lib/typescript/components/types'; import { useStopwatch } from 'react-use-precision-timer'; -import { MIDIAccess, MIDIMessageEvent, requestMIDIAccess } from '@arthi-chaud/react-native-midi'; +// import { MIDIAccess, MIDIMessageEvent, requestMIDIAccess } from '@arthi-chaud/react-native-midi'; import * as Linking from 'expo-linking'; import url from 'url'; import { PianoCanvasContext } from '../models/PianoGame'; @@ -68,15 +68,8 @@ function parseMidiMessage(message: MIDIMessageEvent) { }; } -//create a context with an array of number -export const PianoCC = createContext({ - pressedKeys: new Map(), - timestamp: 0, - messages: [], -}); - const PlayView = ({ songId, route }: RouteProps) => { - const [type, setType] = useState<'practice' | 'normal'>(); + const [playType, setPlayType] = useState<'practice' | 'normal' | null>(null); const accessToken = useSelector((state: RootState) => state.user.accessToken); const navigation = useNavigation(); const screenSize = useBreakpointValue({ base: 'small', md: 'big' }); @@ -111,6 +104,7 @@ const PlayView = ({ songId, route }: RouteProps) => { const statColor = colors.lightText; const onPause = () => { + console.log('onPause'); stopwatch.pause(); setPause(true); webSocket.current?.send( @@ -165,7 +159,7 @@ const PlayView = ({ songId, route }: RouteProps) => { JSON.stringify({ type: 'start', id: song.data!.id, - mode: type, + mode: playType, bearer: accessToken, }) ); @@ -266,7 +260,7 @@ const PlayView = ({ songId, route }: RouteProps) => { ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE).catch(() => {}); const interval = setInterval(() => { setTime(() => getElapsedTime()); // Countdown - }, 1); + }, 300); return () => { ScreenOrientation.unlockAsync().catch(() => {}); @@ -297,10 +291,10 @@ const PlayView = ({ songId, route }: RouteProps) => { if (navigation.getState().routes.at(-1)?.name != route.name) { return; } - if (song.data && !webSocket.current && partitionRendered) { - requestMIDIAccess().then(onMIDISuccess).catch(onMIDIFailure); + if (song.data && !webSocket.current) { + // requestMIDIAccess().then(onMIDISuccess).catch(onMIDIFailure); } - }, [song.data, partitionRendered]); + }, [song.data]); if (!song.data) { return ; @@ -332,36 +326,6 @@ const PlayView = ({ songId, route }: RouteProps) => { zIndex: 100, }} > - { - if (!isVisible) { - // If we dismiss the popup, Go to previous page - navigation.goBack(); - } - } - : undefined - } - > - - setType('practice')} - /> - setType('normal')} - /> - - {( [ [ @@ -461,34 +425,60 @@ const PlayView = ({ songId, route }: RouteProps) => { backgroundColor: 'white', }} > - setPartitionRendered(true)} + onEndReached={() => { + setTimeout(() => { + onEnd(); + }, 500); }} - > - setPartitionRendered(true)} - onEndReached={onEnd} - onError={() => { - console.log('error from partition magic'); - }} - /> - - {!partitionRendered && } + onError={() => { + console.log('error from partition magic'); + }} + /> + { + if (!isVisible) { + // If we dismiss the popup, Go to previous page + navigation.goBack(); + } + } + : undefined + } + > + + setPlayType('practice')} + /> + setPlayType('normal')} + /> + + {colorScheme === 'dark' && (