slight performance improvements

zio/stable
Ansh Nanda 2023-05-23 15:28:46 -07:00
parent b561a51ed9
commit fc9e28ca72
4 changed files with 33 additions and 4 deletions

View File

@ -27,6 +27,7 @@ function genId(): string {
export class LogModel {
entries: LogEntry[] = []
timers = new Map<string, number>()
constructor() {
makeAutoObservable(this)
@ -74,6 +75,21 @@ export class LogModel {
ts: Date.now(),
})
}
time = (label = 'default') => {
this.timers.set(label, performance.now())
}
timeEnd = (label = 'default', warn = false) => {
const endTime = performance.now()
if (this.timers.has(label)) {
const elapsedTime = endTime - this.timers.get(label)!
console.log(`${label}: ${elapsedTime.toFixed(3)}ms`)
this.timers.delete(label)
} else {
warn && console.warn(`Timer with label '${label}' does not exist.`)
}
}
}
function detailsToStr(details?: any) {

View File

@ -292,11 +292,15 @@ export class PreferencesModel {
return res
}
setFeeds(saved: string[], pinned: string[]) {
this.savedFeeds = saved
this.pinnedFeeds = pinned
}
async setSavedFeeds(saved: string[], pinned: string[]) {
const oldSaved = this.savedFeeds
const oldPinned = this.pinnedFeeds
this.savedFeeds = saved
this.pinnedFeeds = pinned
this.setFeeds(saved, pinned)
try {
await this.update((prefs: AppBskyActorDefs.Preferences) => {
const existing = prefs.find(

View File

@ -47,6 +47,10 @@ export class SavedFeedsModel {
return this.feeds.filter(f => !this.isPinned(f))
}
get all() {
return this.pinned.concat(this.unpinned)
}
get pinnedFeedNames() {
return this.pinned.map(f => f.displayName)
}

View File

@ -99,7 +99,7 @@ export const SavedFeeds = withAuthRequired(
/>
<DraggableFlatList
containerStyle={[!isDesktopWeb && s.flex1]}
data={[...savedFeeds.pinned, ...savedFeeds.unpinned]} // make a copy so this FlatList re-renders when pinned changes
data={savedFeeds.all}
keyExtractor={item => item.data.uri}
refreshing={savedFeeds.isRefreshing}
refreshControl={
@ -111,6 +111,11 @@ export const SavedFeeds = withAuthRequired(
/>
}
renderItem={({item, drag}) => <ListItem item={item} drag={drag} />}
getItemLayout={(data, index) => ({
length: 77,
offset: 77 * index,
index,
})}
initialNumToRender={10}
ListFooterComponent={renderListFooterComponent}
ListEmptyComponent={renderListEmptyComponent}
@ -198,7 +203,7 @@ const ListItem = observer(
/>
<TouchableOpacity
accessibilityRole="button"
hitSlop={{top: 10, bottom: 10, left: 10, right: 10}}
hitSlop={10}
onPress={onTogglePinned}>
<FontAwesomeIcon
icon="thumb-tack"