-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: RN specific GC sweep prioritizer
- Loading branch information
Showing
12 changed files
with
214 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/react/src/__tests__/integration-garbage-collection.native.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { | ||
AsyncBoundary, | ||
DataProvider, | ||
GCPolicy, | ||
useSuspense, | ||
} from '@data-client/react'; | ||
import { MockResolver } from '@data-client/test'; | ||
import { render, screen, act } from '@testing-library/react-native'; | ||
import { ArticleResource } from '__tests__/new'; | ||
import { useState } from 'react'; | ||
import '@testing-library/jest-native'; | ||
import { View, Text, Button, TouchableOpacity } from 'react-native'; | ||
|
||
const mockGetList = jest.fn(); | ||
const mockGet = jest.fn(); | ||
const GC_INTERVAL = 5000; | ||
|
||
const ListView = ({ onSelect }: { onSelect: (id: number) => void }) => { | ||
const articles = useSuspense(ArticleResource.getList); | ||
return ( | ||
<View> | ||
{articles.map(article => ( | ||
<TouchableOpacity | ||
key={article.id} | ||
onPress={() => onSelect(article.id ?? 0)} | ||
> | ||
<Text>{article.title}</Text> | ||
</TouchableOpacity> | ||
))} | ||
</View> | ||
); | ||
}; | ||
|
||
const DetailView = ({ id }: { id: number }) => { | ||
const article = useSuspense(ArticleResource.get, { id }); | ||
const [toggle, setToggle] = useState(false); | ||
|
||
return ( | ||
<View> | ||
<Text>{article.title}</Text> | ||
<Text>{article.content}</Text> | ||
<Button title="Toggle Re-render" onPress={() => setToggle(!toggle)} /> | ||
{toggle && <Text>Toggle state: {toggle.toString()}</Text>} | ||
</View> | ||
); | ||
}; | ||
|
||
const TestComponent = () => { | ||
const [view, setView] = useState<'list' | 'detail'>('list'); | ||
const [selectedId, setSelectedId] = useState<number | null>(null); | ||
|
||
return ( | ||
<DataProvider gcPolicy={new GCPolicy({ intervalMS: GC_INTERVAL })}> | ||
<MockResolver | ||
fixtures={[ | ||
{ | ||
endpoint: ArticleResource.getList, | ||
response: mockGetList, | ||
delay: 100, | ||
}, | ||
{ endpoint: ArticleResource.get, response: mockGet, delay: 100 }, | ||
]} | ||
> | ||
<TouchableOpacity onPress={() => setView('list')}> | ||
<Text>Home</Text> | ||
</TouchableOpacity> | ||
<AsyncBoundary fallback={<Text>Loading...</Text>}> | ||
{view === 'list' ? | ||
<ListView | ||
onSelect={id => { | ||
setSelectedId(id); | ||
setView('detail'); | ||
}} | ||
/> | ||
: selectedId !== null && <DetailView id={selectedId} />} | ||
</AsyncBoundary> | ||
</MockResolver> | ||
</DataProvider> | ||
); | ||
}; | ||
|
||
// Test cases | ||
describe('Integration Garbage Collection React Native', () => { | ||
it('should render list view and detail view correctly', async () => { | ||
jest.useFakeTimers(); | ||
mockGetList.mockResolvedValue([ | ||
{ id: 1, title: 'Article 1', content: 'Content 1' }, | ||
{ id: 2, title: 'Article 2', content: 'Content 2' }, | ||
]); | ||
mockGet.mockResolvedValue({ | ||
id: 1, | ||
title: 'Article 1', | ||
content: 'Content 1', | ||
}); | ||
|
||
render(<TestComponent />); | ||
|
||
// Initial render, should show list view | ||
expect(await screen.findByText('Article 1')).toBeTruthy(); | ||
|
||
// Switch to detail view | ||
act(() => { | ||
screen.getByText('Article 1').props.onPress(); | ||
}); | ||
|
||
// Detail view should render | ||
expect(await screen.findByText('Content 1')).toBeTruthy(); | ||
|
||
// Jest time pass to trigger sweep but not expired | ||
act(() => { | ||
jest.advanceTimersByTime(GC_INTERVAL); | ||
}); | ||
|
||
// Switch back to list view | ||
act(() => { | ||
screen.getByText('Home').props.onPress(); | ||
}); | ||
|
||
// List view should instantly render | ||
expect(await screen.findByText('Article 1')).toBeTruthy(); | ||
|
||
// Switch back to detail view | ||
act(() => { | ||
screen.getByText('Article 1').props.onPress(); | ||
}); | ||
|
||
// Jest time pass to expiry | ||
act(() => { | ||
jest.advanceTimersByTime( | ||
ArticleResource.getList.dataExpiryLength ?? 60000, | ||
); | ||
}); | ||
expect(await screen.findByText('Content 1')).toBeTruthy(); | ||
|
||
// Re-render detail view to make sure it still renders | ||
act(() => { | ||
screen.getByText('Toggle Re-render').props.onPress(); | ||
}); | ||
expect(await screen.findByText('Toggle state: true')).toBeTruthy(); | ||
expect(await screen.findByText('Content 1')).toBeTruthy(); | ||
|
||
// Visit list view and see suspense fallback | ||
act(() => { | ||
screen.getByText('Home').props.onPress(); | ||
}); | ||
|
||
expect(screen.getByText('Loading...')).toBeTruthy(); | ||
jest.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
packages/react/src/components/__tests__/__snapshots__/provider.node.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { GCPolicy } from '@data-client/core'; | ||
import { InteractionManager } from 'react-native'; | ||
|
||
/** Can help prevent stuttering by waiting for idle before performing GC sweeps */ | ||
export default class NativeGCPolicy extends GCPolicy { | ||
/** Calls the callback when client is not 'busy' with high priority interaction tasks | ||
* | ||
* Override for platform-specific implementations | ||
*/ | ||
protected idleCallback( | ||
callback: (...args: any[]) => void, | ||
options?: IdleRequestOptions, | ||
) { | ||
InteractionManager.runAfterInteractions(callback); | ||
if (options?.timeout) { | ||
InteractionManager.setDeadline(options.timeout); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GCPolicy as default } from '@data-client/core'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters