| 101 | } |
| 102 | |
| 103 | export function printStore( |
| 104 | store: Store, |
| 105 | includeWeight: boolean = false, |
| 106 | state: StateContext | null = null, |
| 107 | includeSuspense: boolean = true, |
| 108 | ): string { |
| 109 | const snapshotLines = []; |
| 110 | |
| 111 | let rootWeight = 0; |
| 112 | |
| 113 | function printSelectedMarker(index: number): string { |
| 114 | if (state === null) { |
| 115 | return ''; |
| 116 | } |
| 117 | return state.inspectedElementIndex === index ? `→` : ' '; |
| 118 | } |
| 119 | |
| 120 | function printErrorsAndWarnings(element: Element): string { |
| 121 | const {errorCount, warningCount} = |
| 122 | store.getErrorAndWarningCountForElementID(element.id); |
| 123 | if (errorCount === 0 && warningCount === 0) { |
| 124 | return ''; |
| 125 | } |
| 126 | return ` ${errorCount > 0 ? '✕' : ''}${warningCount > 0 ? '⚠' : ''}`; |
| 127 | } |
| 128 | |
| 129 | const ownerFlatTree = state !== null ? state.ownerFlatTree : null; |
| 130 | if (ownerFlatTree !== null) { |
| 131 | snapshotLines.push( |
| 132 | '[owners]' + (includeWeight ? ` (${ownerFlatTree.length})` : ''), |
| 133 | ); |
| 134 | ownerFlatTree.forEach((element, index) => { |
| 135 | const printedSelectedMarker = printSelectedMarker(index); |
| 136 | const printedElement = printElement(element, false); |
| 137 | const printedErrorsAndWarnings = printErrorsAndWarnings(element); |
| 138 | snapshotLines.push( |
| 139 | `${printedSelectedMarker}${printedElement}${printedErrorsAndWarnings}`, |
| 140 | ); |
| 141 | }); |
| 142 | } else { |
| 143 | const errorsAndWarnings = store._errorsAndWarnings; |
| 144 | if (errorsAndWarnings.size > 0) { |
| 145 | let errorCount = 0; |
| 146 | let warningCount = 0; |
| 147 | errorsAndWarnings.forEach(entry => { |
| 148 | errorCount += entry.errorCount; |
| 149 | warningCount += entry.warningCount; |
| 150 | }); |
| 151 | |
| 152 | snapshotLines.push(`✕ ${errorCount}, ⚠ ${warningCount}`); |
| 153 | } |
| 154 | |
| 155 | store.roots.forEach(rootID => { |
| 156 | const {weight} = ((store.getElementByID(rootID): any): Element); |
| 157 | const maybeWeightLabel = includeWeight ? ` (${weight})` : ''; |
| 158 | |
| 159 | // Store does not (yet) expose a way to get errors/warnings per root. |
| 160 | snapshotLines.push(`[root]${maybeWeightLabel}`); |