( tree, visitor, context, options = defaultOptions, )
| 85 | // If a visitor call returns `false` then we will not recurse into the respective |
| 86 | // elements children. |
| 87 | export default function reactTreeWalker( |
| 88 | tree, |
| 89 | visitor, |
| 90 | context, |
| 91 | options = defaultOptions, |
| 92 | ) { |
| 93 | return new Promise((resolve, reject) => { |
| 94 | const safeVisitor = (...args) => { |
| 95 | try { |
| 96 | return visitor(...args) |
| 97 | } catch (err) { |
| 98 | reject(err) |
| 99 | } |
| 100 | return undefined |
| 101 | } |
| 102 | |
| 103 | const recursive = (currentElement, currentContext) => { |
| 104 | if (Array.isArray(currentElement)) { |
| 105 | return Promise.all( |
| 106 | currentElement.map(item => recursive(item, currentContext)), |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | if (!currentElement) { |
| 111 | return Promise.resolve() |
| 112 | } |
| 113 | |
| 114 | if ( |
| 115 | typeof currentElement === 'string' || |
| 116 | typeof currentElement === 'number' |
| 117 | ) { |
| 118 | // Just visit these, they are leaves so we don't keep traversing. |
| 119 | safeVisitor(currentElement, null, currentContext) |
| 120 | return Promise.resolve() |
| 121 | } |
| 122 | |
| 123 | if (currentElement.type) { |
| 124 | if (currentElement.type._context) { |
| 125 | // eslint-disable-next-line no-param-reassign |
| 126 | currentElement.type._context._currentValue = |
| 127 | currentElement.props.value |
| 128 | } |
| 129 | if (currentElement.type.Provider && currentElement.type.Consumer) { |
| 130 | const el = currentElement.props.children( |
| 131 | currentElement.type.Provider._context._currentValue, |
| 132 | ) |
| 133 | return recursive(el, currentContext) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (isReactElement(currentElement)) { |
| 138 | return new Promise(innerResolve => { |
| 139 | const visitCurrentElement = ( |
| 140 | render, |
| 141 | compInstance, |
| 142 | elContext, |
| 143 | childContext, |
| 144 | ) => |
no test coverage detected