返回提示词目录第 1422 页

将冗余事件调用重构为直接调用

排查事件发送与监听关系,替换适宜的间接调用,保留执行语义并提供代码差异和测试结果。

完整提示词

<context>
In-process events are useful when a module must not know its listeners: plugins, many independent subscribers, or a boundary between packages. They hurt when an event has exactly one listener that the emitter depends on: "save the order" emits `order_saved`, and the listener sends the invoice, and nobody can find that by reading the save function. Debugging needs a global search for a string, ordering between listeners is undefined or registration-dependent, and a listener registered in a module nobody imports silently never runs.

Replacing an emit with a direct call changes semantics in ways to check first. Synchronous emitters run listeners inline, so exceptions in a listener may propagate to the emitter or be swallowed depending on the library (Node `EventEmitter` propagates; many buses catch and log; Django `send_robust` catches). Listeners may run inside the emitter's database transaction or after commit (Spring `@TransactionalEventListener`, Django `transaction.on_commit`), which decides whether a failed email rolls back an order. Async buses run later, on another thread or tick. Events can also have hidden listeners: other packages, plugins, tests, analytics or audit code that subscribe by name. Keep an event where there are several independent listeners or a real boundary, and say so.
</context>

<task>
Simplify the events in {{code}}.

1. Map every event: name, emitters, listeners (search by event name string and class, including tests, plugins and other packages), registration site, sync or async, transaction phase, and how listener errors are handled.
2. Decide per event: replace with a direct call (one listener, same module or a dependency the emitter may know), keep (several independent listeners, cross-package boundary, plugins), or keep with explicit registration. Wait for approval.
3. For each replacement, write the direct call where the emit was, preserving: order relative to other code, sync or async execution, transaction phase (call after commit if the listener ran after commit), and error behaviour (propagate or catch and log as before).
4. Remove the listener registration and the event definition when nothing else uses it. Run the tests{{#test_command}} with `{{test_command}}`{{/test_command}} after each event.
5. Show the diff and note every behaviour detail you preserved or that changed.
</task>

<constraints>
- Execution order, transaction phase, sync or async behaviour and error propagation stay the same unless the user approves a change.
- Do not remove an event that has listeners outside the searched code (plugins, other services, analytics) without confirming.
- Do not introduce a circular import; if the direct call would create one, keep the event and say why.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Event map
Table: event, emitters, listeners, sync or async, transaction phase, error handling.
## Decisions
Per event: replace, keep or keep with explicit registration, and why.
## Diff
The change.
## Behaviour notes
What was preserved and how.
## Check
Tests run and results.
</output_format>

可替换内容

  • {{code}}
  • {{#test_command}}
  • {{test_command}}
  • {{/test_command}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、代码重构、事件解耦、事务语义、行为验证

前往 AI 图片创作