-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update preview types to match the implementation
Updates the preview types to include the `@ember/owner` types introduced in an earlier commit. Besides updating the other existing related exports to match the refactored types from the implementation, the refactoring involved in landing correct types internally result in a couple of key changes: 1. `getOwner()` always returns `Owner | undefined`. The previous attempt to make it return `Owner` when working with a "known object" of some sort runs into significant issues: - Ember's public API allows the direct construction of many types which are *normally* managed by the framework. It is legal, if odd and *not recommended*, to instantiate any given `Factory` with its `.create()` method: import Component from '@glimmer/component'; import Session from '../services' export default class Example extends Component { session = Session.create(); } In this example, the `session` property on `Example` will *not* have an `Owner`, so it is not safe for `session` itself to rely on that. This is annoying, but type safe, and straightforward to work around. For example, a helper designed to look up services in templates might do something like this: import Helper from '@ember/component/helper'; import { getOwner } from '@ember/owner'; import { assert } from '@ember/debug'; class GetService extends Helper { compute([serviceName]) { let owner = getOwner(this); assert('unexpected missing an owner!', !!owner); return owner.lookup(`service:${name}`); } } Then if someone did `GetService.create()` instead of using a helper in a template *or* with `invokeHelper()`, they would get a useful error message. - For the same reasons we cannot guarantee that contract from a types perspective, it is actually impossible to *implement* it in a type safe manner. 2. The service registry now *requires* that its fields be `Service` subclasses. We added this constraint as part of generalizing the DI registry system to work well with the `Owner` APIs while avoiding introducing any circularity into the module graph. This should also be future-compatible with alternative future designs. This should not be breaking in practice, because items in the service registry were always expected to be service classes. (Note that this cannot be backported to the release because the modules it represents do not exist until this whole PR lands.)
- Loading branch information
1 parent
ecd3db7
commit 5658b13
Showing
9 changed files
with
402 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 2 additions & 8 deletions
10
types/preview/@ember/engine/-private/container-proxy-mixin.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
declare module '@ember/engine/-private/container-proxy-mixin' { | ||
import Owner from '@ember/owner'; | ||
import { ContainerProxy } from '@ember/owner'; | ||
import Mixin from '@ember/object/mixin'; | ||
|
||
/** | ||
* Given a fullName return a factory manager. | ||
*/ | ||
interface ContainerProxyMixin extends Owner { | ||
/** | ||
* Returns an object that can be used to provide an owner to a | ||
* manually created instance. | ||
*/ | ||
ownerInjection(): {}; | ||
} | ||
interface ContainerProxyMixin extends ContainerProxy {} | ||
const ContainerProxyMixin: Mixin; | ||
export default ContainerProxyMixin; | ||
} |
46 changes: 2 additions & 44 deletions
46
types/preview/@ember/engine/-private/registry-proxy-mixin.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,12 @@ | ||
declare module '@ember/engine/-private/registry-proxy-mixin' { | ||
import Owner from '@ember/owner'; | ||
import { RegistryProxy } from '@ember/owner'; | ||
import Mixin from '@ember/object/mixin'; | ||
|
||
/** | ||
* RegistryProxyMixin is used to provide public access to specific | ||
* registry functionality. | ||
*/ | ||
interface RegistryProxyMixin extends Owner { | ||
/** | ||
* Given a fullName return the corresponding factory. | ||
*/ | ||
resolveRegistration(fullName: string): unknown; | ||
/** | ||
* Unregister a factory. | ||
*/ | ||
unregister(fullName: string): unknown; | ||
/** | ||
* Check if a factory is registered. | ||
*/ | ||
hasRegistration(fullName: string): boolean; | ||
/** | ||
* Register an option for a particular factory. | ||
*/ | ||
registerOption(fullName: string, optionName: string, options: {}): unknown; | ||
/** | ||
* Return a specific registered option for a particular factory. | ||
*/ | ||
registeredOption(fullName: string, optionName: string): {}; | ||
/** | ||
* Register options for a particular factory. | ||
*/ | ||
registerOptions(fullName: string, options: {}): unknown; | ||
/** | ||
* Return registered options for a particular factory. | ||
*/ | ||
registeredOptions(fullName: string): {}; | ||
/** | ||
* Allow registering options for all factories of a type. | ||
*/ | ||
registerOptionsForType(type: string, options: {}): unknown; | ||
/** | ||
* Return the registered options for all factories of a type. | ||
*/ | ||
registeredOptionsForType(type: string): {}; | ||
/** | ||
* Define a dependency injection onto a specific factory or all factories | ||
* of a type. | ||
*/ | ||
inject(factoryNameOrType: string, property: string, injectionName: string): unknown; | ||
} | ||
interface RegistryProxyMixin extends RegistryProxy {} | ||
const RegistryProxyMixin: Mixin; | ||
export default RegistryProxyMixin; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.