Skip to content

Commit

Permalink
Convert engines, applications, and more to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Feb 19, 2022
1 parent 9b0d2ff commit 47f62a5
Show file tree
Hide file tree
Showing 32 changed files with 759 additions and 655 deletions.
57 changes: 30 additions & 27 deletions packages/@ember/-internals/container/lib/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interface LeakTracking {
reset(): void;
}

interface CacheMember {
destroy?: () => void;
}
// interface CacheMember {
// destroy?: () => void;
// }

let leakTracking: LeakTracking;
let containers: WeakSet<Container>;
Expand Down Expand Up @@ -47,7 +47,7 @@ if (DEBUG) {

export interface ContainerOptions {
owner?: Owner;
cache?: { [key: string]: CacheMember };
cache?: { [key: string]: object };
factoryManagerCache?: { [key: string]: FactoryManager<any, any> };
validationCache?: { [key: string]: boolean };
}
Expand All @@ -70,8 +70,8 @@ export default class Container {

readonly owner: Owner | null;
readonly registry: Registry & DebugRegistry;
cache: { [key: string]: CacheMember };
factoryManagerCache!: { [key: string]: FactoryManager<any, any> };
cache: { [key: string]: object };
factoryManagerCache!: { [key: string]: FactoryManager<object> };
readonly validationCache!: { [key: string]: boolean };
isDestroyed: boolean;
isDestroying: boolean;
Expand Down Expand Up @@ -139,11 +139,10 @@ export default class Container {
@private
@method lookup
@param {String} fullName
@param {Object} [options]
@param {String} [options.source] The fullname of the request source (used for local lookup)
@param {TypeOptions} [options]
@return {any}
*/
lookup(fullName: string, options?: TypeOptions): unknown {
lookup(fullName: string, options?: TypeOptions): Factory<object> | object | undefined {
if (this.isDestroyed) {
throw new Error(`Cannot call \`.lookup\` after the owner has been destroyed`);
}
Expand Down Expand Up @@ -208,17 +207,15 @@ export default class Container {
@param {String} fullName
@return {any}
*/
factoryFor(fullName: string): Factory<unknown> | undefined {
factoryFor(fullName: string): FactoryManager<object> | undefined {
if (this.isDestroyed) {
throw new Error(`Cannot call \`.factoryFor\` after the owner has been destroyed`);
}
let normalizedName = this.registry.normalize(fullName);

assert('fullName must be a proper full name', this.registry.isValidFullName(normalizedName));

// TODO: This needs to return a Factory to be compatible with Owner.
// We should correctly the types so that this cast is not necessary.
return factoryFor(this, normalizedName, fullName) as Factory<unknown>;
return factoryFor(this, normalizedName, fullName);
}
}

Expand All @@ -230,7 +227,9 @@ if (DEBUG) {
* Wrap a factory manager in a proxy which will not permit properties to be
* set on the manager.
*/
function wrapManagerInDeprecationProxy<T, C>(manager: FactoryManager<T, C>): FactoryManager<T, C> {
function wrapManagerInDeprecationProxy<T extends object, C>(
manager: FactoryManager<T, C>
): FactoryManager<T, C> {
let validator = {
set(_obj: T, prop: keyof T) {
throw new Error(
Expand Down Expand Up @@ -261,7 +260,11 @@ function isInstantiatable(container: Container, fullName: string) {
return container.registry.getOption(fullName, 'instantiate') !== false;
}

function lookup(container: Container, fullName: string, options: TypeOptions = {}) {
function lookup(
container: Container,
fullName: string,
options: TypeOptions = {}
): Factory<object> | object | undefined {
let normalizedName = fullName;

if (
Expand All @@ -281,14 +284,14 @@ function factoryFor(
container: Container,
normalizedName: string,
fullName: string
): FactoryManager<unknown> | undefined {
): FactoryManager<object> | undefined {
let cached = container.factoryManagerCache[normalizedName];

if (cached !== undefined) {
return cached;
}

let factory = container.registry.resolve(normalizedName) as DebugFactory<unknown> | undefined;
let factory = container.registry.resolve(normalizedName) as DebugFactory<object> | undefined;

if (factory === undefined) {
return;
Expand Down Expand Up @@ -363,7 +366,7 @@ function instantiateFactory(
normalizedName: string,
fullName: string,
options: TypeOptions
) {
): Factory<object> | object | undefined {
let factoryManager = factoryFor(container, normalizedName, fullName);

if (factoryManager === undefined) {
Expand All @@ -373,13 +376,13 @@ function instantiateFactory(
// SomeClass { singleton: true, instantiate: true } | { singleton: true } | { instantiate: true } | {}
// By default majority of objects fall into this case
if (isSingletonInstance(container, fullName, options)) {
let instance = (container.cache[normalizedName] = factoryManager.create() as CacheMember);
let instance = (container.cache[normalizedName] = factoryManager.create());

// if this lookup happened _during_ destruction (emits a deprecation, but
// is still possible) ensure that it gets destroyed
if (container.isDestroying) {
if (typeof instance.destroy === 'function') {
instance.destroy();
if (typeof (instance as any).destroy === 'function') {
(instance as any).destroy();
}
}

Expand Down Expand Up @@ -410,8 +413,8 @@ function destroyDestroyables(container: Container): void {
let value = cache[key];
assert('has cached value', value);

if (value.destroy) {
value.destroy();
if ((value as any).destroy) {
(value as any).destroy();
}
}
}
Expand All @@ -429,8 +432,8 @@ function resetMember(container: Container, fullName: string) {
if (member) {
delete container.cache[fullName];

if (member.destroy) {
member.destroy();
if ((member as any).destroy) {
(member as any).destroy();
}
}
}
Expand All @@ -441,7 +444,7 @@ export interface LazyInjection {
specifier: string;
}

declare interface DebugFactory<T, C extends FactoryClass | object = FactoryClass>
declare interface DebugFactory<T extends object, C extends FactoryClass | object = FactoryClass>
extends Factory<T, C> {
_onLookup?: (fullName: string) => void;
_initFactory?: (factoryManager: FactoryManager<T, C>) => void;
Expand All @@ -458,7 +461,7 @@ export function setFactoryFor(obj: any, factory: FactoryManager<any, any>): void
obj[INIT_FACTORY] = factory;
}

export class FactoryManager<T, C extends FactoryClass | object = FactoryClass> {
export class FactoryManager<T extends object, C extends FactoryClass | object = FactoryClass> {
readonly container: Container;
readonly owner: Owner | null;
readonly class: Factory<T, C> & DebugFactory<T, C>;
Expand Down
52 changes: 24 additions & 28 deletions packages/@ember/-internals/container/lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import { assert, deprecate } from '@ember/debug';
import { set } from '@ember/object';
import { DEBUG } from '@glimmer/env';
import Container, { ContainerOptions, LazyInjection } from './container';

export interface Injection {
property: string;
specifier: string;
}

export type ResolveOptions = {
specifier?: string;
};

export interface TypeOptions {
instantiate?: boolean;
singleton?: boolean;
Expand All @@ -31,9 +28,9 @@ export interface IRegistry {
getOptions(fullName: string): TypeOptions | undefined;
getOptionsForType(type: string): TypeOptions | undefined;
knownForType(type: string): KnownForTypeResult;
makeToString<T, C>(factory: Factory<T, C>, fullName: string): string;
makeToString(factory: Factory<object>, fullName: string): string;
normalizeFullName(fullName: string): string;
resolve<T, C>(fullName: string, options?: ResolveOptions): Factory<T, C> | undefined;
resolve(fullName: string): Factory<object> | object | undefined;
}

export type NotResolver = {
Expand All @@ -44,7 +41,8 @@ export type NotResolver = {
resolve: never;
};

export type Resolve = <T, C>(name: string) => Factory<T, C> | undefined;
// TODO: Review the return here
export type Resolve = (name: string) => Factory<object> | undefined;

export interface ResolverClass {
create(...args: unknown[]): Resolver;
Expand All @@ -53,7 +51,7 @@ export interface ResolverClass {
export interface Resolver {
knownForType?: (type: string) => KnownForTypeResult;
lookupDescription?: (fullName: string) => string;
makeToString?: <T, C>(factory: Factory<T, C>, fullName: string) => string;
makeToString?: (factory: Factory<object>, fullName: string) => string;
normalize?: (fullName: string) => string;
resolve: Resolve;
}
Expand Down Expand Up @@ -81,13 +79,12 @@ const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;
*/
export default class Registry implements IRegistry {
readonly _failSet: Set<string>;
resolver: Resolver | null;
resolver: Resolver | (Resolve & NotResolver) | null;
readonly fallback: IRegistry | null;
readonly registrations: Record<string, object>;
_localLookupCache: Record<string, object>;
readonly registrations: Record<string, Factory<object> | object>;
readonly _normalizeCache: Record<string, string>;
readonly _options: Record<string, TypeOptions>;
readonly _resolveCache: Record<string, object>;
readonly _resolveCache: Record<string, Factory<object> | object>;
readonly _typeOptions: Record<string, TypeOptions>;

set?: typeof set;
Expand All @@ -98,7 +95,6 @@ export default class Registry implements IRegistry {

this.registrations = dictionary(options.registrations || null);

this._localLookupCache = Object.create(null);
this._normalizeCache = dictionary(null);
this._resolveCache = dictionary(null);
this._failSet = new Set();
Expand Down Expand Up @@ -189,12 +185,13 @@ export default class Registry implements IRegistry {
@param {Object} options
*/
register(fullName: string, factory: object, options: TypeOptions & { instantiate: false }): void;
register(fullName: string, factory: Factory<unknown>, options?: TypeOptions): void;
register(fullName: string, factory: object, options: TypeOptions = {}): void {
register(fullName: string, factory: Factory<object>, options?: TypeOptions): void;
register(fullName: string, factory: object | Factory<object>, options: TypeOptions = {}): void {
assert('fullName must be a proper full name', this.isValidFullName(fullName));
assert(`Attempting to register an unknown factory: '${fullName}'`, factory !== undefined);

let normalizedName = this.normalize(fullName);

assert(
`Cannot re-register: '${fullName}', as it has already been resolved.`,
!this._resolveCache[normalizedName]
Expand Down Expand Up @@ -227,8 +224,6 @@ export default class Registry implements IRegistry {

let normalizedName = this.normalize(fullName);

this._localLookupCache = Object.create(null);

delete this.registrations[normalizedName];
delete this._resolveCache[normalizedName];
delete this._options[normalizedName];
Expand Down Expand Up @@ -266,14 +261,12 @@ export default class Registry implements IRegistry {
@private
@method resolve
@param {String} fullName
@param {Object} [options]
@param {String} [options.source] the fullname of the request source (used for local lookups)
@return {Function} fullName's factory
*/
resolve<T, C>(fullName: string): Factory<T, C> | undefined {
let factory = resolve<T, C>(this, this.normalize(fullName));
resolve(fullName: string): Factory<object> | object | undefined {
let factory = resolve(this, this.normalize(fullName));
if (factory === undefined && this.fallback !== null) {
factory = (this.fallback as any).resolve(...arguments);
factory = this.fallback.resolve(fullName);
}
return factory;
}
Expand Down Expand Up @@ -342,7 +335,7 @@ export default class Registry implements IRegistry {
@param {string} fullName
@return {function} toString function
*/
makeToString<T, C>(factory: Factory<T, C>, fullName: string): string {
makeToString(factory: Factory<object>, fullName: string): string {
if (this.resolver !== null && this.resolver.makeToString) {
return this.resolver.makeToString(factory, fullName);
} else if (this.fallback !== null) {
Expand Down Expand Up @@ -560,25 +553,28 @@ if (DEBUG) {
};
}

function resolve<T, C>(registry: Registry, _normalizedName: string): Factory<T, C> | undefined {
function resolve(
registry: Registry,
_normalizedName: string
): Factory<object> | object | undefined {
let normalizedName = _normalizedName;

let cached = registry._resolveCache[normalizedName];
if (cached !== undefined) {
return cached as Factory<T, C>;
return cached as Factory<object>;
}
if (registry._failSet.has(normalizedName)) {
return;
}

let resolved: Factory<T, C> | undefined;
let resolved: Factory<object> | object | undefined;

if (registry.resolver) {
resolved = registry.resolver.resolve<T, C>(normalizedName);
resolved = registry.resolver.resolve(normalizedName);
}

if (resolved === undefined) {
resolved = registry.registrations[normalizedName] as Factory<T, C> | undefined;
resolved = registry.registrations[normalizedName];
}

if (resolved === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ class OutletComponentManager
let mountPoint = currentOwner.mountPoint;

state.engine = currentOwner;
state.engineBucket = { mountPoint };

if (mountPoint) {
state.engineBucket = { mountPoint };
}
}
}

Expand Down
Loading

0 comments on commit 47f62a5

Please sign in to comment.