Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 17 | @file:OptIn( |
| 18 | InternalComposeApi::class, |
| 19 | ExperimentalComposeApi::class, |
| 20 | ComposeCompilerApi::class |
| 21 | ) |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 22 | package androidx.compose |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 23 | |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 24 | import androidx.compose.SlotTable.Companion.EMPTY |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 25 | import androidx.compose.tooling.InspectionTables |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 26 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 27 | internal typealias Change<N> = ( |
| 28 | applier: Applier<N>, |
| 29 | slots: SlotWriter, |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 30 | lifecycleManager: LifecycleManager |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 31 | ) -> Unit |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 32 | |
| 33 | private class GroupInfo( |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 34 | /** |
| 35 | * The current location of the slot relative to the start location of the pending slot changes |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 36 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 37 | var slotIndex: Int, |
| 38 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 39 | /** |
| 40 | * The current location of the first node relative the start location of the pending node |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 41 | * changes |
| 42 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 43 | var nodeIndex: Int, |
| 44 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 45 | /** |
| 46 | * The current number of nodes the group contains after changes have been applied |
| 47 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 48 | var nodeCount: Int |
| 49 | ) |
| 50 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 51 | internal interface LifecycleManager { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 52 | fun entering(instance: CompositionLifecycleObserver) |
| 53 | fun leaving(instance: CompositionLifecycleObserver) |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 56 | /** |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 57 | * Pending starts when the key is different than expected indicating that the structure of the tree |
| 58 | * changed. It is used to determine how to update the nodes and the slot table when changes to the |
| 59 | * structure of the tree is detected. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 60 | */ |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 61 | private class Pending( |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 62 | val keyInfos: MutableList<KeyInfo>, |
| 63 | val startIndex: Int |
| 64 | ) { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 65 | var groupIndex: Int = 0 |
| 66 | |
| 67 | init { |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 68 | require(startIndex >= 0) { "Invalid start index" } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 69 | } |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 70 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 71 | private val usedKeys = mutableListOf<KeyInfo>() |
| 72 | private val groupInfos = run { |
| 73 | var runningNodeIndex = 0 |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 74 | val result = hashMapOf<Group, GroupInfo>() |
Chuck Jazdzewski | a7f2c87 | 2020-03-05 13:49:29 -0800 | [diff] [blame] | 75 | for (index in 0 until keyInfos.size) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 76 | val keyInfo = keyInfos[index] |
| 77 | result[keyInfo.group] = GroupInfo(index, runningNodeIndex, keyInfo.nodes) |
| 78 | runningNodeIndex += keyInfo.nodes |
Mikhail Levchenko | 059f6b6 | 2019-05-08 19:55:44 +0300 | [diff] [blame] | 79 | } |
Chuck Jazdzewski | a7f2c87 | 2020-03-05 13:49:29 -0800 | [diff] [blame] | 80 | result |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 84 | * A multi-map of keys from the previous composition. The keys can be retrieved in the order |
| 85 | * they were generated by the previous composition. |
| 86 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 87 | val keyMap by lazy { |
| 88 | multiMap<Any, KeyInfo>().also { |
Chuck Jazdzewski | a7f2c87 | 2020-03-05 13:49:29 -0800 | [diff] [blame] | 89 | for (index in 0 until keyInfos.size) { |
| 90 | val keyInfo = keyInfos[index] |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 91 | @Suppress("ReplacePutWithAssignment") |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 92 | it.put(keyInfo.joinedKey, keyInfo) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the next key information for the given key. |
| 99 | */ |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 100 | fun getNext(key: Int, dataKey: Any?): KeyInfo? { |
| 101 | val joinedKey: Any = if (dataKey != null) JoinedKey(key, dataKey) else key |
| 102 | return keyMap.pop(joinedKey) |
| 103 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Record that this key info was generated. |
| 107 | */ |
| 108 | fun recordUsed(keyInfo: KeyInfo) = usedKeys.add(keyInfo) |
| 109 | |
| 110 | val used: List<KeyInfo> get() = usedKeys |
| 111 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 112 | // TODO(chuckj): This is a correct but expensive implementation (worst cases of O(N^2)). Rework |
| 113 | // to O(N) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 114 | fun registerMoveSlot(from: Int, to: Int) { |
| 115 | if (from > to) { |
| 116 | groupInfos.values.forEach { group -> |
| 117 | val position = group.slotIndex |
| 118 | if (position == from) group.slotIndex = to |
| 119 | else if (position in to until from) group.slotIndex = position + 1 |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 120 | } |
| 121 | } else if (to > from) { |
| 122 | groupInfos.values.forEach { group -> |
| 123 | val position = group.slotIndex |
| 124 | if (position == from) group.slotIndex = to |
| 125 | else if (position in (from + 1) until to) group.slotIndex = position - 1 |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | fun registerMoveNode(from: Int, to: Int, count: Int) { |
| 131 | if (from > to) { |
| 132 | groupInfos.values.forEach { group -> |
| 133 | val position = group.nodeIndex |
| 134 | if (position in from until from + count) group.nodeIndex = to + (position - from) |
| 135 | else if (position in to until from) group.nodeIndex = position + count |
| 136 | } |
| 137 | } else if (to > from) { |
| 138 | groupInfos.values.forEach { group -> |
| 139 | val position = group.nodeIndex |
| 140 | if (position in from until from + count) group.nodeIndex = to + (position - from) |
| 141 | else if (position in (from + 1) until to) group.nodeIndex = position - count |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fun registerInsert(keyInfo: KeyInfo, insertIndex: Int) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 147 | groupInfos[keyInfo.group] = GroupInfo(-1, insertIndex, 0) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 150 | fun updateNodeCount(group: Group, newCount: Int): Boolean { |
| 151 | val groupInfo = groupInfos[group] |
| 152 | if (groupInfo != null) { |
| 153 | val index = groupInfo.nodeIndex |
| 154 | val difference = newCount - groupInfo.nodeCount |
| 155 | groupInfo.nodeCount = newCount |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 156 | if (difference != 0) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 157 | groupInfos.values.forEach { childGroupInfo -> |
| 158 | if (childGroupInfo.nodeIndex >= index && childGroupInfo != groupInfo) |
| 159 | childGroupInfo.nodeIndex += difference |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 160 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 161 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 162 | return true |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 163 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 164 | return false |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 167 | fun slotPositionOf(keyInfo: KeyInfo) = groupInfos[keyInfo.group]?.slotIndex ?: -1 |
| 168 | fun nodePositionOf(keyInfo: KeyInfo) = groupInfos[keyInfo.group]?.nodeIndex ?: -1 |
| 169 | fun updatedNodeCountOf(keyInfo: KeyInfo) = groupInfos[keyInfo.group]?.nodeCount ?: keyInfo.nodes |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Leland Richardson | 9138cbe | 2019-04-24 14:53:02 -0700 | [diff] [blame] | 172 | private class Invalidation( |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 173 | val scope: RecomposeScope, |
Chuck Jazdzewski | c9e0efe | 2019-05-13 16:02:27 -0700 | [diff] [blame] | 174 | var location: Int |
Leland Richardson | 9138cbe | 2019-04-24 14:53:02 -0700 | [diff] [blame] | 175 | ) |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 176 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 177 | @ComposeCompilerApi |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 178 | interface ScopeUpdateScope { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 179 | fun updateScope(block: (Composer<*>, Int, Int) -> Unit) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 182 | internal enum class InvalidationResult { |
| 183 | /** |
| 184 | * The invalidation was ignored because the associated recompose scope is no longer part of the |
| 185 | * composition or has yet to be entered in the composition. This could occur for invalidations |
| 186 | * called on scopes that are no longer part of composition or if the scope was invalidated |
| 187 | * before the applyChanges() was called that will enter the scope into the composition. |
| 188 | */ |
| 189 | IGNORED, |
| 190 | |
| 191 | /** |
| 192 | * The composition is not currently composing and the invalidation was recorded for a future |
| 193 | * composition. A recomposition requested to be scheduled. |
| 194 | */ |
| 195 | SCHEDULED, |
| 196 | |
| 197 | /** |
| 198 | * The composition that owns the recompose scope is actively composing but the scope has |
| 199 | * already been composed or is in the process of composing. The invalidation is treated as |
| 200 | * SCHEDULED above. |
| 201 | */ |
| 202 | DEFERRED, |
| 203 | |
| 204 | /** |
| 205 | * The composition that owns the recompose scope is actively composing and the invalidated |
| 206 | * scope has not been composed yet but will be recomposed before the composition completes. A |
| 207 | * new recomposition was not scheduled for this invalidation. |
| 208 | */ |
| 209 | IMMINENT |
| 210 | } |
| 211 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 212 | /** |
| 213 | * A RecomposeScope is created for a region of the composition that can be recomposed independently |
| 214 | * of the rest of the composition. The composer will position the slot table to the location |
| 215 | * stored in [anchor] and call [block] when recomposition is requested. It is created by |
| 216 | * [Composer.startRestartGroup] and is used to track how to restart the group. |
| 217 | */ |
Chuck Jazdzewski | 7d86900 | 2020-06-24 13:31:48 -0700 | [diff] [blame] | 218 | internal class RecomposeScope(var composer: Composer<*>?, val key: Int) : ScopeUpdateScope { |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 219 | /** |
| 220 | * An anchor to the location in the slot table that start the group associated with this |
| 221 | * recompose scope. This value is set by [composer] when the scope is committed to the slot |
| 222 | * table by [Composer.applyChanges]. |
| 223 | */ |
Chuck Jazdzewski | ab3c952 | 2019-08-20 12:41:34 -0700 | [diff] [blame] | 224 | var anchor: Anchor? = null |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Return whether the scope is valid. A scope becomes invalid when the slots it updates are |
| 228 | * removed from the slot table. For example, if the scope is in the then clause of an if |
| 229 | * statement that later becomes false. |
| 230 | */ |
Chuck Jazdzewski | 7d86900 | 2020-06-24 13:31:48 -0700 | [diff] [blame] | 231 | val valid: Boolean get() = composer != null && anchor?.valid ?: false |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Used is set when the [RecomposeScope] is used by, for example, [invalidate]. This is used |
| 235 | * as the result of [Composer.endRestartGroup] and indicates whether the lambda that is stored |
| 236 | * in [block] will be used. |
| 237 | */ |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 238 | var used = false |
| 239 | |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 240 | var defaultsInScope = false |
| 241 | var defaultsInvalid = false |
| 242 | |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 243 | var requiresRecompose = false |
| 244 | |
Chuck Jazdzewski | 2a13c35 | 2020-03-19 13:50:58 -0700 | [diff] [blame] | 245 | /** |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 246 | * The lambda to call to restart the scopes composition. |
| 247 | */ |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 248 | private var block: ((Composer<*>, Int, Int) -> Unit)? = null |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 249 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Restart the scope's composition. It is an error if [block] was not updated. The code |
| 252 | * generated by the compiler ensures that when the recompose scope is used then [block] will |
| 253 | * be set but if might occur if the compiler is out-of-date (or ahead of the runtime) or |
| 254 | * incorrect direct calls to [Composer.startRestartGroup] and [Composer.endRestartGroup]. |
| 255 | */ |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 256 | fun <N> compose(composer: Composer<N>) { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 257 | block?.invoke(composer, key, 1) ?: error("Invalid restart scope") |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Invalidate the group which will cause [composer] to request this scope be recomposed. |
| 262 | */ |
Chuck Jazdzewski | 7d86900 | 2020-06-24 13:31:48 -0700 | [diff] [blame] | 263 | fun invalidate(): InvalidationResult = composer?.invalidate(this) ?: InvalidationResult.IGNORED |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 264 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Update [block]. The scope is returned by [Composer.endRestartGroup] when [used] is true |
| 267 | * and implements [ScopeUpdateScope]. |
| 268 | */ |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 269 | override fun updateScope(block: (Composer<*>, Int, Int) -> Unit) { this.block = block } |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 272 | /** |
| 273 | * An instance to hold a value provided by [Providers] and is created by the |
| 274 | * [ProvidableAmbient.provides] infixed operator. |
| 275 | */ |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 276 | class ProvidedValue<T> internal constructor(val ambient: Ambient<T>, val value: T) |
| 277 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 278 | /** |
| 279 | * An ambient map is is an immutable map that maps ambient keys to a provider of their current |
| 280 | * value. It is used both to represent the values provided by a [Providers] call and the combined |
| 281 | * scope of all provided ambients. |
| 282 | */ |
Chuck Jazdzewski | 7d0ef18 | 2020-01-23 14:46:22 -0800 | [diff] [blame] | 283 | internal typealias AmbientMap = BuildableMap<Ambient<Any?>, State<Any?>> |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 284 | |
| 285 | @Suppress("UNCHECKED_CAST") |
| 286 | internal fun <T> AmbientMap.contains(key: Ambient<T>) = this.containsKey(key as Ambient<Any?>) |
| 287 | |
| 288 | @Suppress("UNCHECKED_CAST") |
| 289 | internal fun <T> AmbientMap.getValueOf(key: Ambient<T>) = this[key as Ambient<Any?>]?.value as T |
| 290 | |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 291 | @Composable |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 292 | private fun ambientMapOf(values: Array<out ProvidedValue<*>>): AmbientMap { |
| 293 | val result: AmbientMap = buildableMapOf() |
| 294 | return result.mutate { |
| 295 | for (provided in values) { |
| 296 | @Suppress("UNCHECKED_CAST") |
| 297 | it[provided.ambient as Ambient<Any?>] = provided.ambient.provided(provided.value) |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Implementation of a composer for mutable tree. |
| 304 | */ |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 305 | class Composer<N>( |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Backing storage for the composition |
| 308 | */ |
Chuck Jazdzewski | e7725b9 | 2019-06-24 08:51:16 -0700 | [diff] [blame] | 309 | val slotTable: SlotTable, |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * An adapter that applies changes to the tree using the Applier abstraction. |
| 313 | */ |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 314 | @ComposeCompilerApi |
| 315 | val applier: Applier<N>, |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * Manager for scheduling recompositions. |
| 319 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 320 | @ExperimentalComposeApi |
Adam Powell | c916742 | 2020-04-08 16:50:37 -0700 | [diff] [blame] | 321 | val recomposer: Recomposer |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 322 | ) { |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 323 | init { |
| 324 | FrameManager.ensureStarted() |
| 325 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 326 | private val changes = mutableListOf<Change<N>>() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 327 | private val lifecycleObservers = HashMap< |
| 328 | CompositionLifecycleObserverHolder, |
| 329 | CompositionLifecycleObserverHolder |
| 330 | >() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 331 | private val pendingStack = Stack<Pending?>() |
| 332 | private var pending: Pending? = null |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 333 | private var nodeIndex: Int = 0 |
| 334 | private var nodeIndexStack = IntStack() |
| 335 | private var groupNodeCount: Int = 0 |
| 336 | private var groupNodeCountStack = IntStack() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 337 | private val nodeCountOverrides = HashMap<Group, Int>() |
Chuck Jazdzewski | e7725b9 | 2019-06-24 08:51:16 -0700 | [diff] [blame] | 338 | private var collectKeySources = false |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 339 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 340 | private var nodeExpected = false |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 341 | private val invalidations: MutableList<Invalidation> = mutableListOf() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 342 | private val entersStack = IntStack() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 343 | private var parentProvider: AmbientMap = buildableMapOf() |
| 344 | private val providerUpdates = HashMap<Group, AmbientMap>() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 345 | private var providersInvalid = false |
| 346 | private val providersInvalidStack = IntStack() |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 347 | |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 348 | private val invalidateStack = Stack<RecomposeScope>() |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 349 | |
| 350 | internal var parentReference: CompositionReference? = null |
Leland Richardson | 0b5bf70 | 2019-05-24 17:07:21 -0700 | [diff] [blame] | 351 | internal var isComposing = false |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 352 | |
Matvei Malkov | 0b5d596 | 2019-04-24 13:50:35 +0100 | [diff] [blame] | 353 | private val changesAppliedObservers = mutableListOf<() -> Unit>() |
| 354 | |
| 355 | private fun dispatchChangesAppliedObservers() { |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 356 | trace("Compose:dispatchChangesAppliedObservers") { |
| 357 | val listeners = changesAppliedObservers.toTypedArray() |
| 358 | changesAppliedObservers.clear() |
| 359 | listeners.forEach { it() } |
| 360 | } |
Matvei Malkov | 0b5d596 | 2019-04-24 13:50:35 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | internal fun addChangesAppliedObserver(l: () -> Unit) { |
| 364 | changesAppliedObservers.add(l) |
| 365 | } |
| 366 | |
| 367 | internal fun removeChangesAppliedObserver(l: () -> Unit) { |
| 368 | changesAppliedObservers.remove(l) |
| 369 | } |
| 370 | |
Chuck Jazdzewski | 67e5927 | 2020-04-15 15:58:20 -0700 | [diff] [blame] | 371 | private var reader: SlotReader = slotTable.openReader().also { it.close() } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 372 | |
| 373 | private val insertTable = SlotTable() |
| 374 | private var writer: SlotWriter = insertTable.openWriter().also { it.close() } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 375 | private var hasProvider = false |
Chuck Jazdzewski | 67e5927 | 2020-04-15 15:58:20 -0700 | [diff] [blame] | 376 | private var insertAnchor: Anchor = insertTable.anchor(0) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 377 | private val insertFixups = mutableListOf<Change<N>>() |
Chuck Jazdzewski | 0a9f7fa | 2018-12-20 16:55:46 -0800 | [diff] [blame] | 378 | |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 379 | @InternalComposeApi |
| 380 | fun composeRoot(block: () -> Unit) { |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 381 | startRoot() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 382 | startGroup(invocationKey, invocation) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 383 | block() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 384 | endGroup() |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 385 | endRoot() |
Chuck Jazdzewski | 0a9f7fa | 2018-12-20 16:55:46 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 388 | /** |
| 389 | * Inserts a "Replaceable Group" starting marker in the slot table at the current execution |
| 390 | * position. A Replaceable Group is a group which cannot be moved between its siblings, but |
| 391 | * can be removed or inserted. These groups are inserted by the compiler around branches of |
| 392 | * conditional logic in Composable functions such as if expressions, when expressions, early |
| 393 | * returns, and null-coalescing operators. |
| 394 | * |
| 395 | * A call to [startReplaceableGroup] must be matched with a corresponding call to |
| 396 | * [endReplaceableGroup]. |
| 397 | * |
| 398 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 399 | * directly from source code. Call this API at your own risk. |
| 400 | * |
| 401 | * @param key The source-location-based key for the group. Expected to be unique among its |
| 402 | * siblings. |
| 403 | * |
| 404 | * @see [endReplaceableGroup] |
| 405 | * @see [startMovableGroup] |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 406 | * @see [startRestartGroup] |
| 407 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 408 | @ComposeCompilerApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 409 | fun startReplaceableGroup(key: Int) = start(key, null, false, null) |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 410 | |
Chuck Jazdzewski | 2ee9227 | 2020-07-07 08:35:12 -0700 | [diff] [blame] | 411 | @ComposeCompilerApi |
| 412 | fun startReplaceableGroup(key: Int, sourceInformation: String?) = |
| 413 | start(key, null, false, sourceInformation) |
| 414 | |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Indicates the end of a "Replaceable Group" at the current execution position. A |
| 417 | * Replaceable Group is a group which cannot be moved between its siblings, but |
| 418 | * can be removed or inserted. These groups are inserted by the compiler around branches of |
| 419 | * conditional logic in Composable functions such as if expressions, when expressions, early |
| 420 | * returns, and null-coalescing operators. |
| 421 | * |
| 422 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 423 | * directly from source code. Call this API at your own risk. |
| 424 | * |
| 425 | * @see [startReplaceableGroup] |
| 426 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 427 | @ComposeCompilerApi |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 428 | fun endReplaceableGroup() = endGroup() |
| 429 | |
| 430 | /** |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 431 | * |
| 432 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 433 | * directly from source code. Call this API at your own risk. |
| 434 | * |
| 435 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 436 | @ComposeCompilerApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 437 | fun startDefaults() = start(0, null, false, null) |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 438 | |
| 439 | /** |
| 440 | * |
| 441 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 442 | * directly from source code. Call this API at your own risk. |
| 443 | * |
| 444 | * @see [startReplaceableGroup] |
| 445 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 446 | @ComposeCompilerApi |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 447 | fun endDefaults() { |
| 448 | endGroup() |
| 449 | val scope = currentRecomposeScope |
| 450 | if (scope != null && scope.used) { |
| 451 | scope.defaultsInScope = true |
| 452 | } |
| 453 | } |
| 454 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 455 | @ComposeCompilerApi |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 456 | val defaultsInvalid: Boolean |
| 457 | get() { |
| 458 | return providersInvalid || currentRecomposeScope?.defaultsInvalid == true |
| 459 | } |
| 460 | |
| 461 | /** |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 462 | * Inserts a "Movable Group" starting marker in the slot table at the current execution |
| 463 | * position. A Movable Group is a group which can be moved or reordered between its siblings |
Leland Richardson | b4482ed | 2020-03-15 22:15:44 -0700 | [diff] [blame] | 464 | * and retain slot table state, in addition to being removed or inserted. Movable Groups |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 465 | * are more expensive than other groups because when they are encountered with a mismatched |
| 466 | * key in the slot table, they must be held on to temporarily until the entire parent group |
| 467 | * finishes execution in case it moved to a later position in the group. Movable groups are |
| 468 | * only inserted by the compiler as a result of calls to [key]. |
| 469 | * |
| 470 | * A call to [startMovableGroup] must be matched with a corresponding call to [endMovableGroup]. |
| 471 | * |
| 472 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 473 | * directly from source code. Call this API at your own risk. |
| 474 | * |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 475 | * @param key The source-location-based key for the group. Expected to be unique among its |
| 476 | * siblings. |
| 477 | * |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 478 | * @param dataKey Additional identifying information to compound with [key]. If there are |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 479 | * multiple values, this is expected to be compounded together with [joinKey]. Whatever value |
| 480 | * is passed in here is expected to have a meaningful [equals] and [hashCode] implementation. |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 481 | * |
| 482 | * @see [endMovableGroup] |
| 483 | * @see [key] |
| 484 | * @see [joinKey] |
| 485 | * @see [startReplaceableGroup] |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 486 | * @see [startRestartGroup] |
| 487 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 488 | @ComposeCompilerApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 489 | fun startMovableGroup(key: Int, dataKey: Any?) = start(key, dataKey, false, null) |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 490 | |
Chuck Jazdzewski | 2ee9227 | 2020-07-07 08:35:12 -0700 | [diff] [blame] | 491 | @ComposeCompilerApi |
| 492 | fun startMovableGroup(key: Int, dataKey: Any?, sourceInformation: String?) = |
| 493 | start(key, dataKey, false, sourceInformation) |
| 494 | |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 495 | /** |
| 496 | * Indicates the end of a "Movable Group" at the current execution position. A Movable Group is |
| 497 | * a group which can be moved or reordered between its siblings and retain slot table state, |
| 498 | * in addition to being removed or inserted. These groups are only valid when they are |
| 499 | * inserted as direct children of Container Groups. Movable Groups are more expensive than |
| 500 | * other groups because when they are encountered with a mismatched key in the slot table, |
| 501 | * they must be held on to temporarily until the entire parent group finishes execution in |
| 502 | * case it moved to a later position in the group. Movable groups are only inserted by the |
| 503 | * compiler as a result of calls to [key]. |
| 504 | * |
| 505 | * Warning: This is expected to be executed by the compiler only and should not be called |
| 506 | * directly from source code. Call this API at your own risk. |
| 507 | * |
| 508 | * @see [startMovableGroup] |
| 509 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 510 | @ComposeCompilerApi |
Leland Richardson | cb229cb | 2020-03-11 19:07:23 -0700 | [diff] [blame] | 511 | fun endMovableGroup() = endGroup() |
| 512 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 513 | /** |
| 514 | * Start the composition. This should be called, and only be called, as the first group in |
| 515 | * the composition. |
| 516 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 517 | internal fun startRoot() { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 518 | reader = slotTable.openReader() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 519 | startGroup(rootKey) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 520 | parentReference?.let { parentRef -> |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 521 | parentProvider = parentRef.getAmbientScope() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 522 | providersInvalidStack.push(providersInvalid.asInt()) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 523 | providersInvalid = changed(parentProvider) |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 524 | collectKeySources = parentRef.collectingKeySources |
| 525 | resolveAmbient(InspectionTables, parentProvider)?.let { |
| 526 | it.add(slotTable) |
| 527 | parentRef.recordInspectionTable(it) |
| 528 | } |
Chuck Jazdzewski | 8e7017f | 2020-06-05 11:21:24 -0700 | [diff] [blame] | 529 | startGroup(parentRef.compoundHashKey) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 530 | } |
Chuck Jazdzewski | 0a9f7fa | 2018-12-20 16:55:46 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 533 | /** |
| 534 | * End the composition. This should be called, and only be called, to end the first group in |
| 535 | * the composition. |
| 536 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 537 | internal fun endRoot() { |
Chuck Jazdzewski | 8e7017f | 2020-06-05 11:21:24 -0700 | [diff] [blame] | 538 | if (parentReference != null) { |
| 539 | endGroup() |
| 540 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 541 | endGroup() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 542 | recordEndRoot() |
Chuck Jazdzewski | 006734c | 2018-09-21 09:32:38 -0700 | [diff] [blame] | 543 | finalizeCompose() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 544 | reader.close() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 547 | /** |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 548 | * Discard a pending composition because an error was encountered during composition |
| 549 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 550 | internal fun abortRoot() { |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 551 | cleanUpCompose() |
| 552 | pendingStack.clear() |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 553 | nodeIndexStack.clear() |
| 554 | groupNodeCountStack.clear() |
| 555 | entersStack.clear() |
| 556 | providersInvalidStack.clear() |
| 557 | invalidateStack.clear() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 558 | reader.close() |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 559 | currentCompoundKeyHash = 0 |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 560 | nodeExpected = false |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 564 | * True if the composition is currently scheduling nodes to be inserted into the tree. During |
| 565 | * first composition this is always true. During recomposition this is true when new nodes |
| 566 | * are being scheduled to be added to the tree. |
| 567 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 568 | @ComposeCompilerApi |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 569 | var inserting: Boolean = false |
| 570 | private set |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 571 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 572 | /** |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 573 | * True if the composition should be checking if the composable functions can be skipped. |
| 574 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 575 | @ComposeCompilerApi |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 576 | val skipping: Boolean get() { |
| 577 | return !inserting && |
| 578 | !providersInvalid && |
| 579 | currentRecomposeScope?.requiresRecompose == false |
| 580 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 581 | |
| 582 | /** |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 583 | * Returns the hash of the compound key calculated as a combination of the keys of all the |
| 584 | * currently started groups via [startGroup]. |
| 585 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 586 | @ExperimentalComposeApi |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 587 | var currentCompoundKeyHash: Int = 0 |
| 588 | private set |
| 589 | |
| 590 | /** |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 591 | * Start collecting key source information. This enables enables the tool API to be able to |
| 592 | * determine the source location of where groups and nodes are created. |
| 593 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 594 | @InternalComposeApi |
Chuck Jazdzewski | e7725b9 | 2019-06-24 08:51:16 -0700 | [diff] [blame] | 595 | fun collectKeySourceInformation() { |
| 596 | collectKeySources = true |
| 597 | } |
| 598 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 599 | /** |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 600 | * Helper for collecting lifecycle enter and leave events for later strictly ordered dispatch. |
| 601 | * [lifecycleObservers] should be the long-lived set of observers tracked over time; brand new |
| 602 | * additions or leaves from this set will be tracked by the [LifecycleManager] callback methods. |
| 603 | * Call [dispatchLifecycleObservers] to invoke the observers in LIFO order - last in, |
| 604 | * first out. |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 605 | */ |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 606 | private class LifecycleEventDispatcher( |
| 607 | private val lifecycleObservers: MutableMap<CompositionLifecycleObserverHolder, |
| 608 | CompositionLifecycleObserverHolder> |
| 609 | ) : LifecycleManager { |
| 610 | private val enters = mutableSetOf<CompositionLifecycleObserverHolder>() |
| 611 | private val leaves = mutableSetOf<CompositionLifecycleObserverHolder>() |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 612 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 613 | override fun entering(instance: CompositionLifecycleObserver) { |
| 614 | val holder = CompositionLifecycleObserverHolder(instance) |
| 615 | lifecycleObservers.getOrPut(holder) { |
| 616 | enters.add(holder) |
| 617 | holder |
| 618 | }.apply { count++ } |
| 619 | } |
| 620 | |
| 621 | override fun leaving(instance: CompositionLifecycleObserver) { |
| 622 | val holder = CompositionLifecycleObserverHolder(instance) |
| 623 | val left = lifecycleObservers[holder]?.let { |
| 624 | if (--it.count == 0) { |
| 625 | leaves.add(it) |
| 626 | it |
| 627 | } else null |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 628 | } |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 629 | if (left != null) lifecycleObservers.remove(left) |
| 630 | } |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 631 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 632 | fun dispatchLifecycleObservers() { |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 633 | trace("Compose:lifecycles") { |
| 634 | // Send lifecycle leaves |
Chuck Jazdzewski | a7f2c87 | 2020-03-05 13:49:29 -0800 | [diff] [blame] | 635 | if (leaves.isNotEmpty()) { |
| 636 | for (holder in leaves.reversed()) { |
| 637 | // The count of the holder might be greater than 0 here as it might leave one |
| 638 | // part of the composition and reappear in another. Only send a leave if the |
| 639 | // count is still 0 after all changes have been applied. |
| 640 | if (holder.count == 0) { |
| 641 | holder.instance.onLeave() |
| 642 | lifecycleObservers.remove(holder) |
| 643 | } |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | // Send lifecycle enters |
Chuck Jazdzewski | a7f2c87 | 2020-03-05 13:49:29 -0800 | [diff] [blame] | 648 | if (enters.isNotEmpty()) { |
| 649 | for (holder in enters) { |
| 650 | holder.instance.onEnter() |
| 651 | } |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 656 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 657 | /** |
| 658 | * Apply the changes to the tree that were collected during the last composition. |
| 659 | */ |
| 660 | @InternalComposeApi |
| 661 | fun applyChanges() { |
| 662 | trace("Compose:applyChanges") { |
| 663 | invalidateStack.clear() |
| 664 | val invalidationAnchors = invalidations.map { slotTable.anchor(it.location) to it } |
| 665 | val manager = LifecycleEventDispatcher(lifecycleObservers) |
| 666 | |
| 667 | // Apply all changes |
| 668 | slotTable.write { slots -> |
| 669 | changes.forEach { change -> change(applier, slots, manager) } |
| 670 | changes.clear() |
| 671 | } |
| 672 | |
| 673 | providerUpdates.clear() |
| 674 | |
| 675 | @Suppress("ReplaceManualRangeWithIndicesCalls") // Avoids allocation of an iterator |
| 676 | for (index in 0 until invalidationAnchors.size) { |
| 677 | val (anchor, invalidation) = invalidationAnchors[index] |
| 678 | invalidation.location = slotTable.anchorLocation(anchor) |
| 679 | } |
| 680 | |
| 681 | manager.dispatchLifecycleObservers() |
| 682 | dispatchChangesAppliedObservers() |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | internal fun dispose() { |
| 687 | trace("Compose:Composer.dispose") { |
| 688 | parentReference?.unregisterComposer(this) |
| 689 | invalidateStack.clear() |
| 690 | invalidations.clear() |
| 691 | changes.clear() |
| 692 | applier.clear() |
| 693 | if (slotTable.size > 0) { |
| 694 | val manager = LifecycleEventDispatcher(lifecycleObservers) |
| 695 | slotTable.write { writer -> |
| 696 | writer.removeCurrentGroup(manager) |
| 697 | } |
| 698 | providerUpdates.clear() |
| 699 | manager.dispatchLifecycleObservers() |
| 700 | } |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 701 | dispatchChangesAppliedObservers() |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 702 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 705 | /** |
| 706 | * Start a group with the given key. During recomposition if the currently expected group does |
| 707 | * not match the given key a group the groups emitted in the same parent group are inspected |
| 708 | * to determine if one of them has this key and that group the first such group is moved |
| 709 | * (along with any nodes emitted by the group) to the current position and composition |
| 710 | * continues. If no group with this key is found, then the composition shifts into insert |
| 711 | * mode and new nodes are added at the current position. |
| 712 | * |
| 713 | * @param key The key for the group |
| 714 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 715 | internal fun startGroup(key: Int) = start(key, null, false, null) |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 716 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 717 | internal fun startGroup(key: Int, dataKey: Any?) = start(key, dataKey, false, null) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 718 | |
| 719 | /** |
| 720 | * End the current group. |
| 721 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 722 | internal fun endGroup() = end(false) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 723 | |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 724 | private fun skipGroup() { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 725 | groupNodeCount += reader.skipGroup() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Start emitting a node. It is required that one of [emitNode] or [createNode] is called |
| 730 | * after [startNode]. Similar to [startGroup], if, during recomposition, the current node |
| 731 | * does not have the provided key a node with that key is scanned for and moved into the |
| 732 | * current position if found, if no such node is found the composition switches into insert |
| 733 | * mode and a the node is scheduled to be inserted at the current location. |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 734 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 735 | @ComposeCompilerApi |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 736 | fun startNode() { |
| 737 | start(nodeKey, null, true, null) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 738 | nodeExpected = true |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 741 | /** |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 742 | * Schedule a node to be created and inserted at the current location. This is only valid to |
| 743 | * call when the composer is inserting. |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 744 | */ |
| 745 | @Suppress("UNUSED") |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 746 | @ComposeCompilerApi |
Chuck Jazdzewski | f17fd4d | 2019-08-06 10:18:54 -0700 | [diff] [blame] | 747 | fun <T : N> createNode(factory: () -> T) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 748 | validateNodeExpected() |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 749 | check(inserting) { "createNode() can only be called when inserting" } |
Chuck Jazdzewski | 08d023b | 2018-09-27 10:54:14 -0700 | [diff] [blame] | 750 | val insertIndex = nodeIndexStack.peek() |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 751 | // see emitNode |
Chuck Jazdzewski | 08d023b | 2018-09-27 10:54:14 -0700 | [diff] [blame] | 752 | groupNodeCount++ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 753 | recordFixup { applier, slots, _ -> |
Chuck Jazdzewski | 08d023b | 2018-09-27 10:54:14 -0700 | [diff] [blame] | 754 | val node = factory() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 755 | slots.node = node |
Chuck Jazdzewski | 08d023b | 2018-09-27 10:54:14 -0700 | [diff] [blame] | 756 | applier.insert(insertIndex, node) |
| 757 | applier.down(node) |
| 758 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 759 | // Allocate a slot that will be fixed-up by the above operation. |
| 760 | writer.skip() |
Chuck Jazdzewski | 08d023b | 2018-09-27 10:54:14 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 763 | /** |
| 764 | * Schedule the given node to be inserted. This is only valid to call when the composer is |
| 765 | * inserting. |
| 766 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 767 | @ComposeCompilerApi |
Chuck Jazdzewski | ef18c63 | 2020-06-30 08:29:29 -0700 | [diff] [blame] | 768 | fun emitNode(node: Any?) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 769 | validateNodeExpected() |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 770 | check(inserting) { "emitNode() called when not inserting" } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 771 | val insertIndex = nodeIndexStack.peek() |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 772 | // see emitNode |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 773 | groupNodeCount++ |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 774 | @Suppress("UNCHECKED_CAST") |
| 775 | writer.node = node as N |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 776 | recordApplierOperation { applier, _, _ -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 777 | applier.insert(insertIndex, node) |
| 778 | applier.down(node) |
| 779 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 782 | /** |
| 783 | * Return the instance of the node that was inserted at the given location. This is only |
| 784 | * valid to call when the composition is not inserting. This must be called at the same |
| 785 | * location as [emitNode] or [createNode] as called even if the value is unused. |
| 786 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 787 | @ComposeCompilerApi |
Chuck Jazdzewski | f17fd4d | 2019-08-06 10:18:54 -0700 | [diff] [blame] | 788 | fun useNode(): N { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 789 | validateNodeExpected() |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 790 | check(!inserting) { "useNode() called while inserting" } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 791 | val result = reader.node |
| 792 | recordDown(result) |
| 793 | return result |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 796 | /** |
| 797 | * Called to end the node group. |
| 798 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 799 | @ComposeCompilerApi |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 800 | fun endNode() = end(true) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 801 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 802 | /** |
| 803 | * Schedule a change to be applied to a node's property. This change will be applied to the |
| 804 | * node that is the current node in the tree which was either created by [createNode], |
| 805 | * emitted by [emitNode] or returned by [useNode]. |
| 806 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 807 | internal fun <V, T> apply(value: V, block: T.(V) -> Unit) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 808 | recordApplierOperation { applier, _, _ -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 809 | @Suppress("UNCHECKED_CAST") |
| 810 | (applier.current as T).block(value) |
| 811 | } |
| 812 | } |
| 813 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 814 | /** |
| 815 | * Create a composed key that can be used in calls to [startGroup] or [startNode]. This will |
| 816 | * use the key stored at the current location in the slot table to avoid allocating a new key. |
| 817 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 818 | @ComposeCompilerApi |
Chuck Jazdzewski | f17fd4d | 2019-08-06 10:18:54 -0700 | [diff] [blame] | 819 | fun joinKey(left: Any?, right: Any?): Any = |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 820 | getKey(reader.groupDataKey, left, right) ?: JoinedKey(left, right) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 821 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 822 | /** |
| 823 | * Return the next value in the slot table and advance the current location. |
| 824 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 825 | @ComposeCompilerApi |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 826 | fun nextSlot(): Any? = if (inserting) { |
| 827 | validateNodeNotExpected() |
| 828 | EMPTY |
| 829 | } else reader.next() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 830 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 831 | /** |
| 832 | * Determine if the current slot table value is equal to the given value, if true, the value |
| 833 | * is scheduled to be skipped during [applyChanges] and [changes] return false; otherwise |
| 834 | * [applyChanges] will update the slot table to [value]. In either case the composer's slot |
| 835 | * table is advanced. |
| 836 | * |
| 837 | * @param value the value to be compared. |
| 838 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 839 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 840 | fun changed(value: Any?): Boolean { |
| 841 | return if (nextSlot() != value) { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 842 | updateValue(value) |
| 843 | true |
| 844 | } else { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 845 | false |
| 846 | } |
| 847 | } |
| 848 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 849 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 850 | fun changed(value: Char): Boolean { |
| 851 | val next = nextSlot() |
| 852 | if (next is Char) { |
| 853 | val nextPrimitive: Char = next |
| 854 | if (value == nextPrimitive) return false |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 855 | } |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 856 | updateValue(value) |
| 857 | return true |
| 858 | } |
| 859 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 860 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 861 | fun changed(value: Byte): Boolean { |
| 862 | val next = nextSlot() |
| 863 | if (next is Byte) { |
| 864 | val nextPrimitive: Byte = next |
| 865 | if (value == nextPrimitive) return false |
| 866 | } |
| 867 | updateValue(value) |
| 868 | return true |
| 869 | } |
| 870 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 871 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 872 | fun changed(value: Short): Boolean { |
| 873 | val next = nextSlot() |
| 874 | if (next is Short) { |
| 875 | val nextPrimitive: Short = next |
| 876 | if (value == nextPrimitive) return false |
| 877 | } |
| 878 | updateValue(value) |
| 879 | return true |
| 880 | } |
| 881 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 882 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 883 | fun changed(value: Boolean): Boolean { |
| 884 | val next = nextSlot() |
| 885 | if (next is Boolean) { |
| 886 | val nextPrimitive: Boolean = next |
| 887 | if (value == nextPrimitive) return false |
| 888 | } |
| 889 | updateValue(value) |
| 890 | return true |
| 891 | } |
| 892 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 893 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 894 | fun changed(value: Float): Boolean { |
| 895 | val next = nextSlot() |
| 896 | if (next is Float) { |
| 897 | val nextPrimitive: Float = next |
| 898 | if (value == nextPrimitive) return false |
| 899 | } |
| 900 | updateValue(value) |
| 901 | return true |
| 902 | } |
| 903 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 904 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 905 | fun changed(value: Long): Boolean { |
| 906 | val next = nextSlot() |
| 907 | if (next is Long) { |
| 908 | val nextPrimitive: Long = next |
| 909 | if (value == nextPrimitive) return false |
| 910 | } |
| 911 | updateValue(value) |
| 912 | return true |
| 913 | } |
| 914 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 915 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 916 | fun changed(value: Double): Boolean { |
| 917 | val next = nextSlot() |
| 918 | if (next is Double) { |
| 919 | val nextPrimitive: Double = next |
| 920 | if (value == nextPrimitive) return false |
| 921 | } |
| 922 | updateValue(value) |
| 923 | return true |
| 924 | } |
| 925 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 926 | @ComposeCompilerApi |
Leland Richardson | 7d07865 | 2020-06-02 09:05:03 -0700 | [diff] [blame] | 927 | fun changed(value: Int): Boolean { |
| 928 | val next = nextSlot() |
| 929 | if (next is Int) { |
| 930 | val nextPrimitive: Int = next |
| 931 | if (value == nextPrimitive) return false |
| 932 | } |
| 933 | updateValue(value) |
| 934 | return true |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 937 | /** |
Leland Richardson | 02aa60d | 2020-07-17 15:55:09 -0700 | [diff] [blame] | 938 | * Cache a value in the composition. During initial composition [block] is called to produce the |
| 939 | * value that is then stored in the slot table. During recomposition, if [invalid] is false |
| 940 | * the value is obtained from the slot table and [block] is not invoked. If [invalid] is |
| 941 | * false a new value is produced by calling [block] and the slot table is updated to contain |
| 942 | * the new value. |
| 943 | */ |
| 944 | @ComposeCompilerApi |
| 945 | inline fun <T> cache(invalid: Boolean, block: () -> T): T { |
| 946 | var result = nextSlot() |
| 947 | if (result === EMPTY || invalid) { |
| 948 | val value = block() |
| 949 | updateValue(value) |
| 950 | result = value |
| 951 | } |
| 952 | |
| 953 | @Suppress("UNCHECKED_CAST") |
| 954 | return result as T |
| 955 | } |
| 956 | |
| 957 | /** |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 958 | * Schedule the current value in the slot table to be updated to [value]. |
| 959 | * |
| 960 | * @param value the value to schedule to be written to the slot table. |
| 961 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 962 | @PublishedApi |
| 963 | internal fun updateValue(value: Any?) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 964 | if (inserting) { |
| 965 | writer.update(value) |
| 966 | if (value is CompositionLifecycleObserver) { |
| 967 | record { _, _, lifecycleManager -> lifecycleManager.entering(value) } |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 968 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 969 | } else { |
| 970 | recordSlotTableOperation(-1) { _, slots, lifecycleManager -> |
| 971 | if (value is CompositionLifecycleObserver) |
| 972 | lifecycleManager.entering(value) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 973 | when (val previous = slots.update(value)) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 974 | is CompositionLifecycleObserver -> |
| 975 | lifecycleManager.leaving(previous) |
| 976 | is RecomposeScope -> |
Chuck Jazdzewski | 7d86900 | 2020-06-24 13:31:48 -0700 | [diff] [blame] | 977 | previous.composer = null |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | // Advance the writers reader location to account for the update above. |
| 981 | writersReaderDelta++ |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 982 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 985 | /** |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 986 | * Return the current ambient scope which was provided by a parent group. |
| 987 | */ |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 988 | private fun currentAmbientScope(): AmbientMap { |
| 989 | if (inserting && hasProvider) { |
| 990 | var group: Group? = writer.group(writer.parentLocation) |
| 991 | while (group != null) { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 992 | if (group.key == ambientMapKey && group.dataKey === ambientMap) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 993 | @Suppress("UNCHECKED_CAST") |
| 994 | return group.data as AmbientMap |
| 995 | } |
| 996 | group = group.parent |
| 997 | } |
| 998 | } |
| 999 | if (slotTable.size > 0) { |
| 1000 | var group: Group? = reader.group(reader.parentLocation) |
| 1001 | while (group != null) { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1002 | if (group.key == ambientMapKey && group.dataKey === ambientMap) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1003 | @Suppress("UNCHECKED_CAST") |
| 1004 | return providerUpdates[group] ?: group.data as AmbientMap |
| 1005 | } |
| 1006 | group = group.parent |
| 1007 | } |
| 1008 | } |
| 1009 | return parentProvider |
| 1010 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1011 | |
| 1012 | /** |
| 1013 | * Return the ambient scope for the location provided. If this is while the composer is |
| 1014 | * composing then this is a query from a sub-composition that is being recomposed by this |
| 1015 | * compose which might be inserting the sub-composition. In that case the current scope |
| 1016 | * is the correct scope. |
| 1017 | */ |
| 1018 | private fun ambientScopeAt(location: Int): AmbientMap { |
| 1019 | if (isComposing) { |
| 1020 | // The sub-composer is being composed as part of a nested composition then use the |
| 1021 | // current ambient scope as the one in the slot table might be out of date. |
| 1022 | return currentAmbientScope() |
| 1023 | } |
| 1024 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1025 | if (location >= 0) { |
| 1026 | var group: Group? = slotTable.read { it.group(location) } |
| 1027 | while (group != null) { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1028 | if (group.key == ambientMapKey && group.dataKey === ambientMap) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1029 | @Suppress("UNCHECKED_CAST") |
| 1030 | return providerUpdates[group] ?: group.data as AmbientMap |
| 1031 | } |
| 1032 | group = group.parent |
| 1033 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1034 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1035 | return parentProvider |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Update (or create) the slots to record the providers. The providers maps are first the |
| 1040 | * scope followed by the map used to augment the parent scope. Both are needed to detect |
| 1041 | * inserts, updates and deletes to the providers. |
| 1042 | */ |
| 1043 | private fun updateProviderMapGroup( |
| 1044 | parentScope: AmbientMap, |
| 1045 | currentProviders: AmbientMap |
| 1046 | ): AmbientMap { |
| 1047 | val providerScope = parentScope.mutate { it.putAll(currentProviders) } |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1048 | startGroup(providerMapsKey, providerMaps) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1049 | changed(providerScope) |
| 1050 | changed(currentProviders) |
| 1051 | endGroup() |
| 1052 | return providerScope |
| 1053 | } |
| 1054 | |
| 1055 | internal fun startProviders(values: Array<out ProvidedValue<*>>) { |
| 1056 | val parentScope = currentAmbientScope() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1057 | startGroup(providerKey, provider) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1058 | // The group is needed here because ambientMapOf() might change the number or kind of |
| 1059 | // slots consumed depending on the content of values to remember, for example, the value |
| 1060 | // holders used last time. |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1061 | startGroup(providerValuesKey, providerValues) |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 1062 | val currentProviders = invokeComposableForResult(this) { ambientMapOf(values) } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1063 | endGroup() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1064 | val providers: AmbientMap |
| 1065 | val invalid: Boolean |
| 1066 | if (inserting) { |
| 1067 | providers = updateProviderMapGroup(parentScope, currentProviders) |
| 1068 | invalid = false |
| 1069 | hasProvider = true |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1070 | } else { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1071 | val current = reader.current |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1072 | |
| 1073 | @Suppress("UNCHECKED_CAST") |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1074 | val oldScope = reader.get(current + 1) as AmbientMap |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1075 | |
| 1076 | @Suppress("UNCHECKED_CAST") |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1077 | val oldValues = reader.get(current + 2) as AmbientMap |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1078 | |
| 1079 | // skipping is true iff parentScope has not changed. |
| 1080 | if (!skipping || oldValues != currentProviders) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1081 | providers = updateProviderMapGroup(parentScope, currentProviders) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1082 | |
| 1083 | // Compare against the old scope as currentProviders might have modified the scope |
| 1084 | // back to the previous value. This could happen, for example, if currentProviders |
| 1085 | // and parentScope have a key in common and the oldScope had the same value as |
| 1086 | // currentProviders for that key. If the scope has not changed, because these |
| 1087 | // providers obscure a change in the parent as described above, re-enable skipping |
| 1088 | // for the child region. |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1089 | invalid = providers != oldScope |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1090 | } else { |
| 1091 | // Nothing has changed |
| 1092 | skipGroup() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1093 | providers = oldScope |
| 1094 | invalid = false |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1095 | } |
Leland Richardson | 9f5fe96 | 2019-04-10 11:57:36 -0700 | [diff] [blame] | 1096 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1097 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1098 | if (invalid && !inserting) { |
| 1099 | providerUpdates[reader.group] = providers |
| 1100 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1101 | providersInvalidStack.push(providersInvalid.asInt()) |
| 1102 | providersInvalid = invalid |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1103 | start(ambientMapKey, ambientMap, false, providers) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1106 | internal fun endProviders() { |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1107 | endGroup() |
| 1108 | endGroup() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1109 | providersInvalid = providersInvalidStack.pop().asBool() |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1112 | @PublishedApi |
| 1113 | internal fun <T> consume(key: Ambient<T>): T = resolveAmbient(key, currentAmbientScope()) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1114 | |
| 1115 | /** |
Leland Richardson | 3c0ed0c | 2019-04-18 17:11:48 -0700 | [diff] [blame] | 1116 | * Create or use a memoized `CompositionReference` instance at this position in the slot table. |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1117 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1118 | internal fun buildReference(): CompositionReference { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1119 | startGroup(referenceKey, reference) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1120 | |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 1121 | var ref = nextSlot() as? CompositionReferenceHolder<*> |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1122 | if (ref == null || !inserting) { |
Chuck Jazdzewski | ab3c952 | 2019-08-20 12:41:34 -0700 | [diff] [blame] | 1123 | val scope = invalidateStack.peek() |
| 1124 | scope.used = true |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 1125 | ref = CompositionReferenceHolder( |
| 1126 | CompositionReferenceImpl(scope, currentCompoundKeyHash, collectKeySources) |
| 1127 | ) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1128 | updateValue(ref) |
| 1129 | } |
| 1130 | endGroup() |
| 1131 | |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 1132 | return ref.ref |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1135 | private fun <T> resolveAmbient(key: Ambient<T>, scope: AmbientMap): T { |
| 1136 | if (scope.contains(key)) return scope.getValueOf(key) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1137 | |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 1138 | val ref = parentReference |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1139 | |
| 1140 | if (ref != null) { |
| 1141 | return ref.getAmbient(key) |
| 1142 | } |
| 1143 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1144 | return key.defaultValueHolder.value |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1147 | internal fun <T> parentAmbient(key: Ambient<T>): T = resolveAmbient(key, currentAmbientScope()) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1148 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1149 | private fun <T> parentAmbient(key: Ambient<T>, location: Int): T = |
| 1150 | resolveAmbient(key, ambientScopeAt(location)) |
Leland Richardson | b0c2ea2 | 2018-12-18 18:56:41 -0800 | [diff] [blame] | 1151 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1152 | /** |
| 1153 | * The number of changes that have been scheduled to be applied during [applyChanges]. |
| 1154 | * |
| 1155 | * Slot table movement (skipping groups and nodes) will be coalesced so this number is |
| 1156 | * possibly less than the total changes detected. |
| 1157 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1158 | internal val changeCount get() = changes.size |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1159 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1160 | internal val currentRecomposeScope: RecomposeScope? |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 1161 | get() = |
| 1162 | invalidateStack.let { if (it.isNotEmpty()) it.peek() else null } |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 1163 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1164 | private fun ensureWriter() { |
| 1165 | if (writer.closed) { |
| 1166 | writer = insertTable.openWriter() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1167 | hasProvider = false |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Start the reader group updating the data of the group if necessary |
| 1173 | */ |
| 1174 | private fun startReaderGroup(isNode: Boolean, data: Any?) { |
| 1175 | if (isNode) { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1176 | reader.startNode() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1177 | } else { |
| 1178 | if (data != null && reader.groupData !== data) { |
| 1179 | recordSlotEditingOperation { _, slots, _ -> |
| 1180 | slots.updateData(data) |
| 1181 | } |
| 1182 | } |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1183 | if (data != null) |
| 1184 | reader.startDataGroup() |
| 1185 | else |
| 1186 | reader.startGroup() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1190 | private fun start(key: Int, dataKey: Any?, isNode: Boolean, data: Any?) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1191 | validateNodeNotExpected() |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1192 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1193 | updateCompoundKeyWhenWeEnterGroup(key, dataKey) |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 1194 | |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1195 | // Check for the insert fast path. If we are already inserting (creating nodes) then |
| 1196 | // there is no need to track insert, deletes and moves with a pending changes object. |
| 1197 | if (inserting) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1198 | reader.beginEmpty() |
Chuck Jazdzewski | e7725b9 | 2019-06-24 08:51:16 -0700 | [diff] [blame] | 1199 | if (collectKeySources) |
| 1200 | recordSourceKeyInfo(key) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1201 | when { |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 1202 | isNode -> writer.startNode(null) |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1203 | data != null -> writer.startData(key, dataKey, data) |
| 1204 | else -> writer.startGroup(key, dataKey) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1205 | } |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1206 | pending?.let { pending -> |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1207 | val insertKeyInfo = KeyInfo(key, -1, 0, -1, 0, writer.parentGroup) |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1208 | pending.registerInsert(insertKeyInfo, nodeIndex - pending.startIndex) |
| 1209 | pending.recordUsed(insertKeyInfo) |
| 1210 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1211 | enterGroup(isNode, null) |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1212 | return |
| 1213 | } |
| 1214 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1215 | if (pending == null) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1216 | val slotKey = reader.groupKey |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1217 | if (slotKey == key && dataKey == reader.groupDataKey) { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1218 | // The group is the same as what was generated last time. |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1219 | startReaderGroup(isNode, data) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1220 | } else { |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1221 | pending = Pending( |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1222 | reader.extractKeys(), |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1223 | nodeIndex |
| 1224 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | val pending = pending |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1229 | var newPending: Pending? = null |
| 1230 | if (pending != null) { |
| 1231 | // Check to see if the key was generated last time from the keys collected above. |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1232 | val keyInfo = pending.getNext(key, dataKey) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1233 | if (keyInfo != null) { |
| 1234 | // This group was generated last time, use it. |
| 1235 | pending.recordUsed(keyInfo) |
| 1236 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1237 | // Move the slot table to the location where the information about this group is |
| 1238 | // stored. The slot information will move once the changes are applied so moving the |
| 1239 | // current of the slot table is sufficient. |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1240 | val location = keyInfo.location |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1241 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1242 | // Determine what index this group is in. This is used for inserting nodes into the |
| 1243 | // group. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1244 | nodeIndex = pending.nodePositionOf(keyInfo) + pending.startIndex |
| 1245 | |
| 1246 | // Determine how to move the slot group to the correct position. |
| 1247 | val relativePosition = pending.slotPositionOf(keyInfo) |
| 1248 | val currentRelativePosition = relativePosition - pending.groupIndex |
| 1249 | pending.registerMoveSlot(relativePosition, pending.groupIndex) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1250 | recordReaderMoving(location) |
| 1251 | reader.reposition(location) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1252 | if (currentRelativePosition > 0) { |
| 1253 | // The slot group must be moved, record the move to be performed during apply. |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1254 | recordSlotEditingOperation { _, slots, _ -> |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 1255 | slots.moveGroup(currentRelativePosition) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1256 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1257 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1258 | startReaderGroup(isNode, data) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1259 | } else { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1260 | // The group is new, go into insert mode. All child groups will written to the |
| 1261 | // insertTable until the group is complete which will schedule the groups to be |
| 1262 | // inserted into in the table. |
| 1263 | reader.beginEmpty() |
| 1264 | inserting = true |
Chuck Jazdzewski | e7725b9 | 2019-06-24 08:51:16 -0700 | [diff] [blame] | 1265 | if (collectKeySources) |
| 1266 | recordSourceKeyInfo(key) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1267 | |
| 1268 | ensureWriter() |
| 1269 | writer.beginInsert() |
| 1270 | val insertLocation = writer.current |
Chuck Jazdzewski | 2ee9227 | 2020-07-07 08:35:12 -0700 | [diff] [blame] | 1271 | if (isNode) writer.startNode(null) else writer.startGroup(key, dataKey, data) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1272 | insertAnchor = writer.anchor(insertLocation) |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1273 | val insertKeyInfo = KeyInfo(key, -1, 0, -1, 0, writer.parentGroup) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1274 | pending.registerInsert(insertKeyInfo, nodeIndex - pending.startIndex) |
| 1275 | pending.recordUsed(insertKeyInfo) |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1276 | newPending = Pending( |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1277 | mutableListOf(), |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1278 | if (isNode) 0 else nodeIndex |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1279 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1283 | enterGroup(isNode, newPending) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1286 | private fun enterGroup(isNode: Boolean, newPending: Pending?) { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1287 | // When entering a group all the information about the parent should be saved, to be |
| 1288 | // restored when end() is called, and all the tracking counters set to initial state for the |
| 1289 | // group. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1290 | pendingStack.push(pending) |
| 1291 | this.pending = newPending |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1292 | this.nodeIndexStack.push(nodeIndex) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1293 | if (isNode) nodeIndex = 0 |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1294 | this.groupNodeCountStack.push(groupNodeCount) |
| 1295 | groupNodeCount = 0 |
| 1296 | } |
| 1297 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1298 | private fun exitGroup(expectedNodeCount: Int, inserting: Boolean) { |
| 1299 | // Restore the parent's state updating them if they have changed based on changes in the |
| 1300 | // children. For example, if a group generates nodes then the number of generated nodes will |
| 1301 | // increment the node index and the group's node count. If the parent is tracking structural |
| 1302 | // changes in pending then restore that too. |
| 1303 | val previousPending = pendingStack.pop() |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1304 | if (previousPending != null && !inserting) { |
| 1305 | previousPending.groupIndex++ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1306 | } |
| 1307 | this.pending = previousPending |
| 1308 | this.nodeIndex = nodeIndexStack.pop() + expectedNodeCount |
| 1309 | this.groupNodeCount = this.groupNodeCountStack.pop() + expectedNodeCount |
| 1310 | } |
| 1311 | |
| 1312 | private fun end(isNode: Boolean) { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1313 | // All the changes to the group (or node) have been recorded. All new nodes have been |
| 1314 | // inserted but it has yet to determine which need to be removed or moved. Note that the |
| 1315 | // changes are relative to the first change in the list of nodes that are changing. |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1316 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1317 | val group = if (inserting) |
| 1318 | writer.group(writer.parentLocation) |
| 1319 | else |
| 1320 | reader.group(reader.parentLocation) |
| 1321 | updateCompoundKeyWhenWeExitGroup(group.key, group.dataKey) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1322 | var expectedNodeCount = groupNodeCount |
| 1323 | val pending = pending |
| 1324 | if (pending != null && pending.keyInfos.size > 0) { |
| 1325 | // previous contains the list of keys as they were generated in the previous composition |
| 1326 | val previous = pending.keyInfos |
| 1327 | |
| 1328 | // current contains the list of keys in the order they need to be in the new composition |
| 1329 | val current = pending.used |
| 1330 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1331 | // usedKeys contains the keys that were used in the new composition, therefore if a key |
| 1332 | // doesn't exist in this set, it needs to be removed. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1333 | val usedKeys = current.toSet() |
| 1334 | |
Chuck Jazdzewski | 8898251 | 2020-04-01 14:07:06 -0700 | [diff] [blame] | 1335 | val placedKeys = mutableSetOf<KeyInfo>() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1336 | var currentIndex = 0 |
| 1337 | val currentEnd = current.size |
| 1338 | var previousIndex = 0 |
| 1339 | val previousEnd = previous.size |
| 1340 | |
| 1341 | // Traverse the list of changes to determine startNode movement |
| 1342 | var nodeOffset = 0 |
| 1343 | while (previousIndex < previousEnd) { |
| 1344 | val previousInfo = previous[previousIndex] |
| 1345 | if (!usedKeys.contains(previousInfo)) { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1346 | // If the key info was not used the group was deleted, remove the nodes in the |
| 1347 | // group |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1348 | val deleteOffset = pending.nodePositionOf(previousInfo) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1349 | recordRemoveNode(deleteOffset + pending.startIndex, previousInfo.nodes) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1350 | pending.updateNodeCount(previousInfo.group, 0) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1351 | recordReaderMoving(previousInfo.location) |
| 1352 | reader.reposition(previousInfo.location) |
| 1353 | recordDelete() |
| 1354 | reader.skipGroup() |
Chuck Jazdzewski | 8a990d1 | 2019-03-05 11:34:51 -0800 | [diff] [blame] | 1355 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1356 | // Remove any invalidations pending for the group being removed. These are no |
| 1357 | // longer part of the composition. The group being composed is one after the |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 1358 | // start of the group. |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 1359 | invalidations.removeRange( |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 1360 | previousInfo.location, |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1361 | previousInfo.location + reader.groupSize(previousInfo.location) |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 1362 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1363 | previousIndex++ |
| 1364 | continue |
| 1365 | } |
| 1366 | |
Chuck Jazdzewski | 8898251 | 2020-04-01 14:07:06 -0700 | [diff] [blame] | 1367 | if (previousInfo in placedKeys) { |
| 1368 | // If the group was already placed in the correct location, skip it. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1369 | previousIndex++ |
| 1370 | continue |
| 1371 | } |
| 1372 | |
| 1373 | if (currentIndex < currentEnd) { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1374 | // At this point current should match previous unless the group is new or was |
| 1375 | // moved. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1376 | val currentInfo = current[currentIndex] |
| 1377 | if (currentInfo !== previousInfo) { |
| 1378 | val nodePosition = pending.nodePositionOf(currentInfo) |
Chuck Jazdzewski | 8898251 | 2020-04-01 14:07:06 -0700 | [diff] [blame] | 1379 | placedKeys.add(currentInfo) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1380 | if (nodePosition != nodeOffset) { |
| 1381 | val updatedCount = pending.updatedNodeCountOf(currentInfo) |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 1382 | recordMoveNode( |
| 1383 | nodePosition + pending.startIndex, |
| 1384 | nodeOffset + pending.startIndex, updatedCount |
| 1385 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1386 | pending.registerMoveNode(nodePosition, nodeOffset, updatedCount) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1387 | } // else the nodes are already in the correct position |
| 1388 | } else { |
| 1389 | // The correct nodes are in the right location |
| 1390 | previousIndex++ |
| 1391 | } |
| 1392 | currentIndex++ |
| 1393 | nodeOffset += pending.updatedNodeCountOf(currentInfo) |
| 1394 | } |
| 1395 | } |
| 1396 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1397 | // If there are any current nodes left they where inserted into the right location |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1398 | // when the group began so the rest are ignored. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1399 | realizeMovement() |
| 1400 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1401 | // We have now processed the entire list so move the slot table to the end of the list |
| 1402 | // by moving to the last key and skipping it. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1403 | if (previous.size > 0) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1404 | recordReaderMoving(reader.groupEnd) |
| 1405 | reader.skipToGroupEnd() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1409 | // Detect removing nodes at the end. No pending is created in this case we just have more |
| 1410 | // nodes in the previous composition than we expect (i.e. we are not yet at an end) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1411 | val removeIndex = nodeIndex |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1412 | while (!reader.isGroupEnd) { |
| 1413 | val startSlot = reader.current |
| 1414 | recordDelete() |
| 1415 | val nodesToRemove = reader.skipGroup() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1416 | recordRemoveNode(removeIndex, nodesToRemove) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1417 | invalidations.removeRange(startSlot, reader.current) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1420 | val inserting = inserting |
Chuck Jazdzewski | 819e865 | 2019-07-29 09:40:43 -0700 | [diff] [blame] | 1421 | if (inserting) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1422 | if (isNode) { |
| 1423 | recordInsertUp() |
| 1424 | expectedNodeCount = 1 |
| 1425 | } |
| 1426 | reader.endEmpty() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1427 | val parentGroup = writer.parentGroup |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1428 | writer.endGroup() |
| 1429 | if (!reader.inEmpty) { |
| 1430 | writer.endInsert() |
| 1431 | writer.close() |
| 1432 | recordInsert(insertAnchor) |
| 1433 | this.inserting = false |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1434 | nodeCountOverrides[parentGroup] = 0 |
| 1435 | updateNodeCountOverrides(parentGroup, expectedNodeCount) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1436 | } |
| 1437 | } else { |
| 1438 | if (isNode) recordUp() |
| 1439 | recordEndGroup() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1440 | val parentGroup = reader.parentGroup |
| 1441 | if (expectedNodeCount != parentGroup.nodes) { |
| 1442 | updateNodeCountOverrides(parentGroup, expectedNodeCount) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1443 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1444 | if (isNode) { |
| 1445 | expectedNodeCount = 1 |
| 1446 | reader.endNode() |
| 1447 | } else reader.endGroup() |
| 1448 | |
| 1449 | realizeMovement() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1452 | exitGroup(expectedNodeCount, inserting) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /** |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1456 | * Recompose any invalidate child groups of the current parent group. This should be called |
| 1457 | * after the group is started but on or before the first child group. It is intended to be |
| 1458 | * called instead of [skipReaderToGroupEnd] if any child groups are invalid. If no children |
| 1459 | * are invalid it will call [skipReaderToGroupEnd]. |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1460 | */ |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1461 | private fun recomposeToGroupEnd() { |
Leland Richardson | 0b5bf70 | 2019-05-24 17:07:21 -0700 | [diff] [blame] | 1462 | val wasComposing = isComposing |
| 1463 | isComposing = true |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1464 | var recomposed = false |
| 1465 | |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1466 | val parent = reader.parentLocation |
| 1467 | val end = parent + reader.groupSize(parent) |
| 1468 | val recomposeGroup = reader.group(parent) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1469 | val recomposeIndex = nodeIndex |
| 1470 | val recomposeCompoundKey = currentCompoundKeyHash |
| 1471 | val oldGroupNodeCount = groupNodeCount |
| 1472 | var oldGroup = recomposeGroup |
| 1473 | |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1474 | var firstInRange = invalidations.firstInRange(reader.current, end) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1475 | while (firstInRange != null) { |
| 1476 | val location = firstInRange.location |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1477 | |
| 1478 | invalidations.removeLocation(location) |
| 1479 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1480 | recomposed = true |
| 1481 | |
| 1482 | reader.reposition(location) |
| 1483 | val newGroup = reader.group |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1484 | // Record the changes to the applier location |
| 1485 | recordUpsAndDowns(oldGroup, newGroup, recomposeGroup) |
| 1486 | oldGroup = newGroup |
| 1487 | |
| 1488 | // Calculate the node index (the distance index in the node this groups nodes are |
| 1489 | // located in the parent node). |
| 1490 | nodeIndex = nodeIndexOf( |
| 1491 | location, |
| 1492 | newGroup, |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1493 | parent, |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1494 | recomposeGroup, |
| 1495 | recomposeIndex |
| 1496 | ) |
| 1497 | |
| 1498 | // Calculate the compound hash code (a semi-unique code for every group in the |
| 1499 | // composition used to restore saved state). |
| 1500 | currentCompoundKeyHash = compoundKeyOf( |
| 1501 | newGroup.parent, |
| 1502 | recomposeGroup, |
| 1503 | recomposeCompoundKey |
| 1504 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1505 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1506 | firstInRange.scope.compose(this) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1507 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 1508 | // Using slots.current here ensures composition always walks forward even if a component |
| 1509 | // before the current composition is invalidated when performing this composition. Any |
| 1510 | // such components will be considered invalid for the next composition. Skipping them |
| 1511 | // prevents potential infinite recomposes at the cost of potentially missing a compose |
| 1512 | // as well as simplifies the apply as it always modifies the slot table in a forward |
| 1513 | // direction. |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1514 | firstInRange = invalidations.firstInRange(reader.current, end) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | if (recomposed) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1518 | recordUpsAndDowns(oldGroup, recomposeGroup, recomposeGroup) |
| 1519 | val parentGroup = reader.parentGroup |
| 1520 | reader.skipToGroupEnd() |
| 1521 | val parentGroupNodes = (nodeCountOverrides[parentGroup] ?: parentGroup.nodes) |
| 1522 | nodeIndex = recomposeIndex + parentGroupNodes |
| 1523 | groupNodeCount = oldGroupNodeCount + parentGroupNodes |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1524 | } else { |
| 1525 | // No recompositions were requested in the range, skip it. |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1526 | skipReaderToGroupEnd() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1527 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1528 | currentCompoundKeyHash = recomposeCompoundKey |
| 1529 | |
Leland Richardson | 0b5bf70 | 2019-05-24 17:07:21 -0700 | [diff] [blame] | 1530 | isComposing = wasComposing |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1533 | /** |
| 1534 | * As operations to insert and remove nodes are recorded, the number of nodes that will be in |
| 1535 | * the group after changes are applied is maintained in a side overrides table. This method |
| 1536 | * updates that count and then updates any parent groups that include the nodes this group |
| 1537 | * emits. |
| 1538 | */ |
| 1539 | private fun updateNodeCountOverrides(group: Group, newCount: Int) { |
| 1540 | val currentCount = nodeCountOverrides[group] ?: group.nodes |
| 1541 | if (currentCount != newCount) { |
| 1542 | // Update the overrides |
| 1543 | val delta = newCount - currentCount |
| 1544 | var current: Group? = group |
| 1545 | |
| 1546 | var minPending = pendingStack.size - 1 |
| 1547 | while (current != null) { |
| 1548 | val newCurrentNodes = (nodeCountOverrides[current] ?: current.nodes) + delta |
| 1549 | nodeCountOverrides[current] = newCurrentNodes |
| 1550 | for (pendingIndex in minPending downTo 0) { |
| 1551 | val pending = pendingStack.peek(pendingIndex) |
| 1552 | if (pending != null && pending.updateNodeCount(current, newCurrentNodes)) { |
| 1553 | minPending = pendingIndex - 1 |
| 1554 | break |
| 1555 | } |
| 1556 | } |
| 1557 | if (current.isNode) break |
| 1558 | current = current.parent |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Calculates the node index (the index in the child list of a node will appear in the |
| 1565 | * resulting tree) for [group]. Passing in [recomposeGroup] and its node index in |
| 1566 | * [recomposeIndex] allows the calculation to exit early if there is no node group between |
| 1567 | * [group] and [recomposeGroup]. |
| 1568 | */ |
| 1569 | private fun nodeIndexOf( |
| 1570 | groupLocation: Int, |
| 1571 | group: Group, |
| 1572 | recomposeLocation: Int, |
| 1573 | recomposeGroup: Group, |
| 1574 | recomposeIndex: Int |
| 1575 | ): Int { |
| 1576 | // Find the anchor group which is either the recomposeGroup or the first parent node |
| 1577 | var anchorGroup = group.parent ?: error("Invalid group") |
| 1578 | while (anchorGroup != recomposeGroup) { |
| 1579 | if (anchorGroup.isNode) break |
| 1580 | anchorGroup = anchorGroup.parent ?: error("group not contained in recompose group") |
| 1581 | } |
| 1582 | |
| 1583 | var index = if (anchorGroup.isNode) 0 else recomposeIndex |
| 1584 | |
| 1585 | // An early out if the group and anchor sizes are the same as the index must then be index |
| 1586 | if (anchorGroup.slots == group.slots) return index |
| 1587 | |
| 1588 | // Find the location of the anchor group |
| 1589 | val anchorLocation = |
| 1590 | if (anchorGroup == recomposeGroup) { |
| 1591 | recomposeLocation |
| 1592 | } else { |
| 1593 | // anchor node must be between recomposeLocation and groupLocation but no farther |
| 1594 | // back than anchorGroup.size - group.size + 1 because anchorGroup contains group |
| 1595 | var location = recomposeLocation |
| 1596 | val anchorLimit = groupLocation - (anchorGroup.slots - group.slots + 1) |
| 1597 | if (location < anchorLimit) location = anchorLimit |
| 1598 | while (reader.get(location) !== anchorGroup) |
| 1599 | location++ |
| 1600 | location |
| 1601 | } |
| 1602 | |
| 1603 | // Walk down from the anchor group counting nodes of siblings in front of this group |
| 1604 | var current = anchorLocation |
| 1605 | val nodeIndexLimit = index + ((nodeCountOverrides[anchorGroup] ?: anchorGroup.nodes) - |
| 1606 | group.nodes) |
| 1607 | loop@ while (index < nodeIndexLimit) { |
| 1608 | if (current == groupLocation) break |
| 1609 | current++ |
| 1610 | while (!reader.isGroup(current)) current++ |
| 1611 | while (current < groupLocation) { |
| 1612 | val currentGroup = reader.group(current) |
| 1613 | val end = currentGroup.slots + current + 1 |
| 1614 | if (groupLocation < end) continue@loop |
| 1615 | index += nodeCountOverrides[currentGroup] ?: currentGroup.nodes |
| 1616 | |
| 1617 | current = end |
| 1618 | } |
| 1619 | break |
| 1620 | } |
| 1621 | return index |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * Records the operations necessary to move the applier the node affected by the previous |
| 1626 | * group to the new group. |
| 1627 | */ |
| 1628 | private fun recordUpsAndDowns(oldGroup: Group, newGroup: Group, commonRoot: Group) { |
| 1629 | val nearestCommonRoot = nearestCommonRootOf( |
| 1630 | oldGroup, |
| 1631 | newGroup, |
| 1632 | commonRoot |
| 1633 | ) ?: commonRoot |
| 1634 | |
| 1635 | // Record ups for the nodes between oldGroup and nearestCommonRoot |
| 1636 | var current: Group? = oldGroup |
| 1637 | while (current != null && current != nearestCommonRoot) { |
| 1638 | if (current.isNode) recordUp() |
| 1639 | current = current.parent |
| 1640 | } |
| 1641 | |
| 1642 | // Record downs from nearestCommonRoot to newGroup |
| 1643 | doRecordDownsFor(newGroup, nearestCommonRoot) |
| 1644 | } |
| 1645 | |
| 1646 | private fun doRecordDownsFor(group: Group?, nearestCommonRoot: Group?) { |
| 1647 | if (group != null && group != nearestCommonRoot) { |
| 1648 | doRecordDownsFor(group.parent, nearestCommonRoot) |
| 1649 | @Suppress("UNCHECKED_CAST") |
| 1650 | if (group.isNode) recordDown(group.node as N) |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * Calculate the compound key (a semi-unique key produced for every group in the composition) |
| 1656 | * for [group]. Passing in the [recomposeGroup] and [recomposeKey] allows this method to exit |
| 1657 | * early. |
| 1658 | */ |
| 1659 | private fun compoundKeyOf(group: Group?, recomposeGroup: Group, recomposeKey: Int): Int { |
| 1660 | return if (group == recomposeGroup) recomposeKey else (compoundKeyOf( |
| 1661 | (group ?: error("Detached group")).parent, |
| 1662 | recomposeGroup, |
| 1663 | recomposeKey |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1664 | ) rol 3) xor (if (group.dataKey != null) group.dataKey.hashCode() else group.key) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1667 | internal fun invalidate(scope: RecomposeScope): InvalidationResult { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1668 | if (scope.defaultsInScope) { |
| 1669 | scope.defaultsInvalid = true |
| 1670 | } |
Chuck Jazdzewski | b15857c | 2020-05-26 17:14:02 -0700 | [diff] [blame] | 1671 | val anchor = scope.anchor |
| 1672 | if (anchor == null || insertTable.ownsAnchor(anchor)) |
| 1673 | return InvalidationResult.IGNORED // The scope has not yet entered the composition |
| 1674 | val location = anchor.location(slotTable) |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1675 | if (location < 0) |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1676 | return InvalidationResult.IGNORED // The scope was removed from the composition |
Chuck Jazdzewski | d9fc2ca | 2019-07-11 11:22:47 -0700 | [diff] [blame] | 1677 | |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 1678 | invalidations.insertIfMissing(location, scope) |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1679 | if (isComposing && location >= reader.current) { |
Leland Richardson | 0b5bf70 | 2019-05-24 17:07:21 -0700 | [diff] [blame] | 1680 | // if we are invalidating a scope that is going to be traversed during this |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1681 | // composition. |
| 1682 | return InvalidationResult.IMMINENT |
Leland Richardson | 0b5bf70 | 2019-05-24 17:07:21 -0700 | [diff] [blame] | 1683 | } |
| 1684 | if (parentReference != null) { |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 1685 | parentReference?.invalidate() |
Chuck Jazdzewski | 73a046b | 2019-02-04 14:22:16 -0800 | [diff] [blame] | 1686 | } else { |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 1687 | recomposer.scheduleRecompose(this) |
Chuck Jazdzewski | 73a046b | 2019-02-04 14:22:16 -0800 | [diff] [blame] | 1688 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1689 | return if (isComposing) InvalidationResult.DEFERRED else InvalidationResult.SCHEDULED |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1692 | /** |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1693 | * Skip a group. Skips the group at the current location. This is only valid to call if the |
| 1694 | * composition is not inserting. |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1695 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1696 | @ComposeCompilerApi |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 1697 | fun skipCurrentGroup() { |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 1698 | if (invalidations.isEmpty()) { |
| 1699 | skipGroup() |
| 1700 | } else { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1701 | val reader = reader |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1702 | val key = reader.groupKey |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1703 | val dataKey = reader.groupDataKey |
| 1704 | updateCompoundKeyWhenWeEnterGroup(key, dataKey) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1705 | startReaderGroup(reader.isNode, reader.groupData) |
| 1706 | recomposeToGroupEnd() |
| 1707 | reader.endGroup() |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1708 | updateCompoundKeyWhenWeExitGroup(key, dataKey) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1712 | private fun skipReaderToGroupEnd() { |
| 1713 | groupNodeCount = reader.parentNodes |
| 1714 | reader.skipToGroupEnd() |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * Skip to the end of the group opened by [startGroup]. |
| 1719 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1720 | @ComposeCompilerApi |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1721 | fun skipToGroupEnd() { |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 1722 | check(groupNodeCount == 0) { "No nodes can be emitted before calling skipAndEndGroup" } |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1723 | if (invalidations.isEmpty()) { |
| 1724 | skipReaderToGroupEnd() |
| 1725 | } else { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1726 | recomposeToGroupEnd() |
Chuck Jazdzewski | d7aa6bd | 2020-04-17 16:33:56 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1730 | /** |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1731 | * Start a restart group. A restart group creates a recompose scope and sets it as the current |
| 1732 | * recompose scope of the composition. If the recompose scope is invalidated then this group |
| 1733 | * will be recomposed. A recompose scope can be invalidated by calling the lambda returned by |
| 1734 | * [androidx.compose.invalidate]. |
| 1735 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1736 | @ComposeCompilerApi |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 1737 | fun startRestartGroup(key: Int) { |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 1738 | start(key, null, false, null) |
Chuck Jazdzewski | 2ee9227 | 2020-07-07 08:35:12 -0700 | [diff] [blame] | 1739 | addRecomposeScope(key) |
| 1740 | } |
| 1741 | |
| 1742 | @ComposeCompilerApi |
| 1743 | fun startRestartGroup(key: Int, sourceInformation: String?) { |
| 1744 | start(key, null, false, sourceInformation) |
| 1745 | addRecomposeScope(key) |
| 1746 | } |
| 1747 | |
| 1748 | private fun addRecomposeScope(key: Int) { |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1749 | if (inserting) { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1750 | val scope = RecomposeScope(this, key) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1751 | invalidateStack.push(scope) |
| 1752 | updateValue(scope) |
| 1753 | } else { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1754 | val invalidation = invalidations.removeLocation(reader.parentLocation) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1755 | val scope = reader.next() as RecomposeScope |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1756 | scope.requiresRecompose = invalidation != null |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1757 | invalidateStack.push(scope) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | /** |
| 1762 | * End a restart group. If the recompose scope was marked used during composition then a |
| 1763 | * [ScopeUpdateScope] is returned that allows attaching a lambda that will produce the same |
| 1764 | * composition as was produced by this group (including calling [startRestartGroup] and |
| 1765 | * [endRestartGroup]). |
| 1766 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1767 | @ComposeCompilerApi |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1768 | fun endRestartGroup(): ScopeUpdateScope? { |
| 1769 | // This allows for the invalidate stack to be out of sync since this might be called during exception stack |
| 1770 | // unwinding that might have not called the doneJoin/endRestartGroup in the wrong order. |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 1771 | val scope = if (invalidateStack.isNotEmpty()) invalidateStack.pop() |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1772 | else null |
Leland Richardson | 898e9e6 | 2020-05-18 12:21:22 -0700 | [diff] [blame] | 1773 | scope?.requiresRecompose = false |
Chuck Jazdzewski | 607eef5 | 2020-05-15 13:36:46 -0700 | [diff] [blame] | 1774 | val result = if (scope != null && (scope.used || collectKeySources)) { |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1775 | if (scope.anchor == null) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1776 | scope.anchor = if (inserting) |
| 1777 | insertTable.anchor(writer.parentLocation) |
| 1778 | else |
| 1779 | slotTable.anchor(reader.parentLocation) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1780 | } |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 1781 | scope.defaultsInvalid = false |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1782 | scope |
| 1783 | } else { |
| 1784 | null |
| 1785 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1786 | end(false) |
Chuck Jazdzewski | 01ac330 | 2019-08-13 10:44:45 -0700 | [diff] [blame] | 1787 | return result |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * Synchronously recompose all invalidated groups. This collects the changes which must be |
| 1792 | * applied by [applyChanges] to have an effect. |
| 1793 | */ |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 1794 | @InternalComposeApi |
Filip Pavlis | 2044790 | 2019-07-01 20:34:25 +0100 | [diff] [blame] | 1795 | fun recompose(): Boolean { |
Chuck Jazdzewski | 006734c | 2018-09-21 09:32:38 -0700 | [diff] [blame] | 1796 | if (invalidations.isNotEmpty()) { |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 1797 | trace("Compose:recompose") { |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1798 | nodeIndex = 0 |
| 1799 | var complete = false |
| 1800 | try { |
| 1801 | startRoot() |
| 1802 | skipCurrentGroup() |
| 1803 | endRoot() |
| 1804 | complete = true |
| 1805 | } finally { |
| 1806 | if (!complete) abortRoot() |
Leland Richardson | bf5f6a6 | 2019-05-24 16:25:46 -0700 | [diff] [blame] | 1807 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 1808 | finalizeCompose() |
Chuck Jazdzewski | 0a9f7fa | 2018-12-20 16:55:46 -0800 | [diff] [blame] | 1809 | } |
Chuck Jazdzewski | 6374c86 | 2020-03-23 14:36:57 -0700 | [diff] [blame] | 1810 | return true |
Chuck Jazdzewski | 006734c | 2018-09-21 09:32:38 -0700 | [diff] [blame] | 1811 | } |
Filip Pavlis | 2044790 | 2019-07-01 20:34:25 +0100 | [diff] [blame] | 1812 | return false |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Chuck Jazdzewski | 8a3c04d | 2020-02-11 11:10:24 -0800 | [diff] [blame] | 1815 | internal fun hasInvalidations() = invalidations.isNotEmpty() |
| 1816 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1817 | @Suppress("UNCHECKED_CAST") |
| 1818 | private var SlotWriter.node |
| 1819 | get() = nodeGroup.node as N |
| 1820 | set(value) { nodeGroup.node = value } |
| 1821 | private val SlotWriter.nodeGroup get() = get(current - 1) as NodeGroup |
| 1822 | private fun SlotWriter.nodeGroupAt(location: Int) = get(location) as NodeGroup |
| 1823 | private fun SlotWriter.nodeAt(location: Int) = nodeGroupAt(location).node |
| 1824 | @Suppress("UNCHECKED_CAST") |
| 1825 | private val SlotReader.node get() = nodeGroupAt(current - 1).node as N |
| 1826 | private fun SlotReader.nodeGroupAt(location: Int) = get(location) as NodeGroup |
| 1827 | @Suppress("UNCHECKED_CAST") |
| 1828 | private fun SlotReader.nodeAt(location: Int) = nodeGroupAt(location).node as N |
| 1829 | |
| 1830 | private fun validateNodeExpected() { |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 1831 | check(nodeExpected) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1832 | "A call to createNode(), emitNode() or useNode() expected was not expected" |
| 1833 | } |
| 1834 | nodeExpected = false |
| 1835 | } |
| 1836 | |
| 1837 | private fun validateNodeNotExpected() { |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 1838 | check(!nodeExpected) { "A call to createNode(), emitNode() or useNode() expected" } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1841 | /** |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1842 | * Add a raw change to the change list. Once [record] is called, the operation is realized |
| 1843 | * into the change list. The helper routines below reduce the number of operations that must |
| 1844 | * be realized to change the previous tree to the new tree as well as update the slot table |
| 1845 | * to prepare for the next composition. |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1846 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1847 | private fun record(change: Change<N>) { |
| 1848 | changes.add(change) |
| 1849 | } |
| 1850 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1851 | /** |
| 1852 | * Record a change ensuring that, when it is applied, the state of the writer reflects the |
| 1853 | * current expected state. This will ensure that the applier is focused on the correct node |
| 1854 | * and the slot table writer slot is the same as the current reader's slot. |
| 1855 | */ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1856 | private fun recordOperation(change: Change<N>) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1857 | realizeInsertUps() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1858 | realizeUps() |
| 1859 | realizeDowns() |
| 1860 | realizeOperationLocation(0) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1861 | record(change) |
| 1862 | } |
| 1863 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1864 | /** |
| 1865 | * Record a change ensuring, when it is applied, that the applier is focused on the current |
| 1866 | * node. |
| 1867 | */ |
| 1868 | private fun recordApplierOperation(change: Change<N>) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1869 | realizeInsertUps() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1870 | realizeUps() |
| 1871 | realizeDowns() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1872 | record(change) |
| 1873 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1874 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1875 | /** |
| 1876 | * Record a change that will insert, remove or move a slot table group. This ensures the slot |
| 1877 | * table is prepared for the change be ensuring the parent group is started and then ended |
| 1878 | * as the group is left. |
| 1879 | */ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1880 | private fun recordSlotEditingOperation(offset: Int = 0, change: Change<N>) { |
| 1881 | realizeOperationLocation(offset) |
| 1882 | recordSlotEditing() |
| 1883 | record(change) |
| 1884 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1885 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1886 | /** |
| 1887 | * Record a change ensuring, when it is applied, the write matches the current slot in the |
| 1888 | * reader. |
| 1889 | */ |
| 1890 | private fun recordSlotTableOperation(offset: Int = 0, change: Change<N>) { |
| 1891 | realizeOperationLocation(offset) |
| 1892 | record(change) |
| 1893 | } |
| 1894 | |
| 1895 | // Navigation of the node tree is performed by recording all the locations of the nodes as |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1896 | // they are traversed by the reader and recording them in the downNodes array. When the node |
| 1897 | // navigation is realized all the downs in the down nodes is played to the applier. |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1898 | // |
| 1899 | // If an up is recorded before the corresponding down is realized then it is simply removed |
| 1900 | // from the downNodes stack. |
| 1901 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1902 | private var pendingUps = 0 |
| 1903 | private var downNodes = Stack<N>() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1904 | |
| 1905 | private fun realizeUps() { |
| 1906 | val count = pendingUps |
| 1907 | if (count > 0) { |
| 1908 | pendingUps = 0 |
| 1909 | record { applier, _, _ -> repeat(count) { applier.up() } } |
| 1910 | } |
| 1911 | } |
| 1912 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1913 | private fun realizeDowns(nodes: Array<N>) { |
| 1914 | record { applier, _, _ -> |
| 1915 | for (index in nodes.indices) { |
| 1916 | applier.down(nodes[index]) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1917 | } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1921 | private fun realizeDowns() { |
| 1922 | if (downNodes.isNotEmpty()) { |
| 1923 | @Suppress("UNCHECKED_CAST") |
| 1924 | realizeDowns(downNodes.toArray()) |
| 1925 | downNodes.clear() |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | private fun recordDown(node: N) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1930 | @Suppress("UNCHECKED_CAST") |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1931 | downNodes.push(node) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | private fun recordUp() { |
| 1935 | if (downNodes.isNotEmpty()) { |
| 1936 | downNodes.pop() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1937 | } else { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1938 | pendingUps++ |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1942 | private var pendingInsertUps = 0 |
| 1943 | |
| 1944 | private fun recordInsertUp() { |
| 1945 | pendingInsertUps++ |
| 1946 | } |
| 1947 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1948 | private fun realizeInsertUps() { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1949 | if (pendingInsertUps > 0) { |
| 1950 | val count = pendingInsertUps |
| 1951 | record { applier, _, _ -> repeat(count) { applier.up() } } |
| 1952 | pendingInsertUps = 0 |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | // Navigating the writer slot is performed relatively as the location of a group in the writer |
| 1957 | // might be different than it is in the reader as groups can be inserted, deleted, or moved. |
| 1958 | // |
| 1959 | // writersReaderDelta tracks the difference between reader's current slot the current of |
| 1960 | // the writer must be before the recorded change is applied. Moving the writer to a location |
| 1961 | // is performed by advancing the writer the same the number of slots traversed by the reader |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1962 | // since the last write change. This works transparently for inserts. For deletes the number |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1963 | // of nodes deleted needs to be added to writersReaderDelta. When slots move the delta is |
| 1964 | // updated as if the move has already taken place. The delta is updated again once the group |
| 1965 | // begin edited is complete. |
| 1966 | // |
| 1967 | // The SlotTable requires that the group that contains any moves, inserts or removes must have |
| 1968 | // the group that contains the moved, inserted or removed groups be started with a startGroup |
| 1969 | // and terminated with a endGroup so the effects of the inserts, deletes, and moves can be |
| 1970 | // recorded correctly in its internal data structures. The startedGroups stack maintains the |
| 1971 | // groups that must be closed before we can move past the started group. |
| 1972 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1973 | /** |
| 1974 | * The skew or delta between where the writer will be and where the reader is now. This can |
| 1975 | * be thought of as the unrealized distance the writer must move to match the current slot in |
| 1976 | * the reader. When an operation affects the slot table the writer location must be realized |
| 1977 | * by moving the writer slot table the unrealized distance. |
| 1978 | */ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1979 | private var writersReaderDelta = 0 |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1980 | |
| 1981 | /** |
| 1982 | * Record whether any groups were stared. If no groups were started then the root group |
| 1983 | * doesn't need to be started or ended either. |
| 1984 | */ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1985 | private var startedGroup = false |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 1986 | |
| 1987 | /** |
| 1988 | * A stack of the location of the groups that were started. |
| 1989 | */ |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 1990 | private val startedGroups = IntStack() |
| 1991 | |
| 1992 | private fun realizeOperationLocation(offset: Int) { |
| 1993 | val location = reader.current + offset |
| 1994 | val distance = location - writersReaderDelta |
| 1995 | require(distance >= 0) { "Tried to seek backward" } |
| 1996 | if (distance > 0) { |
| 1997 | record { _, slots, _ -> slots.skip(distance) } |
| 1998 | writersReaderDelta = location |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | private fun recordInsert(anchor: Anchor) { |
| 2003 | if (insertFixups.isEmpty()) { |
| 2004 | recordSlotEditingOperation { _, slots, _ -> |
| 2005 | slots.beginInsert() |
| 2006 | slots.moveFrom(insertTable, anchor.location(insertTable)) |
| 2007 | slots.endInsert() |
| 2008 | } |
| 2009 | } else { |
| 2010 | val fixups = insertFixups.toMutableList() |
| 2011 | insertFixups.clear() |
| 2012 | recordSlotEditing() |
| 2013 | recordOperation { applier, slots, lifecycleManager -> |
| 2014 | insertTable.write { writer -> |
| 2015 | for (fixup in fixups) { |
| 2016 | fixup(applier, writer, lifecycleManager) |
| 2017 | } |
| 2018 | } |
| 2019 | slots.beginInsert() |
| 2020 | slots.moveFrom(insertTable, anchor.location(insertTable)) |
| 2021 | slots.endInsert() |
| 2022 | } |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | private fun recordFixup(change: Change<N>) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2027 | realizeInsertUps() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2028 | val anchor = insertAnchor |
| 2029 | val start = insertTable.anchorLocation(anchor) |
| 2030 | val location = writer.current - start |
| 2031 | insertFixups.add { _, slots, _ -> |
| 2032 | slots.current = location + insertTable.anchorLocation(anchor) |
| 2033 | } |
| 2034 | insertFixups.add(change) |
| 2035 | } |
| 2036 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2037 | /** |
| 2038 | * When a group is removed the reader will move but the writer will not so to ensure both the |
| 2039 | * writer and reader are tracking the same slot we advance the [writersReaderDelta] to |
| 2040 | * account for the removal. |
| 2041 | */ |
| 2042 | private fun recordDelete() { |
| 2043 | recordSlotEditingOperation(change = removeCurrentGroupInstance) |
| 2044 | writersReaderDelta += reader.groupSize + 1 |
| 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * Called when reader current is moved directly, such as when a group moves, to [location]. |
| 2049 | */ |
| 2050 | private fun recordReaderMoving(location: Int) { |
| 2051 | val distance = reader.current - writersReaderDelta |
| 2052 | |
| 2053 | // Ensure the next skip will account for the distance we have already travelled. |
| 2054 | writersReaderDelta = location - distance |
| 2055 | } |
| 2056 | |
| 2057 | private fun recordSlotEditing() { |
| 2058 | val location = reader.parentLocation |
| 2059 | |
| 2060 | if (startedGroups.peekOr(-1) != location) { |
| 2061 | // During initial composition (when the parent and current are both 0), no group needs |
| 2062 | // to be started. |
| 2063 | if (reader.current != 0) { |
| 2064 | val anchor = slotTable.anchor(location) |
| 2065 | startedGroups.push(location) |
| 2066 | startedGroup = true |
| 2067 | recordSlotTableOperation { _, slots, _ -> slots.ensureStarted(anchor) } |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2072 | private fun recordSkipToGroupEnd() { |
| 2073 | recordSlotTableOperation(change = skipToEndGroupInstance) |
| 2074 | writersReaderDelta = reader.current |
| 2075 | } |
| 2076 | |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2077 | private fun recordEndGroup() { |
| 2078 | val location = reader.parentLocation |
| 2079 | val currentStartedGroup = startedGroups.peekOr(-1) |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2080 | check(currentStartedGroup <= location) { "Missed recording an endGroup" } |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2081 | if (startedGroups.peekOr(-1) == location) { |
| 2082 | startedGroups.pop() |
| 2083 | recordSlotTableOperation(change = endGroupInstance) |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | private fun recordEndRoot() { |
| 2088 | if (startedGroup) { |
| 2089 | recordSlotTableOperation(change = endGroupInstance) |
| 2090 | startedGroup = false |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
Chuck Jazdzewski | 776d823 | 2019-08-21 16:24:03 -0700 | [diff] [blame] | 2094 | private fun finalizeCompose() { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2095 | realizeInsertUps() |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2096 | realizeUps() |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2097 | check(pendingStack.isEmpty()) { "Start/end imbalance" } |
| 2098 | check(startedGroups.isEmpty()) { "Missed recording an endGroup()" } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 2099 | cleanUpCompose() |
| 2100 | } |
| 2101 | |
| 2102 | private fun cleanUpCompose() { |
Chuck Jazdzewski | 006734c | 2018-09-21 09:32:38 -0700 | [diff] [blame] | 2103 | pending = null |
| 2104 | nodeIndex = 0 |
| 2105 | groupNodeCount = 0 |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2106 | writersReaderDelta = 0 |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2107 | currentCompoundKeyHash = 0 |
| 2108 | nodeExpected = false |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2109 | startedGroup = false |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2110 | startedGroups.clear() |
| 2111 | nodeCountOverrides.clear() |
Chuck Jazdzewski | 006734c | 2018-09-21 09:32:38 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2114 | private var previousRemove = -1 |
| 2115 | private var previousMoveFrom = -1 |
| 2116 | private var previousMoveTo = -1 |
| 2117 | private var previousCount = 0 |
| 2118 | |
| 2119 | private fun recordRemoveNode(nodeIndex: Int, count: Int) { |
| 2120 | if (count > 0) { |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2121 | check(nodeIndex >= 0) { "Invalid remove index $nodeIndex" } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2122 | if (previousRemove == nodeIndex) previousCount += count |
| 2123 | else { |
| 2124 | realizeMovement() |
| 2125 | previousRemove = nodeIndex |
| 2126 | previousCount = count |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | private fun recordMoveNode(from: Int, to: Int, count: Int) { |
| 2132 | if (count > 0) { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 2133 | if (previousCount > 0 && previousMoveFrom == from - previousCount && |
Matvei Malkov | ac530be | 2019-05-14 19:43:14 +0100 | [diff] [blame] | 2134 | previousMoveTo == to - previousCount |
| 2135 | ) { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2136 | previousCount += count |
| 2137 | } else { |
| 2138 | realizeMovement() |
| 2139 | previousMoveFrom = from |
| 2140 | previousMoveTo = to |
| 2141 | previousCount = count |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | private fun realizeMovement() { |
| 2147 | val count = previousCount |
| 2148 | previousCount = 0 |
| 2149 | if (count > 0) { |
| 2150 | if (previousRemove >= 0) { |
| 2151 | val removeIndex = previousRemove |
| 2152 | previousRemove = -1 |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2153 | recordApplierOperation { applier, _, _ -> applier.remove(removeIndex, count) } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2154 | } else { |
| 2155 | val from = previousMoveFrom |
| 2156 | previousMoveFrom = -1 |
| 2157 | val to = previousMoveTo |
| 2158 | previousMoveTo = -1 |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2159 | recordApplierOperation { applier, _, _ -> applier.move(from, to, count) } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | } |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2163 | |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 2164 | /** |
| 2165 | * A holder that will dispose of its [CompositionReference] when it leaves the composition |
| 2166 | * that will not have its reference made visible to user code. |
| 2167 | */ |
| 2168 | // This warning becomes an error if its advice is followed since Composer needs its type param |
| 2169 | @Suppress("RemoveRedundantQualifierName") |
| 2170 | private class CompositionReferenceHolder<T>( |
| 2171 | val ref: Composer<T>.CompositionReferenceImpl |
| 2172 | ) : CompositionLifecycleObserver { |
| 2173 | override fun onLeave() { |
| 2174 | ref.dispose() |
| 2175 | } |
| 2176 | } |
| 2177 | |
Chuck Jazdzewski | 8e7017f | 2020-06-05 11:21:24 -0700 | [diff] [blame] | 2178 | private inner class CompositionReferenceImpl( |
| 2179 | val scope: RecomposeScope, |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 2180 | override val compoundHashKey: Int, |
| 2181 | override val collectingKeySources: Boolean |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 2182 | ) : CompositionReference() { |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 2183 | var inspectionTables: MutableSet<MutableSet<SlotTable>>? = null |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2184 | val composers = mutableSetOf<Composer<*>>() |
| 2185 | |
Adam Powell | 7a0bfd6 | 2020-07-08 12:26:43 -0700 | [diff] [blame] | 2186 | fun dispose() { |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 2187 | if (composers.isNotEmpty()) { |
| 2188 | inspectionTables?.let { |
| 2189 | for (composer in composers) { |
| 2190 | for (table in it) |
| 2191 | table.remove(composer.slotTable) |
| 2192 | } |
| 2193 | } |
| 2194 | composers.clear() |
| 2195 | } |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2198 | override fun <N> registerComposer(composer: Composer<N>) { |
| 2199 | composers.add(composer) |
| 2200 | } |
| 2201 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2202 | override fun unregisterComposer(composer: Composer<*>) { |
| 2203 | inspectionTables?.forEach { it.remove(composer.slotTable) } |
| 2204 | composers.remove(composer) |
| 2205 | } |
| 2206 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 2207 | override fun invalidate() { |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2208 | // continue invalidating up the spine of AmbientReferences |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 2209 | parentReference?.invalidate() |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2210 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 2211 | invalidate(scope) |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | override fun <T> getAmbient(key: Ambient<T>): T { |
| 2215 | val anchor = scope.anchor |
| 2216 | return if (anchor != null && anchor.valid) { |
| 2217 | parentAmbient(key, anchor.location(slotTable)) |
| 2218 | } else { |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 2219 | // The composition is composing and the ambient has not landed in the slot table |
| 2220 | // yet. This is a synchronous read from a sub-composition so the current ambient |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2221 | parentAmbient(key) |
| 2222 | } |
| 2223 | } |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 2224 | |
| 2225 | override fun getAmbientScope(): AmbientMap { |
| 2226 | return ambientScopeAt(scope.anchor?.location(slotTable) ?: 0) |
| 2227 | } |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 2228 | |
| 2229 | override fun recordInspectionTable(table: MutableSet<SlotTable>) { |
| 2230 | (inspectionTables ?: HashSet<MutableSet<SlotTable>>().also { |
| 2231 | inspectionTables = it |
| 2232 | }).add(table) |
| 2233 | } |
Leland Richardson | c80ff5e | 2019-03-11 09:40:30 -0700 | [diff] [blame] | 2234 | } |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 2235 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2236 | private fun updateCompoundKeyWhenWeEnterGroup(groupKey: Int, dataKey: Any?) { |
| 2237 | if (dataKey == null) |
| 2238 | updateCompoundKeyWhenWeEnterGroupKeyHash(groupKey) |
| 2239 | else |
| 2240 | updateCompoundKeyWhenWeEnterGroupKeyHash(dataKey.hashCode()) |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Chuck Jazdzewski | 0f26b68 | 2020-03-18 11:42:25 -0700 | [diff] [blame] | 2243 | private fun updateCompoundKeyWhenWeEnterGroupKeyHash(keyHash: Int) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2244 | currentCompoundKeyHash = (currentCompoundKeyHash rol 3) xor keyHash |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2247 | private fun updateCompoundKeyWhenWeExitGroup(groupKey: Int, dataKey: Any?) { |
| 2248 | if (dataKey == null) |
| 2249 | updateCompoundKeyWhenWeExitGroupKeyHash(groupKey) |
| 2250 | else |
| 2251 | updateCompoundKeyWhenWeExitGroupKeyHash(dataKey.hashCode()) |
| 2252 | } |
| 2253 | |
| 2254 | private fun updateCompoundKeyWhenWeExitGroupKeyHash(groupKey: Int) { |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2255 | currentCompoundKeyHash = (currentCompoundKeyHash xor groupKey.hashCode()) ror 3 |
Andrey Kulikov | 1e06a87 | 2020-02-28 15:52:00 +0000 | [diff] [blame] | 2256 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2259 | @Suppress("UNCHECKED_CAST") |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 2260 | /*inline */ class Updater<T>(val composer: Composer<*>, val node: T) { |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2261 | inline fun set( |
| 2262 | value: Int, |
| 2263 | /*crossinline*/ |
| 2264 | block: T.(value: Int) -> Unit |
| 2265 | ) = with(composer) { |
| 2266 | if (inserting || nextSlot() != value) { |
| 2267 | updateValue(value) |
| 2268 | node.block(value) |
| 2269 | // val appliedBlock: T.(value: Int) -> Unit = { block(it) } |
| 2270 | // composer.apply(value, appliedBlock) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2271 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | inline fun <reified V> set( |
| 2275 | value: V, |
| 2276 | /*crossinline*/ |
| 2277 | block: T.(value: V) -> Unit |
| 2278 | ) = with(composer) { |
| 2279 | if (inserting || nextSlot() != value) { |
| 2280 | updateValue(value) |
| 2281 | node.block(value) |
| 2282 | // val appliedBlock: T.(value: V) -> Unit = { block(it) } |
| 2283 | // composer.apply(value, appliedBlock) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2284 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | inline fun update( |
| 2288 | value: Int, |
| 2289 | /*crossinline*/ |
| 2290 | block: T.(value: Int) -> Unit |
| 2291 | ) = with(composer) { |
| 2292 | if (inserting || nextSlot() != value) { |
| 2293 | updateValue(value) |
| 2294 | node.block(value) |
| 2295 | // val appliedBlock: T.(value: Int) -> Unit = { block(it) } |
| 2296 | // if (!inserting) composer.apply(value, appliedBlock) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2297 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | inline fun <reified V> update( |
| 2301 | value: V, |
| 2302 | /*crossinline*/ |
| 2303 | block: T.(value: V) -> Unit |
| 2304 | ) = with(composer) { |
| 2305 | if (inserting || nextSlot() != value) { |
| 2306 | updateValue(value) |
| 2307 | node.block(value) |
| 2308 | // val appliedBlock: T.(value: V) -> Unit = { block(it) } |
| 2309 | // if (!inserting) composer.apply(value, appliedBlock) |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2310 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2311 | } |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 2312 | |
| 2313 | inline fun reconcile( |
| 2314 | block: T.() -> Unit |
| 2315 | ) { |
| 2316 | node.block() |
| 2317 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2320 | private fun SlotWriter.removeCurrentGroup(lifecycleManager: LifecycleManager) { |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2321 | // Notify the lifecycle manager of any observers leaving the slot table |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 2322 | // The notification order should ensure that listeners are notified of leaving |
| 2323 | // in opposite order that they are notified of entering. |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2324 | |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 2325 | // To ensure this order, we call `enters` as a pre-order traversal |
| 2326 | // of the group tree, and then call `leaves` in the inverse order. |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2327 | |
| 2328 | var groupEnd = Int.MAX_VALUE |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2329 | var index = 0 |
| 2330 | val groupEndStack = IntStack() |
| 2331 | |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2332 | for (slot in groupSlots()) { |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2333 | when (slot) { |
Chuck Jazdzewski | 60cdc46 | 2020-03-30 09:12:15 -0700 | [diff] [blame] | 2334 | is CompositionLifecycleObserver -> { |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 2335 | lifecycleManager.leaving(slot) |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2336 | } |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2337 | is Group -> { |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2338 | groupEndStack.push(groupEnd) |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2339 | groupEnd = index + slot.slots |
| 2340 | } |
Chuck Jazdzewski | 7d86900 | 2020-06-24 13:31:48 -0700 | [diff] [blame] | 2341 | is RecomposeScope -> { |
| 2342 | slot.composer = null |
| 2343 | } |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | index++ |
| 2347 | |
| 2348 | while (index >= groupEnd) { |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2349 | groupEnd = groupEndStack.pop() |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | if (groupEndStack.isNotEmpty()) error("Invalid slot structure") |
| 2354 | |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2355 | // Remove the item from the slot table and notify the FrameManager if any |
| 2356 | // anchors are orphaned by removing the slots. |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2357 | if (removeGroup()) { |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2358 | FrameManager.scheduleCleanup() |
| 2359 | } |
| 2360 | } |
| 2361 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2362 | // Mutable list |
| 2363 | private fun <K, V> multiMap() = HashMap<K, LinkedHashSet<V>>() |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 2364 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 2365 | private fun <K, V> HashMap<K, LinkedHashSet<V>>.put(key: K, value: V) = getOrPut(key) { |
| 2366 | LinkedHashSet() |
| 2367 | }.add(value) |
| 2368 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2369 | private fun <K, V> HashMap<K, LinkedHashSet<V>>.remove(key: K, value: V) = |
| 2370 | get(key)?.let { |
| 2371 | it.remove(value) |
| 2372 | if (it.isEmpty()) remove(key) |
| 2373 | } |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 2374 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 2375 | private fun <K, V> HashMap<K, LinkedHashSet<V>>.pop(key: K) = get(key)?.firstOrNull()?.also { |
| 2376 | remove(key, it) |
| 2377 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2378 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2379 | private fun getKey(value: Any?, left: Any?, right: Any?): Any? = (value as? JoinedKey)?.let { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 2380 | if (it.left == left && it.right == right) value |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 2381 | else getKey(it.left, left, right) ?: getKey( |
| 2382 | it.right, |
| 2383 | left, |
| 2384 | right |
| 2385 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | // Invalidation helpers |
Chuck Jazdzewski | ece91d3 | 2020-05-11 10:37:03 -0700 | [diff] [blame] | 2389 | private fun MutableList<Invalidation>.findLocation(location: Int): Int { |
| 2390 | var low = 0 |
| 2391 | var high = size - 1 |
| 2392 | |
| 2393 | while (low <= high) { |
| 2394 | val mid = (low + high).ushr(1) // safe from overflows |
| 2395 | val midVal = get(mid) |
| 2396 | val cmp = midVal.location.compareTo(location) |
| 2397 | |
| 2398 | if (cmp < 0) |
| 2399 | low = mid + 1 |
| 2400 | else if (cmp > 0) |
| 2401 | high = mid - 1 |
| 2402 | else |
| 2403 | return mid // key found |
| 2404 | } |
| 2405 | return -(low + 1) // key not found |
| 2406 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2407 | |
Chuck Jazdzewski | 3d43fd8 | 2018-11-27 12:47:48 -0800 | [diff] [blame] | 2408 | private fun MutableList<Invalidation>.insertIfMissing(location: Int, scope: RecomposeScope) { |
| 2409 | val index = findLocation(location) |
| 2410 | if (index < 0) { |
Leland Richardson | 9138cbe | 2019-04-24 14:53:02 -0700 | [diff] [blame] | 2411 | add(-(index + 1), Invalidation(scope, location)) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | private fun MutableList<Invalidation>.firstInRange(start: Int, end: Int): Invalidation? { |
| 2416 | val index = findLocation(start).let { if (it < 0) -(it + 1) else it } |
| 2417 | if (index < size) { |
| 2418 | val firstInvalidation = get(index) |
| 2419 | if (firstInvalidation.location <= end) return firstInvalidation |
| 2420 | } |
| 2421 | return null |
| 2422 | } |
| 2423 | |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 2424 | private fun MutableList<Invalidation>.removeLocation(location: Int): Invalidation? { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 2425 | val index = findLocation(location) |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 2426 | return if (index >= 0) removeAt(index) else null |
Chuck Jazdzewski | b59aa26 | 2019-01-03 16:17:09 -0800 | [diff] [blame] | 2427 | } |
Chuck Jazdzewski | 8a990d1 | 2019-03-05 11:34:51 -0800 | [diff] [blame] | 2428 | |
| 2429 | private fun MutableList<Invalidation>.removeRange(start: Int, end: Int) { |
| 2430 | val index = findLocation(start).let { if (it < 0) -(it + 1) else it } |
| 2431 | while (index < size) { |
| 2432 | val validation = get(index) |
| 2433 | if (validation.location <= end) removeAt(index) |
| 2434 | else break |
| 2435 | } |
| 2436 | } |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 2437 | |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 2438 | private fun Boolean.asInt() = if (this) 1 else 0 |
| 2439 | private fun Int.asBool() = this != 0 |
| 2440 | |
Leland Richardson | 0bb62a8 | 2020-01-06 15:07:37 -0800 | [diff] [blame] | 2441 | @Composable |
Leland Richardson | 985ef59 | 2020-02-14 15:18:10 -0800 | [diff] [blame] | 2442 | val currentComposer: Composer<*> get() { |
Leland Richardson | 0bb62a8 | 2020-01-06 15:07:37 -0800 | [diff] [blame] | 2443 | throw NotImplementedError("Implemented as an intrinsic") |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2446 | // TODO: get rid of the need for this when we merge FrameManager and Recomposer together! |
Leland Richardson | 985ef59 | 2020-02-14 15:18:10 -0800 | [diff] [blame] | 2447 | internal var currentComposerInternal: Composer<*>? = null |
| 2448 | |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 2449 | internal fun invokeComposable(composer: Composer<*>, composable: @Composable () -> Unit) { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 2450 | @Suppress("UNCHECKED_CAST") |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 2451 | val realFn = composable as Function3<Composer<*>, Int, Int, Unit> |
| 2452 | realFn(composer, 0, 1) |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | internal fun <T> invokeComposableForResult( |
| 2456 | composer: Composer<*>, |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 2457 | composable: @Composable () -> T |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 2458 | ): T { |
| 2459 | @Suppress("UNCHECKED_CAST") |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 2460 | val realFn = composable as Function3<Composer<*>, Int, Int, T> |
| 2461 | return realFn(composer, 0, 1) |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 2462 | } |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2463 | |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2464 | private fun Group.distanceFrom(root: Group): Int { |
| 2465 | var count = 0 |
| 2466 | var current: Group? = this |
| 2467 | while (current != null && current != root) { |
| 2468 | current = current.parent |
| 2469 | count++ |
| 2470 | } |
| 2471 | return count |
| 2472 | } |
| 2473 | |
| 2474 | // find the nearest common root |
| 2475 | private fun nearestCommonRootOf(a: Group, b: Group, common: Group): Group? { |
| 2476 | // Early outs, to avoid calling distanceFrom in trivial cases |
| 2477 | if (a == b) return a // A group is the nearest common root of itself |
| 2478 | if (a == common || b == common) return common // If either is common then common is nearest |
| 2479 | if (a.parent == b) return b // if b is a's parent b is the nearest common root |
| 2480 | if (b.parent == a) return a // if a is b's parent a is the nearest common root |
| 2481 | if (a.parent == b.parent) return a.parent // if a an b share a parent it is the nearest common |
| 2482 | |
| 2483 | // Find the nearest using distance from common |
| 2484 | var currentA: Group? = a |
| 2485 | var currentB: Group? = b |
| 2486 | val aDistance = a.distanceFrom(common) |
| 2487 | val bDistance = b.distanceFrom(common) |
| 2488 | repeat(aDistance - bDistance) { currentA = currentA?.parent } |
| 2489 | repeat(bDistance - aDistance) { currentB = currentB?.parent } |
| 2490 | |
| 2491 | // Both ca and cb are now the same distance from a known common root, |
| 2492 | // therefore, the first parent that is the same is the lowest common root. |
| 2493 | while (currentA != currentB) { |
| 2494 | currentA = currentA?.parent |
| 2495 | currentB = currentB?.parent |
| 2496 | } |
| 2497 | |
| 2498 | // ca == cb so it doesn't matter which is returned |
| 2499 | return currentA |
| 2500 | } |
| 2501 | |
| 2502 | private val removeCurrentGroupInstance: Change<*> = { _, slots, lifecycleManager -> |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 2503 | slots.removeCurrentGroup(lifecycleManager) |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2504 | } |
| 2505 | private val skipToEndGroupInstance: Change<*> = { _, slots, _ -> slots.skipToGroupEnd() } |
| 2506 | private val endGroupInstance: Change<*> = { _, slots, _ -> slots.endGroup() } |
| 2507 | |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2508 | private val KeyInfo.joinedKey: Any get() = if (dataKey != null) JoinedKey(key, dataKey) else key |
| 2509 | |
| 2510 | /* |
| 2511 | * Integer keys are arbitrary values in the biload range. The do not need to be unique as if |
| 2512 | * there is a chance they will collide with a compiler generated key they are paired with a |
| 2513 | * OpaqueKey to ensure they are unique. |
| 2514 | */ |
| 2515 | |
| 2516 | // rootKey doesn't need a corresponding OpaqueKey as it never has sibling nodes and will always |
| 2517 | // a unique key. |
| 2518 | private const val rootKey = 100 |
| 2519 | |
| 2520 | // An arbitrary value paired with a boxed Int or a JoinKey data key. |
| 2521 | private const val nodeKey = 125 |
| 2522 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2523 | @PublishedApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2524 | internal const val invocationKey = 200 |
| 2525 | |
| 2526 | @PublishedApi |
| 2527 | internal val invocation = OpaqueKey("provider") |
| 2528 | |
| 2529 | @PublishedApi |
| 2530 | internal const val providerKey = 201 |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2531 | |
| 2532 | @PublishedApi |
Andrey Kulikov | 15f4421 | 2020-03-12 14:25:14 +0000 | [diff] [blame] | 2533 | internal val provider = OpaqueKey("provider") |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2534 | |
| 2535 | @PublishedApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2536 | internal const val ambientMapKey = 202 |
| 2537 | |
| 2538 | @PublishedApi |
Chuck Jazdzewski | c14dc6e | 2020-04-13 16:26:35 -0700 | [diff] [blame] | 2539 | internal val ambientMap = OpaqueKey("ambientMap") |
| 2540 | |
| 2541 | @PublishedApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2542 | internal const val providerValuesKey = 203 |
| 2543 | |
| 2544 | @PublishedApi |
Andrey Kulikov | 15f4421 | 2020-03-12 14:25:14 +0000 | [diff] [blame] | 2545 | internal val providerValues = OpaqueKey("providerValues") |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2546 | |
| 2547 | @PublishedApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2548 | internal const val providerMapsKey = 204 |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2549 | |
| 2550 | @PublishedApi |
Chuck Jazdzewski | bb0fab24 | 2020-05-08 14:11:33 -0700 | [diff] [blame] | 2551 | internal val providerMaps = OpaqueKey("providers") |
| 2552 | |
| 2553 | @PublishedApi |
| 2554 | internal const val referenceKey = 206 |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2555 | |
| 2556 | @PublishedApi |
Andrey Kulikov | 15f4421 | 2020-03-12 14:25:14 +0000 | [diff] [blame] | 2557 | internal val reference = OpaqueKey("reference") |