deprecation-app icon indicating copy to clipboard operation
deprecation-app copied to clipboard

Missing guide for the get/set owner migration from `@ember/application` to `@ember/owner`

Open NullVoxPopuli opened this issue 3 months ago • 0 comments

Here is a path for libraries to support both (with typescript):

import { appEmberSatisfies, importSync, macroCondition } from '@embroider/macros';

import type Owner from '@ember/owner';

interface CompatOwner {
  getOwner: (context: unknown) => Owner | undefined;
  setOwner: (context: unknown, owner: Owner) => void;
}

export const compatOwner = {} as CompatOwner;

if (macroCondition(appEmberSatisfies('>=4.12.0'))) {
  // In no version of ember where `@ember/owner` tried to be imported did it exist
  // Using 'any' here because importSync can't lookup types correctly
  compatOwner.getOwner = (importSync('@ember/owner') as any).getOwner;
  compatOwner.setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
  // Using 'any' here because importSync can't lookup types correctly
  compatOwner.getOwner = (importSync('@ember/application') as any).getOwner;
  compatOwner.setOwner = (importSync('@ember/application') as any).setOwner;
}

// usage
compatOwner.getOwner(this)

and for app devs, here is how they can use libraries that use @ember/owner, even if they're behind 4.11 (when @ember/owner landed in stable)

https://github.com/NullVoxPopuli/ember-polyfill-get-and-set-owner-from-ember-owner

Requires:

  • @embroider/macros 1.19.4 (which does not require embroider)

NullVoxPopuli avatar Nov 25 '25 15:11 NullVoxPopuli