Move the logic just before sending the response
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3536831. -->
Reported by: [andras_szilagyi](https://www.drupal.org/user/1217938)
Related to !12
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>In <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/track_usage/issues/3535772" title="Status: Closed (fixed)">#3535772: Track Usage module creates too many database queries </a></span>, Track Usages module moves the actual tracking and recording at the end of the request, just before the response is sent back to the user. The <code>file_visibility_track_usage</code> submodule needs to account for that because it should run just after <code>track_usage</code> has finished its job.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li>Make <code>FileVisibility</code> service also an event subscriber and add a <code>::doUpdateFileVisibility()</code> method that subscribes to <code>KernelEvents::RESPONSE</code></li>
<li>In <code>file_visibility_track_usage</code>, alter the priorities of <code>KernelEvents::RESPONSE</code> subscribers so that every time <code>FileVisibility</code> comes after <code>Recorder</code>.</li>
<li><code>FileVisibility::updateFilesVisibility()</code> will only register entities, the work is moved in <code>::doUpdateFileVisibility()</code></li>
</ul>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>None.</p>
<h3 id="summary-ui-changes">User interface changes</h3>
<p>None.</p>
<h3 id="summary-introduced-terminology">Introduced terminology</h3>
<p>None.</p>
<h3 id="summary-api-changes">API changes</h3>
<p>None.</p>
<h3 id="summary-data-model-changes">Data model changes</h3>
<p>None.</p>
> Related issue: [Issue #3535772](https://www.drupal.org/node/3535772)
issue