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

[Bug] Multiple service instances based on invocation syntax #19794

Open
tcwood opened this issue Oct 19, 2021 · 2 comments
Open

[Bug] Multiple service instances based on invocation syntax #19794

tcwood opened this issue Oct 19, 2021 · 2 comments

Comments

@tcwood
Copy link

tcwood commented Oct 19, 2021

🐞 Describe the Bug

Varying service syntaxes can result in separate services rather than one shared service. This came up in an app where an addon component invoked a service from the addon in one way and the app invoked the service in another way. It doesn't seem like the app should need to know how the addon service is invoked in order to function properly.

The various invocations I tested were:

  1. @service() - relying on the variable name matching the service name
  2. @service('<service name>')
  3. @service('<addon/app name>$<service name>') - this syntax comes from the ember-holy-futuristic-template-namespacing-batman package
  4. @service('<addon/app name>@<service name>')

1/2 create one shared instance of a service and 3/4 create a separate shared instance of a service.

🔬 Minimal Reproduction

There's a minimal reproduction of this at https://github.com/tcwood/ember-service-repro

It creates a service and then 4 components that use the service with the syntaxes listed above. It creates table cells containing buttons to interact with the service and the value coming from the service for each component. A console.log is also placed in the service's constructor to show how many times it is being instantiated.

😕 Actual Behavior

Separate services are created by varying syntaxes. The gif below shows sample usage of the reproduction app.

service

🤔 Expected Behavior

Only one service is created regardless of the syntax used. In the gif above that would result in all of the right side values always being synced.

🌍 Environment

  • Ember: - 3.18.0 (in the repro app), 3.26.2 in the app where I noticed this
  • Node.js/npm: - 6.14.10
  • OS: - osx
  • Browser: - chrome
@SergeAstapov
Copy link
Contributor

According to comment in ember-cli/ember-resolver#531

This is expected behavior.

@tcwood
Copy link
Author

tcwood commented Oct 26, 2021

Thanks @SergeAstapov for the link.

From the comment in that issue:

So generalizing, the pattern for looking up something from a foreign package is:

this.owner.lookup(`${packageName}@{type}:${name}`);
this.owner.lookup(`{type}:${packageName}@${name}`);

how does that translate into the service invocation?

Does @service(${packageName}@${name}) translate into something like this.owner.lookup(service:${packageName}@${name})?

If this is the case, what are best practices as either an addon creator or in the consuming app? Currently, I have a situation where an addon service uses the #2 syntax from the first message and the app uses the #3 syntax, resulting in separate service instances.

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

No branches or pull requests

2 participants