* Discovers work that needs to be done since the last pass. If viewport * has changed, it will try to build new elements, measure changed elements, * and schedule layouts and preloads within a reasonable distance of the * current viewport. Finally, this process also updates inViewport state
()
| 1046 | * @private |
| 1047 | */ |
| 1048 | discoverWork_() { |
| 1049 | // TODO(dvoytenko): vsync separation may be needed for different phases |
| 1050 | |
| 1051 | const now = this.win.Date.now(); |
| 1052 | |
| 1053 | // Ensure all resources layout phase complete; when relayoutAll is requested |
| 1054 | // force re-layout. |
| 1055 | const { |
| 1056 | elementsThatScrolled_: elementsThatScrolled, |
| 1057 | relayoutAll_: relayoutAll, |
| 1058 | relayoutTop_: relayoutTop, |
| 1059 | } = this; |
| 1060 | this.relayoutAll_ = false; |
| 1061 | this.relayoutTop_ = -1; |
| 1062 | |
| 1063 | // Phase 1: Build and relayout as needed. All mutations happen here. |
| 1064 | let relayoutCount = 0; |
| 1065 | let remeasureCount = 0; |
| 1066 | for (let i = 0; i < this.resources_.length; i++) { |
| 1067 | const r = this.resources_[i]; |
| 1068 | if ( |
| 1069 | r.getState() == ResourceState_Enum.NOT_BUILT && |
| 1070 | !r.isBuilding() && |
| 1071 | !r.element.R1() |
| 1072 | ) { |
| 1073 | this.buildOrScheduleBuildForResource_(r, /* checkForDupes */ true); |
| 1074 | } |
| 1075 | |
| 1076 | if ( |
| 1077 | relayoutAll || |
| 1078 | !r.hasBeenMeasured() || |
| 1079 | // NOT_LAID_OUT is the state after build() but before measure(). |
| 1080 | r.getState() == ResourceState_Enum.NOT_LAID_OUT |
| 1081 | ) { |
| 1082 | relayoutCount++; |
| 1083 | } |
| 1084 | if (r.isMeasureRequested()) { |
| 1085 | remeasureCount++; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | // Phase 2: Remeasure if there were any relayouts. Unfortunately, currently |
| 1090 | // there's no way to optimize this. All reads happen here. |
| 1091 | let toUnload; |
| 1092 | if ( |
| 1093 | relayoutCount > 0 || |
| 1094 | remeasureCount > 0 || |
| 1095 | relayoutAll || |
| 1096 | relayoutTop != -1 || |
| 1097 | elementsThatScrolled.length > 0 |
| 1098 | ) { |
| 1099 | for (let i = 0; i < this.resources_.length; i++) { |
| 1100 | const r = this.resources_[i]; |
| 1101 | if ((r.hasOwner() && !r.isMeasureRequested()) || r.element.R1()) { |
| 1102 | // If element has owner, and measure is not requested, do nothing. |
| 1103 | continue; |
| 1104 | } |
| 1105 | let needsMeasure = |
no test coverage detected