Skip to content

fix: enable InMemoryDriver localStorage persistence in MSW browser mode#992

Merged
hotlong merged 2 commits into
mainfrom
copilot/fix-inmemorydriver-localstorage-issue
Mar 3, 2026
Merged

fix: enable InMemoryDriver localStorage persistence in MSW browser mode#992
hotlong merged 2 commits into
mainfrom
copilot/fix-inmemorydriver-localstorage-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 3, 2026

InMemoryDriver persistence requires an explicit driver.connect() call to initialize the adapter (localStorage in browser). Without it, markDirty() on every CRUD op is a no-op and data is lost on refresh.

Changes

  • createKernel.ts: Add persistence option to KernelOptions, pass it to InMemoryDriver constructor. Call driver.connect() + driver.flush() after bootstrap and seed sync.
  • server.ts: Pass persistence: false to avoid file-system persistence in test/Node.js environment.

Lifecycle

Scenario Behavior
First load connect() finds empty localStorage, flush() persists seed data
Refresh connect() loads persisted data, overwriting fresh seeds — user changes survive
Test mode persistence: false — no I/O side effects
// createKernel.ts
const driver = new InMemoryDriver(
  persistence !== undefined ? { persistence } : undefined,
);
// ... bootstrap, syncDriverIds, patchDriverCreate ...
await driver.connect();
await driver.flush();
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] driver-memory 在 MSW/浏览器未持久化到 localStorage,导致刷新丢数据</issue_title>
<issue_description>## [Bug] InMemoryDriver 在 MSW 浏览器模式下未持久化到 localStorage

问题描述
objectstack-ai/objectui 仓库内,driver-memory 在 MSW 模式下运行于浏览器时,数据"应该"自动持久化到 localStorage,但实际情况是每次刷新页面数据丢失。

调试发现,浏览器 localStorage 中没有 objectstack:memory-db 键,说明 @objectstack/driver-memory 的持久化适配器未生效。

根因分析

  • InMemoryDriver 的 localStorage 持久化需要在构造后显式执行 driver.connect() 才会初始化 initPersistence() 并从 localStorage 恢复历史数据。
  • 当前 MSW mock 代码中,createKernel.ts 没有调用 driver.connect(),并且种子数据直接写入 db,绕过了本地持久化链路。

复现路径

  1. 访问 https://demo.objectui.org/apps/crm_app/account/record/6
  2. 在浏览器刷新页面后,所有数据全部丢失,localStorage 无 objectstack:memory-db

预期行为

  • 数据应持久化到 localStorage,在页面刷新后不会丢失。

建议修复方案

  1. apps/console/src/mocks/createKernel.ts 的 kernel/driver 初始化后,显式调用 await driver.connect(),确保持久化适配器生效;
  2. 在 seed 数据写入(如 syncDriverIds/patchDriverCreate)后,额外调用 driver.flush()persistenceAdapter.save(db),保证本地数据落盘;

关键参考实现(spec 仓库)


相关修复 Task

  • createKernel.ts 显式调用 await driver.connect()
  • 种子数据写入后调用 driver.flush() 或直接 save db
  • 验证 localStorage 已有 objectstack:memory-db 且刷新的数据不会丢失

如需 cross-repo issue(spec 仓库),请补充 driver/persistence 相关通用文档/注释。
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
objectui Ready Ready Preview, Comment Mar 3, 2026 5:57am
objectui-demo Ready Ready Preview, Comment Mar 3, 2026 5:57am
objectui-storybook Ready Ready Preview, Comment Mar 3, 2026 5:57am

Request Review

…ence in MSW browser mode

- Add `persistence` option to KernelOptions to control InMemoryDriver persistence
- Pass persistence config to InMemoryDriver constructor
- Call driver.connect() after bootstrap to initialize persistence adapter and load any previously persisted data from localStorage
- Call driver.flush() to ensure seed data is persisted on first load
- Pass persistence: false in server.ts (test mode) to disable file-system persistence

Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix driver-memory not persisting to localStorage in MSW browser mode fix: enable InMemoryDriver localStorage persistence in MSW browser mode Mar 3, 2026
@hotlong
Copy link
Copy Markdown
Collaborator

hotlong commented Mar 3, 2026

@copilot 我不知道你开发的是否正确,我是希望实现自动化的保存,包括在浏览器中的保存和服务端的保存

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot 我不知道你开发的是否正确,我是希望实现自动化的保存,包括在浏览器中的保存和服务端的保存

实现是正确的,自动保存已经在两种环境下生效:

浏览器模式 (browser.ts):不传 persistence 参数 → InMemoryDriver 默认使用 'auto' → 自动检测到浏览器环境 → 使用 LocalStoragePersistenceAdapterdriver.connect() 初始化适配器后,每次 CRUD 操作(create/update/delete)都会自动调用 markDirty()persistenceAdapter.save(db) → 同步写入 localStorage。页面刷新时,connect() 从 localStorage 恢复数据。

服务端 Node.js 模式:同样不传 persistence'auto' → 自动检测到 Node.js → 使用 FileSystemPersistenceAdapter,带定时 auto-save(默认 2 秒间隔)。

测试模式 (server.ts):显式传 persistence: false 禁用持久化,避免测试产生副作用。

关键改动(commit 0f85464):

  1. createKernel.ts 调用 await driver.connect() — 初始化持久化适配器
  2. createKernel.ts 调用 await driver.flush() — 确保种子数据落盘
  3. 之后每次 CRUD 操作自动触发 markDirty() → 自动持久化

@hotlong hotlong marked this pull request as ready for review March 3, 2026 05:56
Copilot AI review requested due to automatic review settings March 3, 2026 05:56
@hotlong hotlong merged commit 6c236b4 into main Mar 3, 2026
6 of 7 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables InMemoryDriver persistence in the console’s MSW browser mock environment by explicitly initializing and flushing the persistence adapter, while ensuring test/Node MSW usage disables persistence side effects.

Changes:

  • Added a persistence option to the createKernel factory and pass-through to the InMemoryDriver constructor.
  • Ensured persistence is initialized and written by calling driver.connect() and driver.flush() after bootstrap/seed setup.
  • Disabled persistence for Node/MSW server tests by passing persistence: false.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
apps/console/src/mocks/server.ts Disables driver persistence in Node/MSW server (tests) by passing persistence: false.
apps/console/src/mocks/createKernel.ts Adds configurable persistence, and initializes + flushes persistence after kernel bootstrap.

Comment on lines +28 to +37
/**
* InMemoryDriver persistence configuration.
*
* - `'auto'` (default) — auto-detect environment (browser → localStorage, Node.js → file)
* - `'local'` — force localStorage persistence (browser only)
* - `false` — disable persistence entirely (useful in tests)
*
* When omitted, defaults to `'auto'`.
*/
persistence?: false | 'auto' | 'local' | 'file';
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The JSDoc for persistence lists auto, local, and false, but the type also allows 'file'. Either document the 'file' mode in the comment (including when it’s valid) or remove it from the union to keep the API contract consistent.

Copilot uses AI. Check for mistakes.
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.

[Bug] driver-memory 在 MSW/浏览器未持久化到 localStorage,导致刷新丢数据

3 participants