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

Create DEFAULT_QUEUE when FileQueueService is created #1094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gilest
Copy link
Collaborator

@gilest gilest commented Mar 4, 2025

Context

This issue occurs due to a computation being rendered before a dependency of that computation is updated – during the same render pass.

In this case:

    const fileQueueService = this.owner.lookup(
      'service:file-queue',
    ) as FileQueueService;

    await render(
      <template>
        <ul>
          {{! `fileQueueService.files` is a computation of all files in all queues }}
          {{#each fileQueueService.files as |file|}}
            <li>
              {{file.name}}
            </li>
          {{/each}}
        </ul>

        {{! The `fileQueue` helper calls `fileQueueService.findOrCreate` which may create a new queue }}
        {{! Thus invalidating `fileQueueService.files` }}
        {{#let (fileQueue) as |queue|}}
          <label>
            <input type='file' {{queue.selectFile}} hidden />
            Select File
          </label>
        {{/let}}
      </template>,
    );

This is sometimes called a "backtracking re-render". Further reading:

Partial fix

A workaround to prevent this issue for the DEFAULT_QUEUE is to create it early, before the render pass.

However, with a named queue this would need to be done by consumers in their own app code. A demonstration of this has been added as a test.

Probably, some documentation should be updated to warn about this.

Considerations

This is only a partial fix. As a breaking change we might want to restrict the fileQueue helper to only being able find queues, and not create them.

That would mean users should create any named queues explicity in their own app code outside of rendering. This is a minor limitation, but I think it might be a better design since it suggests using autotracking in a more conventional way. Users would still be able to create dynamically named queues if they wanted, thought that's probably rare.

Partially fixes #1085

@gilest gilest added the bug label Mar 4, 2025
@gilest gilest force-pushed the fix/service-before-helper branch from deedc3e to c67ba93 Compare March 4, 2025 22:45
Comment on lines +307 to +311
const fileQueueService = this.owner.lookup(
'service:file-queue',
) as FileQueueService;
const customName = 'uploads';
fileQueueService.create(customName);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the case of a named queue. A user would need to manually create it before a rendering pass to avoid this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Service cannot be used before helper
2 participants