Skip to content

Commit

Permalink
Future public @ember/engine types
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Feb 2, 2022
1 parent 0678c2d commit dd914f6
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 69 deletions.
10 changes: 4 additions & 6 deletions packages/@ember/-internals/container/lib/container.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Factory, LookupOptions, Owner, setOwner } from '@ember/-internals/owner';
import { Factory, Owner, setOwner } from '@ember/-internals/owner';
import { dictionary, symbol } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import Registry, { DebugRegistry } from './registry';
import Registry, { DebugRegistry, TypeOptions } from './registry';

interface LeakTracking {
hasContainers(): boolean;
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class Container {
@param {String} [options.source] The fullname of the request source (used for local lookup)
@return {any}
*/
lookup(fullName: string, options: LookupOptions): any {
lookup<T>(fullName: string, options?: TypeOptions): T | undefined {
if (this.isDestroyed) {
throw new Error(`Can not call \`.lookup\` after the owner has been destroyed`);
}
Expand Down Expand Up @@ -206,8 +206,6 @@ export default class Container {
@public
@method factoryFor
@param {String} fullName
@param {Object} [options]
@param {String} [options.source] The fullname of the request source (used for local lookup)
@return {any}
*/
factoryFor<T, C>(fullName: string): Factory<T, C> | undefined {
Expand Down Expand Up @@ -261,7 +259,7 @@ function isInstantiatable(container: Container, fullName: string) {
return container.registry.getOption(fullName, 'instantiate') !== false;
}

function lookup(container: Container, fullName: string, options: LookupOptions = {}) {
function lookup(container: Container, fullName: string, options: TypeOptions = {}) {
let normalizedName = fullName;

if (
Expand Down
26 changes: 13 additions & 13 deletions packages/@ember/-internals/container/lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface IRegistry {
fullName: string,
optionName: K
): TypeOptions[K] | undefined;
getOptions(fullName: string): TypeOptions;
getOptionsForType(type: string): TypeOptions;
getOptions(fullName: string): TypeOptions | undefined;
getOptionsForType(type: string): TypeOptions | undefined;
knownForType(type: string): KnownForTypeResult;
makeToString<T, C>(factory: Factory<T, C>, fullName: string): string;
normalizeFullName(fullName: string): string;
Expand Down Expand Up @@ -79,12 +79,12 @@ export default class Registry implements IRegistry {
readonly _failSet: Set<string>;
resolver: Resolver | (Resolve & NotResolver) | null;
readonly fallback: IRegistry | null;
readonly registrations: { [key: string]: object };
_localLookupCache: { [key: string]: object };
readonly _normalizeCache: { [key: string]: string };
readonly _options: { [key: string]: TypeOptions };
readonly _resolveCache: { [key: string]: object };
readonly _typeOptions: { [key: string]: TypeOptions };
readonly registrations: { [key: string]: object | undefined };
_localLookupCache: { [key: string]: object | undefined };
readonly _normalizeCache: { [key: string]: string | undefined };
readonly _options: { [key: string]: TypeOptions | undefined };
readonly _resolveCache: { [key: string]: object | undefined };
readonly _typeOptions: { [key: string]: TypeOptions | undefined };

constructor(options: RegistryOptions = {}) {
this.fallback = options.fallback || null;
Expand Down Expand Up @@ -182,7 +182,7 @@ export default class Registry implements IRegistry {
@param {Function} factory
@param {Object} options
*/
register<T, C>(fullName: string, factory: Factory<T, C>, options: object = {}): void {
register<T, C>(fullName: string, factory: Factory<T, C>, options: TypeOptions = {}): void {
assert('fullName must be a proper full name', this.isValidFullName(fullName));
assert(`Attempting to register an unknown factory: '${fullName}'`, factory !== undefined);

Expand Down Expand Up @@ -396,8 +396,8 @@ export default class Registry implements IRegistry {
this._typeOptions[type] = options;
}

getOptionsForType(type: string): TypeOptions {
let optionsForType = this._typeOptions[type];
getOptionsForType(type: string): TypeOptions | undefined {
let optionsForType = type in this._typeOptions ? this._typeOptions[type] : undefined;
if (optionsForType === undefined && this.fallback !== null) {
optionsForType = this.fallback.getOptionsForType(type);
}
Expand All @@ -415,9 +415,9 @@ export default class Registry implements IRegistry {
this._options[normalizedName] = options;
}

getOptions(fullName: string): TypeOptions {
getOptions(fullName: string): TypeOptions | undefined {
let normalizedName = this.normalize(fullName);
let options = this._options[normalizedName];
let options = normalizedName in this._options ? this._options[normalizedName] : undefined;

if (options === undefined && this.fallback !== null) {
options = this.fallback.getOptions(fullName);
Expand Down
3 changes: 2 additions & 1 deletion packages/@ember/-internals/glimmer/lib/components/link-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Owner } from '@ember/-internals/owner';
import { RouterState, RoutingService } from '@ember/-internals/routing';
import { isSimpleClick } from '@ember/-internals/views';
import { assert, debugFreeze, inspect, warn } from '@ember/debug';
import { EngineInstance, getEngineParent } from '@ember/engine';
import { getEngineParent } from '@ember/engine';
import EngineInstance from '@ember/engine/instance';
import { flaggedInstrument } from '@ember/instrumentation';
import { action } from '@ember/object';
import { service } from '@ember/service';
Expand Down
17 changes: 7 additions & 10 deletions packages/@ember/-internals/glimmer/lib/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { privatize as P } from '@ember/-internals/container';
import { TypeOptions } from '@ember/-internals/container/lib/registry';
import { ENV } from '@ember/-internals/environment';
import { Factory, FactoryClass, LookupOptions, Owner } from '@ember/-internals/owner';
import { Factory, FactoryClass, Owner } from '@ember/-internals/owner';
import { assert } from '@ember/debug';
import { _instrumentStart } from '@ember/instrumentation';
import { DEBUG } from '@glimmer/env';
Expand Down Expand Up @@ -56,16 +57,12 @@ function instrumentationPayload(name: string) {
return { object: `component:${name}` };
}

function componentFor(
name: string,
owner: Owner,
options?: LookupOptions
): Option<Factory<{}, {}>> {
function componentFor(name: string, owner: Owner): Option<Factory<{}, {}>> {
let fullName = `component:${name}`;
return owner.factoryFor(fullName, options) || null;
return owner.factoryFor(fullName) || null;
}

function layoutFor(name: string, owner: Owner, options?: LookupOptions): Option<Template> {
function layoutFor(name: string, owner: Owner, options?: TypeOptions): Option<Template> {
let templateFullName = `template:components/${name}`;

return owner.lookup(templateFullName, options) || null;
Expand All @@ -88,9 +85,9 @@ type LookupResult =
function lookupComponentPair(
owner: Owner,
name: string,
options?: LookupOptions
options?: TypeOptions
): Option<LookupResult> {
let component = componentFor(name, owner, options);
let component = componentFor(name, owner);

if (component !== null && component.class !== undefined) {
let layout = getComponentTemplate(component.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ if (ENV._DEBUG_RENDER_TREE) {
Resolver = ModuleBasedTestResolver;

init() {
super.init(...arguments);
super.init();
this.register(
'template:application',
compileTemplate(
Expand Down Expand Up @@ -234,7 +234,7 @@ if (ENV._DEBUG_RENDER_TREE) {
Resolver = ModuleBasedTestResolver;

init() {
super.init(...arguments);
super.init();
this.register(
'template:application',
compileTemplate(
Expand Down Expand Up @@ -579,7 +579,7 @@ if (ENV._DEBUG_RENDER_TREE) {
Resolver = ModuleBasedTestResolver;

init() {
super.init(...arguments);
super.init();
this.register(
'template:application',
compileTemplate(
Expand Down
14 changes: 5 additions & 9 deletions packages/@ember/-internals/owner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { getOwner as glimmerGetOwner, setOwner as glimmerSetOwner } from '@glimm
@module @ember/application
*/

export interface LookupOptions {
singleton?: boolean;
instantiate?: boolean;
}

export interface FactoryClass {
positionalParams?: string | string[] | undefined | null;
}
Expand All @@ -27,11 +22,12 @@ export interface EngineInstanceOptions {
}

import EngineInstance from '@ember/engine/instance';
import { TypeOptions } from '../container/lib/registry';
export interface Owner {
lookup<T>(fullName: string, options?: LookupOptions): T | undefined;
factoryFor<T, C>(fullName: string, options?: LookupOptions): Factory<T, C> | undefined;
register<T, C>(fullName: string, factory: Factory<T, C>, options?: LookupOptions): void;
hasRegistration(name: string, options?: LookupOptions): boolean;
lookup<T>(fullName: string, options?: TypeOptions): T | undefined;
factoryFor<T, C>(fullName: string): Factory<T, C> | undefined;
register<T, C>(fullName: string, factory: Factory<T, C>, options?: TypeOptions): void;
hasRegistration(name: string): boolean;

/** @internal */
mountPoint?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container } from '@ember/-internals/container';
import { LookupOptions } from '@ember/-internals/owner';
import { TypeOptions } from '@ember/-internals/container/lib/registry';
/**
@module ember
*/
Expand All @@ -14,7 +14,7 @@ import { LookupOptions } from '@ember/-internals/owner';
export default function controllerFor(
container: Container,
controllerName: string,
lookupOptions: LookupOptions
lookupOptions: TypeOptions
) {
return container.lookup(`controller:${controllerName}`, lookupOptions);
}
11 changes: 11 additions & 0 deletions packages/@ember/-internals/runtime/lib/mixins/container_proxy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Container } from '@ember/-internals/container';
import Mixin from '../../types/mixin';

interface ContainerProxy {
ownerInjection: Container['ownerInjection'];
lookup: Container['lookup'];
factoryFor: Container['factoryFor'];
}
declare const ContainerProxy: Mixin;

export { ContainerProxy as default };
17 changes: 17 additions & 0 deletions packages/@ember/-internals/runtime/lib/mixins/registry_proxy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Registry } from '@ember/-internals/container';
import Mixin from '../../types/mixin';

interface RegistryProxyMixin {
resolveRegistration: Registry['resolve'];
register: Registry['register'];
unregister: Registry['unregister'];
hasRegistration: Registry['has'];
registeredOption: Registry['getOption'];
registerOptions: Registry['options'];
registeredOptions: Registry['getOptions'];
registerOptionsForType: Registry['optionsForType'];
registeredOptionsForType: Registry['getOptionsForType'];
}
declare const RegistryProxyMixin: Mixin;

export { RegistryProxyMixin as default };
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default Mixin.create({
@param {String} fullName
@return {Function} fullName's factory
*/
resolveRegistration(fullName, options) {
resolveRegistration(fullName) {
assert('fullName must be a proper full name', this.__registry__.isValidFullName(fullName));
return this.__registry__.resolve(fullName, options);
return this.__registry__.resolve(fullName);
},

/**
Expand Down
8 changes: 2 additions & 6 deletions packages/@ember/application/instance.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EngineInstance } from '@ember/engine';
import EngineInstance from '@ember/engine/instance';

export class BootOptions {
isBrowser: boolean;
Expand All @@ -19,8 +19,4 @@ export class BootOptions {
toEnvironment(): Record<string, unknown>;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface ApplicationInstance extends EngineInstance {}
declare class ApplicationInstance {}

export default ApplicationInstance;
export default class ApplicationInstance extends EngineInstance {}
7 changes: 3 additions & 4 deletions packages/@ember/application/type-tests/instance.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Application from '@ember/application';
import ApplicationInstance, { BootOptions } from '@ember/application/instance';
import { EngineInstance } from '@ember/engine';
import EngineInstance from '@ember/engine/instance';
import EmberObject from '@ember/object';

import { expectTypeOf } from 'expect-type';
Expand All @@ -25,9 +25,8 @@ expectTypeOf(
).toEqualTypeOf<Store | undefined>();

expectTypeOf(instance.hasRegistration('service:store')).toEqualTypeOf<boolean>();
expectTypeOf(
instance.hasRegistration('service:store', { singleton: true, instantiate: true })
).toEqualTypeOf<boolean>();
// @ts-expect-error requires name
instance.hasRegistration();

expectTypeOf(instance.boot()).toEqualTypeOf<void>();

Expand Down
19 changes: 11 additions & 8 deletions packages/@ember/engine/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import EngineInstance from './instance';
import { EngineInstanceOptions, Factory } from '@ember/-internals/owner';
import { EngineInstanceOptions } from '@ember/-internals/owner';
import { Namespace, RegistryProxyMixin } from '@ember/-internals/runtime';

export function getEngineParent(instance: EngineInstance): EngineInstance | undefined;
export { getEngineParent } from '@ember/engine/lib/engine-parent';

export { EngineInstance };

export default class Engine {
constructor(...args: any[]);
init(...args: any[]): void;
register<T, C>(fullName: string, factory: Factory<T, C>, options?: object): void;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Engine extends RegistryProxyMixin {}
declare class Engine extends Namespace {
buildInstance(options?: EngineInstanceOptions): EngineInstance;
initializer: (initializer: unknown) => void;
instanceInitializer: (initializer: unknown) => void;
Resolver: unknown | null;
}

export { Engine as default };
10 changes: 7 additions & 3 deletions packages/@ember/engine/instance.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ContainerProxyMixin, RegistryProxyMixin } from '@ember/-internals/runtime';
import { Owner } from '@ember/-internals/owner';
import { BootOptions } from '@ember/application/instance';
import EmberObject from '@ember/object';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface EngineInstance extends Owner {}
interface EngineInstance extends RegistryProxyMixin, ContainerProxyMixin, Owner {}

declare class EngineInstance extends EmberObject {
boot(): void;
init(...args: unknown[]): void;
boot(options?: BootOptions): void;
unregister(fullName: string): void;
}

export default EngineInstance;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@module @ember/engine
*/
import { symbol } from '@ember/-internals/utils';
import EngineInstance from '../instance';

const ENGINE_PARENT = symbol('ENGINE_PARENT');

Expand All @@ -15,7 +16,7 @@ const ENGINE_PARENT = symbol('ENGINE_PARENT');
@static
@private
*/
export function getEngineParent(engine) {
export function getEngineParent(engine: EngineInstance): EngineInstance | undefined {
return engine[ENGINE_PARENT];
}

Expand All @@ -27,6 +28,6 @@ export function getEngineParent(engine) {
@param {EngineInstance} parent The parent engine instance.
@private
*/
export function setEngineParent(engine, parent) {
export function setEngineParent(engine: EngineInstance, parent: EngineInstance): void {
engine[ENGINE_PARENT] = parent;
}
Loading

0 comments on commit dd914f6

Please sign in to comment.