Skip to content

Commit

Permalink
Merge pull request #20253 from emberjs/rfc821-pt2
Browse files Browse the repository at this point in the history
[FEATURE] Add the `Resolver` type to preview types
  • Loading branch information
chriskrycho authored Nov 3, 2022
2 parents 8699faa + 7077f12 commit 52c13e5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
22 changes: 21 additions & 1 deletion type-tests/preview/@ember/owner-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import Owner, { Factory, FactoryManager, FullName, RegisterOptions } from '@ember/owner';
import Owner, {
Factory,
FactoryManager,
FullName,
RegisterOptions,
Resolver,
KnownForTypeResult,
} from '@ember/owner';
import { expectTypeOf } from 'expect-type';

// Just a class we can construct in the Factory and FactoryManager tests
Expand Down Expand Up @@ -53,6 +60,19 @@ expectTypeOf(aFactoryManager.create(goodPojo)).toEqualTypeOf<ConstructThis>();
// @ts-expect-error
aFactoryManager.create(badPojo);

// ----- Resolver ----- //
declare let resolver: Resolver;
expectTypeOf<Resolver['normalize']>().toEqualTypeOf<((fullName: FullName) => string) | undefined>();
expectTypeOf<Resolver['lookupDescription']>().toEqualTypeOf<
((fullName: FullName) => string) | undefined
>();
expectTypeOf(resolver.resolve('some-name')).toEqualTypeOf<object | Factory<object> | undefined>();
const knownForFoo = resolver.knownForType?.('foo');
expectTypeOf(knownForFoo).toEqualTypeOf<KnownForTypeResult<'foo'> | undefined>();
expectTypeOf(knownForFoo?.['foo:bar']).toEqualTypeOf<boolean | undefined>();
// @ts-expect-error
knownForFoo?.['blah'];

// This one is last so it can reuse the bits from above!
// ----- Owner ----- //
declare let owner: Owner;
Expand Down
35 changes: 0 additions & 35 deletions types/preview/@ember/-internals/resolver.d.ts

This file was deleted.

3 changes: 1 addition & 2 deletions types/preview/@ember/application/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ declare module '@ember/application' {
import { EventDispatcherEvents } from '@ember/application/types';
import Router from '@ember/routing/router';
import Registry from '@ember/application/-private/registry';
import type { Resolver } from '@ember/-internals/resolver';
import { AnyFn } from 'ember/-private/type-utils';
import Owner from '@ember/owner';
import Owner, { Resolver } from '@ember/owner';
import type GlimmerComponent from '@glimmer/component';
import EmberObject from '@ember/object';

Expand Down
2 changes: 1 addition & 1 deletion types/preview/@ember/debug/container-debug-adapter.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module '@ember/debug/container-debug-adapter' {
import type { Resolver } from '@ember/-internals/resolver';
import type { Resolver } from '@ember/owner';

/**
* The ContainerDebugAdapter helps the container and resolver interface
Expand Down
2 changes: 1 addition & 1 deletion types/preview/@ember/engine/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare module '@ember/engine' {
import type RegistryProxyMixin from '@ember/engine/-private/registry-proxy-mixin';
import type Initializer from '@ember/engine/-private/types/initializer';
import type EngineInstance from '@ember/engine/instance';
import type { Resolver } from '@ember/-internals/resolver';
import type { Resolver } from '@ember/owner';

/**
* The `Engine` class contains core functionality for both applications and
Expand Down
30 changes: 30 additions & 0 deletions types/preview/@ember/owner/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@ declare module '@ember/owner' {
readonly class: Factory<T>;
}

/**
* A record mapping all known items of a given type: if the item is known it
* will be `true`; otherwise it will be `false` or `undefined`.
*/
export type KnownForTypeResult<Type extends string> = {
[FullName in `${Type}:${string}`]: boolean | undefined;
};

/**
* A `Resolver` is the mechanism responsible for looking up code in your
* application and converting its naming conventions into the actual classes,
* functions, and templates that Ember needs to resolve its dependencies, for
* example, what template to render for a given route. It is a system that helps
* the app resolve the lookup of JavaScript modules agnostic of what kind of
* module system is used, which can be AMD, CommonJS or just plain globals. It
* is used to lookup routes, models, components, templates, or anything that is
* used in your Ember app.
*
* This interface represents the contract a custom resolver must implement. Most
* apps never need to think about this: the application's resolver is supplied by
* `ember-resolver` in the default blueprint.
*/
export interface Resolver {
resolve: (name: string) => Factory<object> | object | undefined;
knownForType?: <Type extends string>(type: Type) => KnownForTypeResult<Type>;
lookupDescription?: (fullName: FullName) => string;
makeToString?: (factory: Factory<object>, fullName: FullName) => string;
normalize?: (fullName: FullName) => string;
}

// Don't export things unless we *intend* to.
export {};
}
2 changes: 0 additions & 2 deletions types/preview/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import './ember';
import './ember/-private/type-utils';

import './@ember/-internals/resolver';

import './@ember/application';
import './@ember/application/-private/event-dispatcher';
import './@ember/application/-private/registry';
Expand Down

0 comments on commit 52c13e5

Please sign in to comment.