Skip to content

Commit

Permalink
Refactor Owner handling with better public API
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Feb 11, 2022
1 parent cf18a2d commit bd02b0f
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Owner } from '@ember/-internals/owner';
import { generateControllerFactory } from '@ember/-internals/routing';
import { assert } from '@ember/debug';
import EngineInstance from '@ember/engine/instance';
import { associateDestroyableChild } from '@glimmer/destroyable';
import {
Expand Down Expand Up @@ -74,6 +75,7 @@ class MountManager
// we should resolve the engine app template in the helper
// it also should use the owner that looked up the mount helper.

assert('Expected owner to be an EngineInstance', owner instanceof EngineInstance);
let engine = owner.buildChildEngineInstance(name);

engine.boot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ class OutletComponentManager
let currentOwner = valueForRef(currentStateRef)!.render!.owner;

if (parentOwner && parentOwner !== currentOwner) {
let engine = currentOwner as EngineInstance;
assert(
'Expected currentOwner to be an EngineInstance',
currentOwner instanceof EngineInstance
);

assert('invalid engine: missing mountPoint', typeof currentOwner.mountPoint === 'string');
assert('invalid engine: missing routable', currentOwner.routable === true);
let mountPoint = currentOwner.mountPoint;

let mountPoint = engine.mountPoint!;

state.engine = engine;
state.engine = currentOwner;
state.engineBucket = { mountPoint };
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/@ember/-internals/glimmer/lib/components/link-to.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Owner } from '@ember/-internals/owner';
import { Route, RouterState, RoutingService } from '@ember/-internals/routing';
import { isSimpleClick } from '@ember/-internals/views';
import { assert, debugFreeze, inspect, warn } from '@ember/debug';
Expand Down Expand Up @@ -490,11 +489,13 @@ class LinkTo extends InternalComponent {
}

private get isEngine(): boolean {
return getEngineParent(this.owner as EngineInstance) !== undefined;
let owner = this.owner;
return owner instanceof EngineInstance && getEngineParent(owner) !== undefined;
}

private get engineMountPoint(): string | undefined {
return (this.owner as Owner | EngineInstance).mountPoint;
let owner = this.owner;
return owner instanceof EngineInstance ? owner.mountPoint : undefined;
}

private classFor(state: 'active' | 'loading' | 'disabled'): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {

import { ENV } from '@ember/-internals/environment';
import { Component, setComponentManager } from '@ember/-internals/glimmer';
import { EngineInstanceOptions, Owner } from '@ember/-internals/owner';
import { Owner } from '@ember/-internals/owner';
import { Route } from '@ember/-internals/routing';
import Controller from '@ember/controller';
import { assert, captureRenderTree } from '@ember/debug';
import Engine from '@ember/engine';
import EngineInstance from '@ember/engine/instance';
import EngineInstance, { EngineInstanceOptions } from '@ember/engine/instance';
import { CapturedRenderNode } from '@glimmer/interfaces';
import { componentCapabilities, setComponentTemplate } from '@glimmer/manager';
import { templateOnlyComponent } from '@glimmer/runtime';
Expand Down
34 changes: 19 additions & 15 deletions packages/@ember/-internals/owner/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getOwner as glimmerGetOwner, setOwner as glimmerSetOwner } from '@glimmer/owner';
import { TypeOptions } from '../container/lib/registry';

/**
@module @ember/application
*/

export { TypeOptions };

export interface FactoryClass {
positionalParams?: string | string[] | undefined | null;
}
Expand All @@ -16,25 +19,26 @@ export interface Factory<T, C extends FactoryClass | object = FactoryClass> {
create(props?: { [prop: string]: any }): T;
}

export interface EngineInstanceOptions {
mountPoint: string;
routable: boolean;
}

import EngineInstance from '@ember/engine/instance';
import { TypeOptions } from '../container/lib/registry';
// A combination of the public methods on ContainerProxyMixin and RegistryProxyMixin
export interface Owner {
// From ContainerProxy
ownerInjection(): void;
lookup(fullName: string, options?: TypeOptions): unknown;
factoryFor(fullName: string): Factory<unknown> | undefined;
register(fullName: string, factory: Factory<unknown>, options?: TypeOptions): void;
hasRegistration(name: string): boolean;

/** @internal */
mountPoint?: string;
/** @internal */
routable?: boolean;
/** @internal */
buildChildEngineInstance(name: string, options?: EngineInstanceOptions): EngineInstance;
// From RegistryProxy
resolveRegistration<T, C>(fullName: string): Factory<T, C> | undefined;
register(fullName: string, factory: Factory<unknown>, options?: TypeOptions): void;
unregister(fullName: string): void;
hasRegistration(fullName: string): boolean;
registeredOption<K extends keyof TypeOptions>(
fullName: string,
optionName: K
): TypeOptions[K] | undefined;
registerOptions(fullName: string, options: TypeOptions): void;
registeredOptions(fullName: string): TypeOptions | undefined;
registerOptionsForType(type: string, options: TypeOptions): void;
registeredOptionsForType(type: string): TypeOptions | undefined;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { isProxy, lookupDescriptor, symbol } from '@ember/-internals/utils';
import Controller from '@ember/controller';
import { assert, info, isTesting } from '@ember/debug';
import EngineInstance from '@ember/engine/instance';
import { dependentKeyCompat } from '@ember/object/compat';
import { once } from '@ember/runloop';
import { DEBUG } from '@glimmer/env';
Expand Down Expand Up @@ -548,7 +549,7 @@ class Route<T = unknown>
_setRouteName(name: string) {
this.routeName = name;
let owner = getOwner(this);
assert('Route is unexpectedly missing an owner', owner);
assert('Expected route to have EngineInstance as owner', owner instanceof EngineInstance);
this.fullRouteName = getEngineRouteName(owner, name)!;
}

Expand Down Expand Up @@ -1679,7 +1680,7 @@ class Route<T = unknown>
modelFor(_name: string): unknown | undefined {
let name;
let owner = getOwner(this);
assert('Route is unexpectedly missing an owner', owner);
assert('Expected router owner to be an EngineInstance', owner instanceof EngineInstance);
let transition =
this._router && this._router._routerMicrolib
? this._router._routerMicrolib.activeTransition
Expand Down Expand Up @@ -2332,7 +2333,7 @@ function addQueryParamsObservers(controller: any, propNames: string[]) {
});
}

function getEngineRouteName(engine: Owner, routeName: string) {
function getEngineRouteName(engine: EngineInstance, routeName: string) {
if (engine.routable) {
let prefix = engine.mountPoint;

Expand Down
8 changes: 2 additions & 6 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Router, {
TransitionState,
} from 'router_js';
import { EngineRouteInfo } from './engines';
import EngineInstance from '@ember/engine/instance';

function defaultDidTransition<R extends Route>(
this: EmberRouter<R>,
Expand Down Expand Up @@ -113,11 +114,6 @@ interface OutletState<T extends RenderOutletState = RenderOutletState> {
wasUsed?: boolean;
}

interface EngineInstance extends Owner {
boot(): void;
destroy(): void;
}

export interface QueryParam {
prop: string;
urlKey: string;
Expand Down Expand Up @@ -1409,7 +1405,7 @@ class EmberRouter<R extends Route = Route> extends EmberObject.extend(Evented) i

if (!engineInstance) {
let owner = getOwner(this);
assert('Router is unexpectedly missing an owner', owner);
assert('Expected router to have EngineInstance as owner', owner instanceof EngineInstance);

assert(
`You attempted to mount the engine '${name}' in your router map, but the engine can not be found.`,
Expand Down
3 changes: 2 additions & 1 deletion packages/@ember/-internals/routing/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { get } from '@ember/-internals/metal';
import { getOwner } from '@ember/-internals/owner';
import { assert, deprecate } from '@ember/debug';
import EngineInstance from '@ember/engine/instance';
import EmberError from '@ember/error';
import Router, { STATE_SYMBOL, InternalRouteInfo, ModelFor } from 'router_js';
import Route from './system/route';
Expand Down Expand Up @@ -247,7 +248,7 @@ export function prefixRouteNameArg<T extends NamedRouteArgs<Route> | UnnamedRout
): T {
let routeName: string;
let owner = getOwner(route);
assert('Route is unexpectedly missing an owner', owner);
assert('Expected route to have EngineInstance as owner', owner instanceof EngineInstance);

let prefix = owner.mountPoint;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Container } from '@ember/-internals/container';
import { TypeOptions } from '@ember/-internals/container/lib/registry';
import { Factory } from '@ember/-internals/owner';
import Mixin from '../../types/mixin';

interface ContainerProxy {
ownerInjection: Container['ownerInjection'];
lookup: Container['lookup'];
factoryFor: Container['factoryFor'];
/** @internal */
__container__: Container;

ownerInjection(): void;
lookup(fullName: string, options?: TypeOptions): unknown;
factoryFor(fullName: string): Factory<unknown> | undefined;
}
declare const ContainerProxy: Mixin;

Expand Down
25 changes: 16 additions & 9 deletions packages/@ember/-internals/runtime/lib/mixins/registry_proxy.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Registry } from '@ember/-internals/container';
import { TypeOptions } from '@ember/-internals/container/lib/registry';
import { Factory } from '@ember/-internals/owner';
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'];
/** @internal */
__registry__: Registry;
resolveRegistration<T, C>(fullName: string): Factory<T, C> | undefined;
register(fullName: string, factory: Factory<unknown>, options?: TypeOptions): void;
unregister(fullName: string): void;
hasRegistration(fullName: string): boolean;
registeredOption<K extends keyof TypeOptions>(
fullName: string,
optionName: K
): TypeOptions[K] | undefined;
registerOptions(fullName: string, options: TypeOptions): void;
registeredOptions(fullName: string): TypeOptions | undefined;
registerOptionsForType(type: string, options: TypeOptions): void;
registeredOptionsForType(type: string): TypeOptions | undefined;
}
declare const RegistryProxyMixin: Mixin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export default Mixin.create({
@param factoryNameOrType {String}
@param property {String}
@param injectionName {String}
@deprecated
**/
inject: registryAlias('injection'),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/application/lib/application.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EngineInstanceOptions } from '@ember/-internals/owner';
import { EventDispatcher } from '@ember/-internals/views';
import Engine from '@ember/engine';
import { EngineInstanceOptions } from '@ember/engine/instance';
import ApplicationInstance, { BootOptions } from '../instance';

export default class Application extends Engine {
Expand Down
3 changes: 1 addition & 2 deletions packages/@ember/engine/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import EngineInstance from './instance';
import { EngineInstanceOptions } from '@ember/-internals/owner';
import EngineInstance, { EngineInstanceOptions } from './instance';
import { Namespace, RegistryProxyMixin } from '@ember/-internals/runtime';

export { getEngineParent } from '@ember/engine/lib/engine-parent';
Expand Down
10 changes: 8 additions & 2 deletions packages/@ember/engine/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import { Owner } from '@ember/-internals/owner';
import { BootOptions } from '@ember/application/instance';
import EmberObject from '@ember/object';

export interface EngineInstanceOptions {
mountPoint: string;
routable: boolean;
}

interface EngineInstance extends RegistryProxyMixin, ContainerProxyMixin, Owner {}

declare class EngineInstance extends EmberObject {
init(...args: unknown[]): void;
boot(options?: BootOptions): void;
unregister(fullName: string): void;
mountPoint: string;
routable: boolean;
buildChildEngineInstance(name: string, options?: EngineInstanceOptions): EngineInstance;
}

export default EngineInstance;

0 comments on commit bd02b0f

Please sign in to comment.