Skip to content

Commit

Permalink
Sync 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
ykmnkmi committed Dec 13, 2024
1 parent 95dcbf1 commit 8e00e3d
Showing 1 changed file with 79 additions and 41 deletions.
120 changes: 79 additions & 41 deletions ngdart/lib/src/meta/di_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ import '../di/injector.dart';
/// ## Example
///
/// ```
/// class Engine {}
/// class Engine {
/// const Engine();
/// }
///
/// const engine = Engine();
///
/// @Injectable()
/// class Car {
/// Car(@Inject(Engine) this.engine);
///
/// final Engine engine;
/// Car(@Inject("MyEngine") this.engine);
/// }
///
/// var engine = Engine();
///
/// var injector = Injector.resolveAndCreate([
/// Provider("MyEngine", useValue: engine),
/// Car
/// ]);
/// @GenerateInjector([
/// ValueProvider(Engine, engine),
/// ClassProvider(Car),
/// ])
/// final InjectorFactory injector = ng.injector$Injector;
///
/// expect(injector.get(Car).engine, same(engine));
/// Car car = injector(root).get(Car);
/// expect(car.engine, same(engine));
/// ```
///
/// When `@Inject()` is not present, [Injector] will use the type annotation of
Expand All @@ -33,13 +37,18 @@ import '../di/injector.dart';
/// ```
/// class Engine {}
///
/// @Injectable()
/// class Car {
/// Car(Engine engine) {} // same as Car(@Inject(Engine) Engine engine)
/// }
///
/// var injector = Injector.resolveAndCreate([Engine, Car]);
/// expect(injector.get(Car).engine, isInstanceOf<Engine>());
/// @GenerateInjector([
/// ClassProvider(Engine),
/// ClassProvider(Car),
/// ])
/// final InjectorFactory injector = ng.injector$Injector;
///
/// Car car = injector(root).get(Car);
/// expect(car.engine, isA<Engine>());
/// ```
@Target({TargetKind.parameter})
class Inject {
Expand Down Expand Up @@ -100,7 +109,7 @@ class Inject {
///
/// ```
/// // Could be put anywhere DI providers are allowed.
/// const FactoryProvider(MyService, createMyService);
/// FactoryProvider(MyService, createMyService);
///
/// // A `Provide` may now use `createMyService` via `useFactory`.
/// @Injectable()
Expand All @@ -115,6 +124,7 @@ class Inject {
TargetKind.function,
TargetKind.method,
})
@Deprecated('It does nothing but throw on invalid uses.')
class Injectable {
const Injectable();
}
Expand All @@ -128,14 +138,16 @@ class Injectable {
/// ```
/// class Engine {}
///
/// @Injectable()
/// class Car {
/// final Engine engine;
/// final Engine? engine;
/// Car(@Optional() this.engine);
/// }
///
/// var injector = Injector.resolveAndCreate([Car]);
/// expect(injector.get(Car).engine, isNull);
/// @GenerateInjector([ClassProvider(Car)])
/// final InjectorFactory injector = ng.injector$Injector;
///
/// Car car = injector(root).get(Car);
/// expect(car.engine, isNull);
/// ```
@Target({TargetKind.parameter})
class Optional {
Expand All @@ -149,20 +161,31 @@ class Optional {
/// ```
/// class Dependency {}
///
/// @Injectable()
/// class NeedsDependency {
/// final Dependency dependency;
/// NeedsDependency(@Self() this.dependency);
/// }
///
/// var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]);
/// var nd = inj.get(NeedsDependency);
/// @GenerateInjector([
/// ClassProvider(Dependency),
/// ClassProvider(NeedsDependency),
/// ])
/// final InjectorFactory injector = ng.injector$Injector;
///
/// NeedsDependency needsDependency = injector(root).get(NeedsDependency);
/// expect(needsDependency.dependency, isA<Dependency>());
///
/// @GenerateInjector([
/// ClassProvider(Dependency),
/// ])
/// final InjectorFactory parent = ng.parent$Injector;
///
/// expect(nd.dependency, new isInstanceOf<Dependency>());
/// @GenerateInjector([
/// ClassProvider(NeedsDependency),
/// ])
/// final InjectorFactory child = ng.child$Injector;
///
/// inj = Injector.resolveAndCreate([Dependency]);
/// var child = inj.resolveAndCreateChild([NeedsDependency]);
/// expect(() => child.get(NeedsDependency), throws);
/// expect(() => child(parent(root)).get(NeedsDependency), throwsA(anything));
/// ```
@Target({TargetKind.parameter})
class Self {
Expand All @@ -177,19 +200,31 @@ class Self {
/// ```
/// class Dependency {}
///
/// @Injectable()
/// class NeedsDependency {
/// final Dependency dependency;
/// NeedsDependency(@SkipSelf() this.dependency);
/// }
///
/// var parent = Injector.resolveAndCreate([Dependency]);
/// var child = parent.resolveAndCreateChild([NeedsDependency]);
/// expect(child.get(NeedsDependency).dependency, new
/// isInstanceOf<Dependency>());
/// @GenerateInjector([
/// ClassProvider(Dependency),
/// ])
/// final InjectorFactory parent = ng.parent$Injector;
///
/// @GenerateInjector([
/// ClassProvider(NeedsDependency),
/// ])
/// final InjectorFactory child = ng.child$Injector;
///
/// NeedsDependency needsDependency = child(parent(root)).get(NeedsDependency)
/// expect(needsDependency.dependency, isA<Dependency>());
///
/// var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]);
/// expect(() => inj.get(NeedsDependency), throws);
/// @GenerateInjector([
/// ClassProvider(Dependency),
/// ClassProvider(NeedsDependency),
/// ])
/// final InjectorFactory injector = ng.injector$Injector;
///
/// expect(() => injector(root).get(NeedsDependency), throwsA(anything));
/// ```
@Target({TargetKind.parameter})
class SkipSelf {
Expand All @@ -211,43 +246,46 @@ class SkipSelf {
/// `HostService` is defined at `ParentCmp`, and `OtherService` is defined at
/// `App`.
///
///```dart
///```
/// class OtherService {}
/// class HostService {}
///
/// @Directive(
/// selector: 'child-directive'
/// selector: 'child-directive',
/// )
/// class ChildDirective {
/// ChildDirective(
/// @Optional() @Host() OtherService os,
/// @Optional() @Host() HostService hs) {
/// print("os is null", os);
/// print("hs is NOT null", hs);
/// @Optional() @Host() OtherService? os,
/// @Optional() @Host() HostService? hs,
/// ) {
/// print('os is null: $os');
/// print('hs is NOT null: $hs');
/// }
/// }
///
/// @Component(
/// selector: 'parent-cmp',
/// directives: [ChildDirective],
/// providers: [HostService],
/// template: '''
/// Dir: <child-directive></child-directive>
/// ''',
/// directives: [ChildDirective]
/// )
/// class ParentCmp {}
///
/// @Component(
/// selector: 'app',
/// directives: [ParentCmp]
/// providers: [OtherService],
/// template: '''
/// Parent: <parent-cmp></parent-cmp>
/// ''',
/// directives: [ParentCmp]
/// )
/// class App {}
///
/// bootstrap(App);
/// void main() {
/// runApp(ng.AppNgFactory);
/// }
///```
@Target({TargetKind.parameter})
class Host {
Expand Down

0 comments on commit 8e00e3d

Please sign in to comment.