X Tutup
Skip to content

Commit 358a675

Browse files
committed
feat(di): support type literals in DI
1 parent 4320859 commit 358a675

File tree

7 files changed

+155
-2
lines changed

7 files changed

+155
-2
lines changed

modules/angular2/di.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export * from './src/di/annotations';
99
export * from './src/di/decorators';
1010
export {Injector} from './src/di/injector';
1111
export {Binding, ResolvedBinding, Dependency, bind} from './src/di/binding';
12-
export {Key, KeyRegistry} from './src/di/key';
12+
export {Key, KeyRegistry, TypeLiteral} from './src/di/key';
1313
export {
1414
NoBindingError,
1515
AbstractBindingError,

modules/angular2/src/di/key.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {MapWrapper} from 'angular2/src/facade/collection';
2-
import {stringify} from 'angular2/src/facade/lang';
2+
import {stringify, CONST, Type} from 'angular2/src/facade/lang';
3+
import {TypeLiteral} from './type_literal';
4+
5+
export {TypeLiteral} from './type_literal';
36

47
// TODO: uncoment `int` once https://github.com/angular/angular/issues/1414 is fixed
58

@@ -18,6 +21,10 @@ import {stringify} from 'angular2/src/facade/lang';
1821
export class Key {
1922
token: Object;
2023
id: number;
24+
25+
/**
26+
* @private
27+
*/
2128
constructor(token: Object, id: number) {
2229
this.token = token;
2330
this.id = id;
@@ -46,6 +53,13 @@ export class KeyRegistry {
4653
get(token: Object): Key {
4754
if (token instanceof Key) return token;
4855

56+
// TODO: workaround for https://github.com/Microsoft/TypeScript/issues/3123
57+
var theToken = token;
58+
if (token instanceof TypeLiteral) {
59+
theToken = token.type;
60+
}
61+
token = theToken;
62+
4963
if (MapWrapper.contains(this._allKeys, token)) {
5064
return MapWrapper.get(this._allKeys, token);
5165
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library angular2.di.type_literal;
2+
3+
/**
4+
* Use type literals as DI keys corresponding to generic types.
5+
*
6+
* Example:
7+
*
8+
* ```
9+
* Injector.resolveAndCreate([
10+
* bind(new TypeLiteral<List<int>>()).toValue([1, 2, 3])
11+
* ]);
12+
*
13+
* class Foo {
14+
* // Delend on `List<int>` normally.
15+
* Foo(List<int> list) { ... }
16+
* }
17+
* ```
18+
*
19+
* This capability might be added to the language one day. See:
20+
*
21+
* https://code.google.com/p/dart/issues/detail?id=11923
22+
*/
23+
class TypeLiteral<T> {
24+
const TypeLiteral();
25+
Type get type => T;
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Type literals is a Dart-only feature. This is here only so we can x-compile
3+
* to multiple languages.
4+
*/
5+
export class TypeLiteral {
6+
get type(): any { throw new Error("Type literals are only supported in Dart"); }
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// This file contains tests that make sense only in Dart
2+
library angular2.test.di.integration_dart_spec;
3+
4+
import 'package:angular2/angular2.dart';
5+
import 'package:angular2/di.dart';
6+
import 'package:angular2/src/test_lib/test_bed.dart';
7+
import 'package:angular2/test_lib.dart';
8+
9+
main() {
10+
describe('TypeLiteral', () {
11+
it('should publish via injectables',
12+
inject([TestBed, AsyncTestCompleter], (tb, async) {
13+
tb.overrideView(Dummy, new View(
14+
template: '<type-literal-component></type-literal-component>',
15+
directives: [TypeLiteralComponent]
16+
));
17+
18+
tb.createView(Dummy).then((view) {
19+
view.detectChanges();
20+
expect(view.rootNodes).toHaveText('[Hello, World]');
21+
async.done();
22+
});
23+
}));
24+
});
25+
}
26+
27+
@Component(selector: 'dummy')
28+
class Dummy {}
29+
30+
@Component(
31+
selector: 'type-literal-component',
32+
injectables: const [
33+
const Binding(
34+
const TypeLiteral<List<String>>(),
35+
toValue: const <String>['Hello', 'World'])
36+
]
37+
)
38+
@View(
39+
template: '{{list}}'
40+
)
41+
class TypeLiteralComponent {
42+
final List<String> list;
43+
44+
TypeLiteralComponent(this.list);
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// This file contains tests that make sense only in Dart
2+
library angular2.test.di.injector_dart_spec;
3+
4+
import 'package:angular2/test_lib.dart';
5+
import 'package:angular2/di.dart';
6+
7+
main() {
8+
describe('Injector', () {
9+
it('should support TypeLiteral', () {
10+
var i = Injector.resolveAndCreate([
11+
bind(new TypeLiteral<List<int>>()).toValue([1, 2, 3]),
12+
Foo,
13+
]);
14+
expect(i.get(Foo).value).toEqual([1, 2, 3]);
15+
});
16+
});
17+
}
18+
19+
class Foo {
20+
final List<int> value;
21+
Foo(this.value);
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// This file contains tests that make sense only in Dart
2+
library angular2.test.di.key_dart_spec;
3+
4+
import 'package:angular2/test_lib.dart';
5+
import 'package:angular2/di.dart';
6+
7+
main() {
8+
describe('TypeLiteral', () {
9+
it('contains type', () {
10+
var t = new TypeLiteral<List<int>>();
11+
expect('${t.type}').toEqual('List<int>');
12+
});
13+
14+
it('can be a constant', () {
15+
var a = const TypeLiteral<List<int>>();
16+
var b = const TypeLiteral<List<int>>();
17+
expect(identical(a, b)).toBe(true);
18+
});
19+
20+
it('can be unique', () {
21+
var a = const TypeLiteral<List<String>>();
22+
var b = const TypeLiteral<List<int>>();
23+
expect(identical(a, b)).toBe(false);
24+
});
25+
});
26+
27+
describe('Key', () {
28+
KeyRegistry registry;
29+
30+
beforeEach(() {
31+
registry = new KeyRegistry();
32+
});
33+
34+
it('understands TypeLiteral', () {
35+
var k = registry.get(const TypeLiteral<List<int>>());
36+
expect('${k.token}').toEqual('List<int>');
37+
});
38+
});
39+
}

0 commit comments

Comments
 (0)
X Tutup