X Tutup
Skip to content

Commit 0a7d10b

Browse files
committed
refactor(core): separate reflective injector from Injector interface
BREAKING CHANGE: - Injector was renamed into `ReflectiveInjector`, as `Injector` is only an abstract class with one method on it - `Injector.getOptional()` was changed into `Injector.get(token, notFoundValue)` to make implementing injectors simpler - `ViewContainerRef.createComponent` now takes an `Injector` instead of `ResolvedProviders`. If a reflective injector should be used, create one before calling this method. (e.g. via `ReflectiveInjector.resolveAndCreate(…)`.
1 parent efbd446 commit 0a7d10b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1790
-1719
lines changed

modules/angular2/examples/core/di/ts/forward_ref/forward_ref.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import {Inject, Injector, forwardRef, resolveForwardRef, ForwardRefFn} from 'angular2/core';
1+
import {
2+
Inject,
3+
ReflectiveInjector,
4+
forwardRef,
5+
resolveForwardRef,
6+
ForwardRefFn
7+
} from 'angular2/core';
28

39
// #docregion forward_ref_fn
410
var ref = forwardRef(() => Lock);
@@ -13,7 +19,7 @@ class Door {
1319
// Only at this point Lock is defined.
1420
class Lock {}
1521

16-
var injector = Injector.resolveAndCreate([Door, Lock]);
22+
var injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);
1723
var door = injector.get(Door);
1824
expect(door instanceof Door).toBe(true);
1925
expect(door.lock instanceof Lock).toBe(true);

modules/angular2/src/compiler/runtime_metadata.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'angular2/src/facade/lang';
1212
import {StringMapWrapper} from 'angular2/src/facade/collection';
1313
import {BaseException} from 'angular2/src/facade/exceptions';
14-
import {NoAnnotationError} from 'angular2/src/core/di/exceptions';
14+
import {NoAnnotationError} from 'angular2/src/core/di/reflective_exceptions';
1515
import * as cpl from './compile_metadata';
1616
import * as md from 'angular2/src/core/metadata/directives';
1717
import * as dimd from 'angular2/src/core/metadata/di';
@@ -27,7 +27,11 @@ import {PLATFORM_DIRECTIVES, PLATFORM_PIPES} from 'angular2/src/core/platform_di
2727
import {MODULE_SUFFIX} from './util';
2828
import {assertArrayOfStrings} from './assertions';
2929
import {getUrlScheme} from 'angular2/src/compiler/url_resolver';
30-
import {Provider, constructDependencies, Dependency} from 'angular2/src/core/di/provider';
30+
import {Provider} from 'angular2/src/core/di/provider';
31+
import {
32+
constructDependencies,
33+
ReflectiveDependency
34+
} from 'angular2/src/core/di/reflective_provider';
3135
import {
3236
OptionalMetadata,
3337
SelfMetadata,
@@ -185,7 +189,7 @@ export class RuntimeMetadataResolver {
185189

186190
getDependenciesMetadata(typeOrFunc: Type | Function,
187191
dependencies: any[]): cpl.CompileDiDependencyMetadata[] {
188-
var deps: Dependency[];
192+
var deps: ReflectiveDependency[];
189193
try {
190194
deps = constructDependencies(typeOrFunc, dependencies);
191195
} catch (e) {

modules/angular2/src/compiler/view_compiler/util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ export function getPropertyInView(property: o.Expression, viewPath: CompileView[
3131

3232
export function injectFromViewParentInjector(token: CompileTokenMetadata,
3333
optional: boolean): o.Expression {
34-
var method = optional ? 'getOptional' : 'get';
35-
return o.THIS_EXPR.prop('parentInjector').callMethod(method, [createDiTokenExpression(token)]);
34+
var args = [createDiTokenExpression(token)];
35+
if (optional) {
36+
args.push(o.NULL_EXPR);
37+
}
38+
return o.THIS_EXPR.prop('parentInjector').callMethod('get', args);
3639
}
3740

3841
export function getViewFactoryName(component: CompileDirectiveMetadata,

modules/angular2/src/core/application_ref.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
print,
88
IS_DART
99
} from 'angular2/src/facade/lang';
10-
import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
10+
import {provide, Provider, Injector, ReflectiveInjector, OpaqueToken} from 'angular2/src/core/di';
1111
import {
1212
APP_COMPONENT_REF_PROMISE,
1313
APP_COMPONENT,
@@ -48,7 +48,7 @@ function _componentProviders(appComponentType: Type): Array<Type | Provider | an
4848
() => { appRef._unloadComponent(ref); })
4949
.then((componentRef) => {
5050
ref = componentRef;
51-
var testability = injector.getOptional(Testability);
51+
var testability = injector.get(Testability, null);
5252
if (isPresent(testability)) {
5353
injector.get(TestabilityRegistry)
5454
.registerApplication(componentRef.location.nativeElement, testability);
@@ -115,7 +115,7 @@ export function disposePlatform(): void {
115115

116116
function _createPlatform(providers?: Array<Type | Provider | any[]>): PlatformRef {
117117
_platformProviders = providers;
118-
let injector = Injector.resolveAndCreate(providers);
118+
let injector = ReflectiveInjector.resolveAndCreate(providers);
119119
_platform = new PlatformRef_(injector, () => {
120120
_platform = null;
121121
_platformProviders = null;
@@ -125,7 +125,7 @@ function _createPlatform(providers?: Array<Type | Provider | any[]>): PlatformRe
125125
}
126126

127127
function _runPlatformInitializers(injector: Injector): void {
128-
let inits: Function[] = <Function[]>injector.getOptional(PLATFORM_INITIALIZER);
128+
let inits: Function[] = <Function[]>injector.get(PLATFORM_INITIALIZER, null);
129129
if (isPresent(inits)) inits.forEach(init => init());
130130
}
131131

@@ -201,11 +201,11 @@ export class PlatformRef_ extends PlatformRef {
201201
/** @internal */
202202
_disposeListeners: Function[] = [];
203203

204-
constructor(private _injector: Injector, private _dispose: () => void) { super(); }
204+
constructor(private _injector: ReflectiveInjector, private _dispose: () => void) { super(); }
205205

206206
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
207207

208-
get injector(): Injector { return this._injector; }
208+
get injector(): ReflectiveInjector { return this._injector; }
209209

210210
application(providers: Array<Type | Provider | any[]>): ApplicationRef {
211211
var app = this._initApp(createNgZone(), providers);
@@ -239,7 +239,7 @@ export class PlatformRef_ extends PlatformRef {
239239
private _initApp(zone: NgZone,
240240
providers: Array<Type | Provider | any[]>): Promise<ApplicationRef>|
241241
ApplicationRef {
242-
var injector: Injector;
242+
var injector: ReflectiveInjector;
243243
var app: ApplicationRef;
244244
zone.run(() => {
245245
providers = ListWrapper.concat(providers, [
@@ -283,7 +283,7 @@ export class PlatformRef_ extends PlatformRef {
283283
}
284284

285285
function _runAppInitializers(injector: Injector): Promise<any> {
286-
let inits: Function[] = injector.getOptional(APP_INITIALIZER);
286+
let inits: Function[] = injector.get(APP_INITIALIZER, null);
287287
let promises: Promise<any>[] = [];
288288
if (isPresent(inits)) {
289289
inits.forEach(init => {
@@ -390,7 +390,8 @@ export class ApplicationRef_ extends ApplicationRef {
390390
/** @internal */
391391
private _enforceNoNewChanges: boolean = false;
392392

393-
constructor(private _platform: PlatformRef_, private _zone: NgZone, private _injector: Injector) {
393+
constructor(private _platform: PlatformRef_, private _zone: NgZone,
394+
private _injector: ReflectiveInjector) {
394395
super();
395396
if (isPresent(this._zone)) {
396397
ObservableWrapper.subscribe(this._zone.onMicrotaskEmpty,

modules/angular2/src/core/di.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ export * from './di/decorators';
2020
export {forwardRef, resolveForwardRef, ForwardRefFn} from './di/forward_ref';
2121

2222
export {Injector} from './di/injector';
23+
export {ReflectiveInjector} from './di/reflective_injector';
2324
export {
2425
Binding,
2526
ProviderBuilder,
26-
ResolvedBinding,
27-
ResolvedFactory,
28-
Dependency,
2927
bind,
3028

3129
Provider,
32-
ResolvedProvider,
3330
provide
3431
} from './di/provider';
35-
export {Key} from './di/key';
32+
export {
33+
ResolvedReflectiveBinding,
34+
ResolvedReflectiveFactory,
35+
ReflectiveDependency,
36+
37+
ResolvedReflectiveProvider
38+
} from './di/reflective_provider';
39+
export {ReflectiveKey} from './di/reflective_key';
3640
export {
3741
NoProviderError,
3842
AbstractProviderError,
@@ -41,5 +45,5 @@ export {
4145
InvalidProviderError,
4246
NoAnnotationError,
4347
OutOfBoundsError
44-
} from './di/exceptions';
48+
} from './di/reflective_exceptions';
4549
export {OpaqueToken} from './di/opaque_token';

0 commit comments

Comments
 (0)
X Tutup