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

fix: hydrated for indexedDB #5356

Merged
merged 3 commits into from
Sep 4, 2024

Conversation

Dogtiti
Copy link
Member

@Dogtiti Dogtiti commented Sep 3, 2024

💻 变更类型 | Change Type

  • feat
  • fix
  • refactor
  • perf
  • style
  • test
  • docs
  • ci
  • chore
  • build

🔀 变更说明 | Description of Change

📝 补充信息 | Additional Information

Summary by CodeRabbit

  • New Features

    • Enhanced state management for IndexedDB storage, ensuring only fully hydrated state objects are processed and stored.
    • Improved retrieval process with automatic state hydration checks during data fetching.
    • Added functionality to track hydration status within the store, improving lifecycle management.
  • Bug Fixes

    • Resolved issues related to incomplete or uninitialized state data being saved in storage.

Copy link

vercel bot commented Sep 3, 2024

@Dogtiti is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Sep 3, 2024

Walkthrough

The changes to the IndexedDBStorage class enhance the logic within the getItem and setItem methods. The getItem method now stores the retrieved value in a variable before returning it, improving readability. The setItem method introduces a check to prevent saving items unless the state is fully hydrated. Additionally, modifications in the store management ensure accurate tracking of hydration status during initialization and storage operations.

Changes

File Change Summary
app/utils/indexedDB-storage.ts Modified getItem to store fetched values in a variable; updated setItem to prevent saving unhydrated states.
app/utils/store.ts Enhanced MakeUpdater type with _hasHydrated property and setHasHydrated method; updated createPersistStore to manage hydration status during rehydration.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant IndexedDBStorage
    participant LocalStorage

    User->>IndexedDBStorage: getItem(key)
    IndexedDBStorage->>LocalStorage: retrieve value
    LocalStorage-->>IndexedDBStorage: return value
    IndexedDBStorage->>IndexedDBStorage: store value in variable
    IndexedDBStorage->>User: return stored value (if hydrated)

    User->>IndexedDBStorage: setItem(key, value)
    IndexedDBStorage->>IndexedDBStorage: check if value is hydrated
    alt if hydrated
        IndexedDBStorage->>LocalStorage: save value
    else
        IndexedDBStorage->>User: exit early (not saved)
    end
Loading

Poem

🐰 In the garden where data flows,
Hydration's key is what we chose.
With every fetch, we check and parse,
Ensuring states are never sparse.
So hop along, let’s store with cheer,
For every byte, we hold so dear! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Dogtiti
Copy link
Member Author

Dogtiti commented Sep 3, 2024

fix: #5354

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 97a4a91 and 886ffc0.

Files selected for processing (1)
  • app/utils/indexedDB-storage.ts (1 hunks)
Additional comments not posted (2)
app/utils/indexedDB-storage.ts (2)

7-12: LGTM!

The code changes in the getItem method are approved for the following reasons:

  • The code segment correctly handles the case where the value is not found in IndexedDB by falling back to localStorage.
  • Parsing the value before checking for the state property is a good practice to avoid type errors.
  • Setting _hasHydrated to true when the state property exists ensures that the state is properly hydrated when items are fetched.

20-23: LGTM!

The code changes in the setItem method are approved for the following reasons:

  • Parsing the value before checking for the _hasHydrated property is a good practice to avoid type errors.
  • Exiting early when _hasHydrated is not present enforces a condition that ensures only fully hydrated states are stored, potentially preventing issues related to incomplete or uninitialized state data.

Copy link
Contributor

github-actions bot commented Sep 3, 2024

Your build has completed!

Preview deployment

@laxfer
Copy link

laxfer commented Sep 3, 2024

你好,经测试,这次就没有往indexedDB写入值,建议你调试下indexedDB,测试的时候,可以先删除localStorage的代码
20240903175529

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Hello, after testing, no value was written to indexedDB this time. It is recommended that you debug indexedDB. When testing, you can delete the localStorage code first.
20240903175529

@lloydzhou

This comment was marked as outdated.

@Issues-translate-bot

This comment was marked as outdated.

@laxfer

This comment was marked as outdated.

@Issues-translate-bot

This comment was marked as outdated.

@lloydzhou
Copy link
Member

@laxfer

  1. 您前面提到的刷新之后。indexeddb数据丢失的问题。原因在于,getval以及setval是异步操作,刷新的时候可能导致某一次刚刚初始化(这个时候sessions中是空的,这个时候展示了一个默认的消息,但是sessions中实际上是没有消息的)还没来得及读取到Storage中读取的数据,就尝试保存数据,将数据覆盖了导致的错误。
  2. 所以,现在如果是全新的开启一个会话,这个时候应该是会跳过保存数据的。

这个原来存储的数据丢失问题在对话页面连续三次刷新时就能复现,目前尝试了,没有解决,可能有些复杂!

刚刚准备把我自己的消息隐藏,点错了,把你的comment隐藏了,抱歉

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


@laxfer

  1. After the refresh you mentioned earlier. Indexeddb data loss problem. The reason is that getval and setval are asynchronous operations. When refreshing, it may cause a certain initialization (the sessions are empty at this time, and a default message is displayed at this time, but there is actually no message in the sessions) before it is too late. After reading the data read from Storage, try to save the data and overwrite the data, causing an error.
  2. Therefore, if you open a new session now, saving data should be skipped at this time.

This original stored data loss problem can be reproduced when the conversation page is refreshed three times in a row. I have tried it so far, but it has not been solved. It may be a bit complicated!

I was just about to hide my own message, but I clicked the wrong button and hid your comment. Sorry.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 886ffc0 and ed9aae5.

Files selected for processing (3)
  • app/store/prompt.ts (1 hunks)
  • app/utils/indexedDB-storage.ts (1 hunks)
  • app/utils/store.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/utils/indexedDB-storage.ts
Additional comments not posted (5)
app/utils/store.ts (4)

17-17: LGTM!

The code changes are approved.


21-21: LGTM!

The code changes are approved.


38-42: LGTM!

The code changes are approved.


49-49: LGTM!

The code changes are approved.

Also applies to: 68-70

app/store/prompt.ts (1)

182-183: LGTM!

The code changes are approved. The return statement provides a cleanup function that sets the state to indicate that hydration has occurred. This change enhances the functionality of the store by ensuring that the state is properly updated after the initialization process, reflecting a more robust handling of the store's initialization process.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ed9aae5 and 53dcae9.

Files selected for processing (2)
  • app/utils/indexedDB-storage.ts (1 hunks)
  • app/utils/store.ts (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/utils/indexedDB-storage.ts
  • app/utils/store.ts

@lloydzhou lloydzhou merged commit 8570457 into ChatGPTNextWeb:main Sep 4, 2024
1 of 2 checks passed
@lloydzhou lloydzhou added the bug Something isn't working label Sep 4, 2024
@lloydzhou lloydzhou deleted the feature/indexdb branch September 26, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants