ember.js icon indicating copy to clipboard operation
ember.js copied to clipboard

Promote `*engineParent` methods for public API

Open villander opened this issue 4 years ago • 5 comments

this API was introduced before by https://github.com/emberjs/ember.js/pull/13518 to enable developers and ember internal tracking the parents of engine instances. Sometimes we want to use getEngineParent as an immediate parent (which usually will be the host app, but maybe another engine) to get the desirable parent to do something since getting the rootApplication or access the parent context across engines.

A great example, using glimmer-apollo I can for example share the Apollo Client across engines (nested or not )

On ember-source: 3.28.1 I already can access these methods using import { getEngineParent, setEngineParent } from '@ember/engine'; now we just need to make it public in the docs

cc: @rwjblue @dgeb

villander avatar Oct 12 '21 21:10 villander

I don't fully understand the motivating use cases here. Can you explain in more detail why this API is required to support this?

rwjblue avatar Oct 14 '21 20:10 rwjblue

@rwjblue sometimes I need to get the rootOwner inside an architecture with ember-engines, and sometimes just get the immediate parent, that can be an engine or host app.

I'm using this "hack" to access the host app for example:

get rootApplication {
  const owner = getOwner(this);
  const appRouter = owner.lookup('router:main');
  return getOwner(appRouter);
}

But actually, I could access in a more elegant way using the getParentEngine API which is making more sense, since it belongs to Engine API and I don't need to break the separation of concerns, accessing directly the owner of the route:main injected in the EngineInstance.

import { getEngineParent } from '@ember/engine';

get rootApplication() {
    let parent = getEngineParent(getOwner(this));
    while (getEngineParent(parent)) {
      parent = getEngineParent(parent);
    }
    return parent;
}
 
 get parentEngine() {
    let parent = getEngineParent(getOwner(this));
    return parent;
 }

Please, see with more details the hack example in the addon ember-engines-router-service

For glimmer-apollo I'd love to use the same client registered into host-app to be reused in every engine and get only an ApolloClient instance - to do that I need to use the "hack" getOwner(getOwner(this).lookup('router:main')) to access the host app - which break the isolation principle.

Then, if these cases are usually when we want to access parents from an engine perspective, why don't make it public? and avoid developers break the isolation principle proposed by ember-engines?

villander avatar Oct 14 '21 20:10 villander

@rwjblue @runspired can we merge it?

villander avatar May 12 '22 22:05 villander

Is this still relevant?

kategengler avatar Dec 12 '23 16:12 kategengler

@kategengler yes! It’s still relevant IMO.

cc @runspired @SergeAstapov

villander avatar Mar 01 '24 10:03 villander