Skip to content

Commit 8bfb429

Browse files
committed
Add error handling for getAll() failure during init
1 parent ba345a2 commit 8bfb429

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -906,41 +906,53 @@ function initializeWithDefaultKeyStates(): Promise<void> {
906906
// This is faster than lazy-loading individual keys because:
907907
// 1. One DB transaction instead of hundreds
908908
// 2. All subsequent reads are synchronous cache hits
909-
return Storage.getAll().then((pairs) => {
910-
const allDataFromStorage: Record<string, unknown> = {};
911-
912-
for (const [key, value] of pairs) {
913-
// RAM-only keys should never be loaded from storage as they may have stale persisted data
914-
// from before the key was migrated to RAM-only.
915-
if (isRamOnlyKey(key)) {
916-
continue;
909+
return Storage.getAll()
910+
.then((pairs) => {
911+
const allDataFromStorage: Record<string, unknown> = {};
912+
for (const [key, value] of pairs) {
913+
// RAM-only keys should never be loaded from storage as they may have stale persisted data
914+
// from before the key was migrated to RAM-only.
915+
if (isRamOnlyKey(key)) {
916+
continue;
917+
}
918+
allDataFromStorage[key] = value;
917919
}
918-
allDataFromStorage[key] = value;
919-
}
920920

921-
// Load all storage data into cache silently (no subscriber notifications)
922-
cache.setAllKeys(Object.keys(allDataFromStorage));
923-
cache.merge(allDataFromStorage);
921+
// Load all storage data into cache silently (no subscriber notifications)
922+
cache.setAllKeys(Object.keys(allDataFromStorage));
923+
cache.merge(allDataFromStorage);
924924

925-
// Extract only the default key states from storage and merge with defaults
926-
const defaultKeysFromStorage = Object.keys(defaultKeyStates).reduce((obj: Record<string, unknown>, key) => {
927-
if (key in allDataFromStorage) {
928-
// eslint-disable-next-line no-param-reassign
929-
obj[key] = allDataFromStorage[key];
930-
}
931-
return obj;
932-
}, {});
925+
// Extract only the default key states from storage and merge with defaults
926+
const defaultKeysFromStorage = Object.keys(defaultKeyStates).reduce((obj: Record<string, unknown>, key) => {
927+
if (key in allDataFromStorage) {
928+
// eslint-disable-next-line no-param-reassign
929+
obj[key] = allDataFromStorage[key];
930+
}
931+
return obj;
932+
}, {});
933933

934-
const merged = utils.fastMerge(defaultKeysFromStorage, defaultKeyStates, {
935-
shouldRemoveNestedNulls: true,
936-
}).result;
937-
cache.merge(merged ?? {});
934+
const merged = utils.fastMerge(defaultKeysFromStorage, defaultKeyStates, {
935+
shouldRemoveNestedNulls: true,
936+
}).result;
937+
cache.merge(merged ?? {});
938938

939-
// Only notify subscribers for default key states — same as before.
940-
// Other keys will be picked up by subscribers when they connect.
941-
// FIXME: Maybe we dont need this, but some tests in E/App are failing if we remove it.
942-
for (const [key, value] of Object.entries(merged ?? {})) keyChanged(key, value);
943-
});
939+
// Only notify subscribers for default key states — same as before.
940+
// Other keys will be picked up by subscribers when they connect.
941+
// FIXME: Maybe we dont need this, but some tests in E/App are failing if we remove it.
942+
for (const [key, value] of Object.entries(merged ?? {})) keyChanged(key, value);
943+
})
944+
.catch((error) => {
945+
Logger.logAlert(`Failed to load data from storage during init. The app will boot with default key states only. Error: ${error}`);
946+
947+
// Populate the key index so getAllKeys() returns correct results for default keys.
948+
// Without this, subscribers that check getAllKeys() would see an empty set even
949+
// though we have default values in cache.
950+
cache.setAllKeys(Object.keys(defaultKeyStates));
951+
952+
// Boot with defaults so the app renders instead of deadlocking.
953+
// Users will get a fresh-install experience but the app won't be bricked.
954+
cache.merge(defaultKeyStates);
955+
});
944956
}
945957

946958
/**

0 commit comments

Comments
 (0)