Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate all AsyncStorage direct calls to our storage wrapper #1169

Merged
merged 18 commits into from
Feb 23, 2024

Conversation

zatteo
Copy link
Contributor

@zatteo zatteo commented Feb 16, 2024

### 🔧 Tech

* Migrate all AsyncStorage direct calls to our storage wrapper

Now every react-native-async-storage calls are centralized in our storage wrapper. This is a prerequisite to potentially migrate to react-native-mmkv or another library.

Some cases I encountered

  • a JSON object is stored stringified with AsyncStorage.setItem and is parsed after AsyncStorage.getItem
    => because getData and storeData already does the stringify/parse, I removed the remaining JSON.stringify and JSON.parse calls when migrating to getData and storeData. Ex. : Bundle, Oauth
  • a string is stored directly with AsyncStorage.setItem
    => getData will try to parse a not stringified string if there is already a value stored directly with AsyncStorage.setItem. I added a compatibilty check. Ex. : DefaultRedirectionUrl

What did I test

  • Logging in with previous app version (master) ; migrate to this version; use the app
  • Logging in with this version; use the app

Checklist

Before merging this PR, the following things must have been done if relevant:

  • Tested on iOS
  • Tested on Android
  • Test coverage
  • README and documentation

@zatteo zatteo force-pushed the refactor/use-storage-wrapper branch from bd26fc5 to 1db64a8 Compare February 16, 2024 11:15
@zatteo zatteo changed the title Refactor/use storage wrapper Migrate all AsyncStorage direct calls to our storage wrapper Feb 16, 2024
@zatteo zatteo marked this pull request as ready for review February 16, 2024 11:25
Copy link
Member

@Ldoppea Ldoppea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this simplifies a lot the API and this seems to be retro-compatible 👍

Note to myself: update my "log to files" PR #1137 to use storage API

src/libs/localStore/storage.ts Outdated Show resolved Hide resolved
src/libs/localStore/storage.ts Outdated Show resolved Hide resolved
@@ -66,8 +64,6 @@ export const asyncLogout = async (client?: CozyClient): Promise<null> => {

await sendKonnectorsLogs(client)
await client.logout()
await clearClient()
await resetSessionToken()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like resetSessionToken is not called anymore so we can remove the method implementation?

clearClient seems to be used in a single place (in iconTable.ts) but i'm wondering if this is not a potential bug?

Copy link
Contributor Author

@zatteo zatteo Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed resetSessionToken.

Why do you think clearClient in iconTable.ts may be a potential bug ? cc @acezard if you have insights here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I anticipate that clearing the client only without cleaning related CozyPersistedStorageKeys may produce bugs.

Copy link
Contributor Author

@zatteo zatteo Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so you mean we could replace this we the clearData function. Good idea.

This method will be used to clear EVERYTHING in local storage. Will
be used only in tests environment for the moment.
…ect AsyncStorage calls

We add a new enum called DevicePersistedStorageKeys dedicated to values that :
- are linked to the device (so only one value can be stored contrary to UserPersistedStorageKeys)
- are not removed when logging out
Since Oauth and SessionCreated are now part of the StorageKeys
object, they are also removed when calling clearData().
@zatteo zatteo force-pushed the refactor/use-storage-wrapper branch from 1db64a8 to 4e01eb4 Compare February 23, 2024 10:24
@zatteo zatteo requested a review from Ldoppea February 23, 2024 10:28
**Previously**

- StorageKey = key where data is persisted for a cozy life (so
removed at log out)
- DevicePersistedStorageKeys = key where data is persisted for the
device life (so NOT removed at log out)

**Now**

- CozyPersistedStorageKeys  = key where data is persisted for a cozy
(so until log out)
- DevicePersistedStorageKeys = key where data is persisted for the
device life (so NOT removed at log out)
StorageKey = CozyPersistedStorageKeys | DevicePersistedStorageKeys

There is still a 3rd enum called UserPersistedStorageKeys but it is
not used with the same storage wrapper so it can not be included in
the new StorageKeys.
@zatteo zatteo force-pushed the refactor/use-storage-wrapper branch from f17f3a4 to dadc43f Compare February 23, 2024 13:52
We previously renamed StorageKeys to CozyPersistedDeviceKeys.
clearData method was clearing StorageKeys. It now clears
CozyPersistedDeviceKeys. Let's be explicit by also renaming
clearData.
@zatteo zatteo merged commit 7b734cd into master Feb 23, 2024
1 check passed
@zatteo zatteo deleted the refactor/use-storage-wrapper branch February 23, 2024 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants