Service provided by ember engine being instantiated twice


#1

Hi,

I’ve got an ember engine providing a service that’s supposed to be used by the host app to share info to the engine. When I set a value on this service in the host app, this value is lost when I try to access it in the engine’s code.

I put a debugger statement in the engine’s constructor method (I use ember-decorators) and it looks like the an engine object is instantiated twice: lookup is called once on the host app owner, and once on the engine’s owner.

I believe I could work around this issue by creating a new service on the host app and explicitly declaring it in the engine’s dependencies, but I expected a service singleton to cross the host / engine boundary.

What am I missing here ?


#2

How are you providing the service from the engine to the host app? In general, injected service dependencies work by flowing down from the app into the engine, but what you described seemed like the other way around…


#3

Hi Robert. Thx for your answer and sorry for the late response, I’m back from 1 week of holidays :wink:

The service was defined in the addon/services dir of the addon and re-exported in the app/services dir of the addon.

The app then injected it wherever necessary and was setting a value on it. I thought that doing so, the value would then be accessible when using the service somewhere in the addon. But as I explained in the original post, this was not the case and I think this was because 2 different instances of the service were created.

I solved my issue by using in the addon a service defined by the host app


#4

Awesome, hope you had a great time :smile_cat:

Assuming that the “addon” here is actually the engine addon, this is definitely not good. An engine should not have anything in app/ (as a matter of fact, I thought we prevented this…).

Yep, this was the correct path forward IMHO.

The singleton’ness is per “owner”. In this case, the app and the engine have completely independent registry/container/owner constructs. This is absolutely by design, as this is one of the primary mechanisms by which we get isolation in an engine.