You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributor-docs/components/dependencies.md
+18-19Lines changed: 18 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -8,19 +8,19 @@ parent: Firebase Components
8
8
1. TOC
9
9
{:toc}
10
10
11
-
This page gives an overview of various different dependency types supported by the Components Framework.
11
+
This page gives an overview of the different dependency types supported by the Components Framework.
12
12
13
13
## Background
14
14
15
15
As discussed in [Firebase Components]({{ site.baseurl }}{% link components/components.md %}), in order
16
-
for a `Component` to be injected with the things it needs to function, it needs to declare dependencies.
16
+
for a `Component` to be injected with the things it needs to function, it has to declare its dependencies.
17
17
These dependencies are then made available and injected into `Components` at runtime.
18
18
19
-
Firebase Components provide different types of dependencies
19
+
Firebase Components provide different types of dependencies.
20
20
21
21
## Lazy vs Eager dependencies
22
22
23
-
When it comes to initializing a given component, there are 2 ways to provide its dependencies to it.
23
+
When it comes to initialize a component, there are 2 ways of provide its dependencies.
24
24
25
25
### Direct Injection
26
26
@@ -35,14 +35,14 @@ class MyComponent(private val dep : MyDep) {
35
35
```
36
36
37
37
As you can see above the component's dependency is passed by value directly,
38
-
which means that the dependency component needs to have fully initialized before
38
+
which means that the dependency needs to be fully initialized before
39
39
it's handed off to the requesting component. As a result `MyComponent` may have to pay the cost
40
-
of `MyDep`initialization just to be able to be created.
40
+
of initializing `MyDep` just to be created.
41
41
42
42
### Lazy/Provider Injection
43
43
44
-
With this type of injection, instead of getting and instance of the dependency directly, the dependency
45
-
is passed into the `Component` with the help of the`com.google.firebase.inject.Provider`
44
+
With this type of injection, instead of getting an instance of the dependency directly, the dependency
45
+
is passed into the `Component` with the help of a`com.google.firebase.inject.Provider`
46
46
47
47
```java
48
48
publicinterfaceProvider<T> { Tget(); }
@@ -58,17 +58,17 @@ class MyComponent(private val dep : Provider<MyDep>) {
58
58
}
59
59
```
60
60
61
-
On the surface this does not look like a big change, but it has an important side effect where,
62
-
in order to create an instance of `MyComponent`, there is no need to initialize `MyDep`and instead
63
-
to delay it until it's actually needed.
61
+
On the surface this does not look like a big change, but it has an important side effect. In order to create
62
+
an instance of `MyComponent`, we don't need to initialize `MyDep`anymore. Instead, initialization can be
63
+
delayed until `MyDep` is actually used.
64
64
65
65
It is also benefitial to use a `Provider` in the context of [Play's dynamic feature delivery](https://developer.android.com/guide/playcore/feature-delivery).
66
66
See [Dynamic Module Support]({{ site.baseurl }}{% link components/dynamic_modules.md %}) for more details.
67
67
68
68
## Required dependencies
69
69
70
70
This type of dependency informs the `ComponentRuntime` that a given `Component` cannot function without a dependency.
71
-
When the dependency is missing during initialization, `ComponentRuntime` will throw a `DependencyMissingDependency`.
71
+
When the dependency is missing during initialization, `ComponentRuntime` will throw a `MissingDependencyException`.
72
72
This type of dependency is useful for built-in components that are always present like `Context`, `FirebaseApp`,
73
73
`FirebaseOptions`, [Executors]({{ site.baseurl }}{% link components/executors.md %}).
74
74
@@ -89,7 +89,7 @@ This type of dependencies is useful when your `Component` can operate normally w
89
89
available, but can have enhanced functionality when present. e.g. `Firestore` can work without `Auth` but
90
90
provides secure database access when `Auth` is present.
91
91
92
-
To declare a required dependency use the following in your `ComponentRegistrar`:
92
+
To declare an optional dependency use the following in your `ComponentRegistrar`:
93
93
94
94
```java
95
95
.add(Dependency.optionalProvider(MyDep.class))
@@ -101,19 +101,18 @@ The provider will return `null` if the dependency is not present in the app.
101
101
102
102
{: .warning }
103
103
When the app uses [Play's dynamic feature delivery](https://developer.android.com/guide/playcore/feature-delivery),
104
-
`provider.get()` can return your dependency when it becomes available, so be prepared for that.
105
-
And don't store references to the result of `provider.get()` calls.
104
+
`provider.get()` will return your dependency when it becomes available. To support this use case, don't store references to the result of `provider.get()` calls.
106
105
107
106
See [Dynamic Module Support]({{ site.baseurl }}{% link components/dynamic_modules.md %}) for details
108
107
109
108
{: .warning }
110
-
See Deferred dependencies if you your dependency has a callback based apis
109
+
See Deferred dependencies if you your dependency has a callback based API
111
110
112
111
## Deferred Dependencies
113
112
114
113
Useful for optional dependencies which have a listener-style API, i.e. the dependent component registers a
115
-
listener with the dependency and never calls it again(instead the dependency will call the registered listener).
116
-
A good example is `Firestore`'s use of `Auth` where `Firestore` registers a token change listener to get
114
+
listener with the dependency and never calls it again(instead the dependency will call the registered listener).
115
+
A good example is `Firestore`'s use of `Auth`, where `Firestore` registers a token change listener to get
117
116
notified when a new token is available. The problem is that when `Firestore` initializes, `Auth` may not be
118
117
present in the app, and is instead part of a dynamic module that can be loaded at runtime on demand.
119
118
@@ -130,7 +129,7 @@ public interface Deferred<T> {
130
129
}
131
130
```
132
131
133
-
To use it a component needs to use`Dependency.deferred(SomeType.class)`:
132
+
To use it a component needs to call`Dependency.deferred(SomeType.class)`:
0 commit comments