Skip to content

Commit

Permalink
For mozilla-mobile#15279: LazyMonitored implement Lazy + update built…
Browse files Browse the repository at this point in the history
…-in API use.

By having LazyMonitored implement Lazy, we can continue to pass these
values directly into the ac APIs that require Lazy references. For some
reason, implementing `Lazy.value` can replace `operator fun getValue`
required for delegates.
  • Loading branch information
mcomella committed Oct 31, 2020
1 parent c19d700 commit 1b96b0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/org/mozilla/fenix/components/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ class Core(
// Use these for startup-path code, where we don't want to do any work that's not strictly necessary.
// For example, this is how the GeckoEngine delegates (history, logins) are configured.
// We can fully initialize GeckoEngine without initialized our storage.
val lazyHistoryStorage = lazy { PlacesHistoryStorage(context, crashReporter) }
val lazyBookmarksStorage = lazy { PlacesBookmarksStorage(context) }
val lazyPasswordsStorage = lazy { SyncableLoginsStorage(context, passwordsEncryptionKey) }
val lazyHistoryStorage = lazyMonitored { PlacesHistoryStorage(context, crashReporter) }
val lazyBookmarksStorage = lazyMonitored { PlacesBookmarksStorage(context) }
val lazyPasswordsStorage = lazyMonitored { SyncableLoginsStorage(context, passwordsEncryptionKey) }

/**
* The storage component to sync and persist tabs in a Firefox Sync account.
*/
val lazyRemoteTabsStorage = lazy { RemoteTabsStorage() }
val lazyRemoteTabsStorage = lazyMonitored { RemoteTabsStorage() }

// For most other application code (non-startup), these wrappers are perfectly fine and more ergonomic.
val historyStorage by lazyMonitored { lazyHistoryStorage.value }
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun <T> lazyMonitored(initializer: () -> T) = LazyMonitored(initializer)
* For example, we can count the number of components initialized to see how the number of
* components initialized on start up impacts start up time.
*/
class LazyMonitored<T>(initializer: () -> T) {
class LazyMonitored<T>(initializer: () -> T) : Lazy<T> {
// Lazy is thread safe.
private val lazyValue = lazy {
// We're unlikely to have 4 billion components so we don't handle overflow.
Expand All @@ -40,5 +40,6 @@ class LazyMonitored<T>(initializer: () -> T) {
}
}

operator fun getValue(thisRef: Any?, property: KProperty<*>): T = lazyValue.value
override val value: T get() = lazyValue.value
override fun isInitialized(): Boolean = lazyValue.isInitialized()
}

0 comments on commit 1b96b0f

Please sign in to comment.