-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ (synced-prefs) preloading in redux state to improve perf (#3544)
- Loading branch information
1 parent
2e56935
commit 888d2e9
Showing
10 changed files
with
100 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,22 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { useCallback } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { useQuery } from 'loot-core/client/query-hooks'; | ||
import { send } from 'loot-core/platform/client/fetch'; | ||
import { q } from 'loot-core/shared/query'; | ||
import { saveSyncedPrefs } from 'loot-core/client/actions'; | ||
import { type State } from 'loot-core/client/state-types'; | ||
import { type SyncedPrefs } from 'loot-core/src/types/prefs'; | ||
|
||
type SetSyncedPrefsAction = (value: Partial<SyncedPrefs>) => void; | ||
|
||
/** @deprecated: please use `useSyncedPref` (singular) */ | ||
export function useSyncedPrefs(): [SyncedPrefs, SetSyncedPrefsAction] { | ||
const { data: queryData } = useQuery<{ id: string; value: string }[]>( | ||
() => q('preferences').select(['id', 'value']), | ||
[], | ||
const dispatch = useDispatch(); | ||
const setPrefs = useCallback<SetSyncedPrefsAction>( | ||
newValue => { | ||
dispatch(saveSyncedPrefs(newValue)); | ||
}, | ||
[dispatch], | ||
); | ||
|
||
const prefs = useMemo<SyncedPrefs>( | ||
() => | ||
(queryData ?? []).reduce( | ||
(carry, { id, value }) => ({ | ||
...carry, | ||
[id]: value, | ||
}), | ||
{}, | ||
), | ||
[queryData], | ||
); | ||
|
||
const setPrefs = useCallback<SetSyncedPrefsAction>(newValue => { | ||
Object.entries(newValue).forEach(([id, value]) => { | ||
send('preferences/save', { | ||
id: id as keyof SyncedPrefs, | ||
value: String(value), | ||
}); | ||
}); | ||
}, []); | ||
const prefs = useSelector((state: State) => state.prefs.synced); | ||
|
||
return [prefs, setPrefs]; | ||
} |
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
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,6 @@ | ||
--- | ||
category: Maintenance | ||
authors: [MatissJanis] | ||
--- | ||
|
||
SyncedPrefs: preload in redux state and fetch from there; improved performance. |