feat(state): 添加运行时临时注册与注销功能#133
Merged
Merged
Conversation
- 实现 RegisterReducerHandle 和 RegisterMiddleware 方法,支持获取注销句柄 - 添加 IUnRegister 接口和 DefaultUnRegister 实现,提供精确注销能力 - 修改内部数据结构,使用 Registration 包装对象确保注销时的身份稳定性 - 实现中间件和 reducer 的快照机制,确保运行中注销不影响当前 dispatch - 添加相关单元测试验证运行时注册注销的正确性 - 更新文档说明运行时临时注册与注销的使用方式和约束条件
|
|
Overall Grade |
Security Reliability Complexity Hygiene |
Code Review Summary
| Analyzer | Status | Updated (UTC) | Details |
|---|---|---|---|
| C# | Mar 23, 2026 12:58p.m. | Review ↗ | |
| Secrets | Mar 23, 2026 12:58p.m. | Review ↗ |
|
🧙 Sourcery 已完成对你的 Pull Request 的代码审查! 提示和命令与 Sourcery 互动
自定义你的体验前往你的控制面板以:
获取帮助Original review guide in English🧙 Sourcery has finished reviewing your pull request! Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - 我发现了 1 个问题
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="GFramework.Core/StateManagement/Store.cs" line_range="623-632" />
<code_context>
+ /// Dispatch 在离开状态锁前复制列表,以便后续在锁外执行稳定、不可变的中间件序列。
+ /// </summary>
+ /// <returns>当前中间件链的快照;若未注册则返回空数组。</returns>
+ private IStoreMiddleware<TState>[] CreateMiddlewareSnapshot()
+ {
+ if (_middlewares.Count == 0)
+ {
+ return Array.Empty<IStoreMiddleware<TState>>();
+ }
+
+ var snapshot = new IStoreMiddleware<TState>[_middlewares.Count];
+ for (var i = 0; i < _middlewares.Count; i++)
+ {
+ snapshot[i] = _middlewares[i].Middleware;
+ }
+
+ return snapshot;
+ }
+
</code_context>
<issue_to_address>
**issue (bug_risk):** CreateMiddlewareSnapshot 假定由外部负责加锁,如果将来在其他地方复用,这可能会比较脆弱。
CreateMiddlewareSnapshot 在未取得 _lock 的情况下读取 _middlewares,目前只在 Dispatch 的临界区内被调用,所以现在是安全的,但这依赖于一个不明显的前置条件。如果有一天在锁外被调用,就可能引入竞争条件/一致性问题。
为了让它更安全、更清晰,可以选择在 CreateMiddlewareSnapshot 内部获取 _lock,或者传入一个已在锁保护下的 List<MiddlewareRegistration> 让它只做映射投影,或者把它做成一个明确“仅在加锁条件下使用”的辅助方法(例如带注释的本地函数)。这样可以降低未来重构时被误用的风险。
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据这些反馈改进后续的评审。
Original comment in English
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="GFramework.Core/StateManagement/Store.cs" line_range="623-632" />
<code_context>
+ /// Dispatch 在离开状态锁前复制列表,以便后续在锁外执行稳定、不可变的中间件序列。
+ /// </summary>
+ /// <returns>当前中间件链的快照;若未注册则返回空数组。</returns>
+ private IStoreMiddleware<TState>[] CreateMiddlewareSnapshot()
+ {
+ if (_middlewares.Count == 0)
+ {
+ return Array.Empty<IStoreMiddleware<TState>>();
+ }
+
+ var snapshot = new IStoreMiddleware<TState>[_middlewares.Count];
+ for (var i = 0; i < _middlewares.Count; i++)
+ {
+ snapshot[i] = _middlewares[i].Middleware;
+ }
+
+ return snapshot;
+ }
+
</code_context>
<issue_to_address>
**issue (bug_risk):** CreateMiddlewareSnapshot assumes external locking, which could be fragile if reused elsewhere.
CreateMiddlewareSnapshot reads _middlewares without taking _lock and is currently only used from within Dispatch’s critical section, so it’s safe today but relies on a non-obvious precondition. If it’s ever called outside the lock, it could introduce a race/consistency bug.
To make this safer and clearer, either acquire _lock inside CreateMiddlewareSnapshot, pass a locked List<MiddlewareRegistration> into it so it only projects, or make it a clearly "locked-only" helper (e.g., a local function) with a comment. This will reduce the risk of misuse in future refactors.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- 添加GFramework.Core.Abstractions.Property命名空间引用 - 重新排序using语句以优化代码结构 - 保持现有功能不变的同时改进代码组织方式
- 为CreateMiddlewareSnapshot方法添加状态锁保护 - 为CreateReducerSnapshot方法添加状态锁保护 - 更新方法注释说明锁的安全性保障机制 - 避免调用方需要了解锁顺序的隐式依赖关系
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
为 store 中的 reducers 和 middlewares 添加基于运行时句柄的注册机制和精确注销支持,同时保持 dispatch 快照语义不变。
New Features:
Enhancements:
Documentation:
Tests:
Original summary in English
Summary by Sourcery
Add runtime handle-based registration and precise unregistration support for reducers and middlewares in the store while preserving dispatch snapshot semantics.
New Features:
Enhancements:
Documentation:
Tests: