(current, action)
| 40 | } |
| 41 | |
| 42 | function buildView(current, action) { |
| 43 | const { payload, prevKey, key } = action; |
| 44 | const currentKeyPosition = positionFor(current, key); |
| 45 | const afterPreviousKeyPosition = positionAfter(current, prevKey); |
| 46 | switch (action.type) { |
| 47 | case 'value': |
| 48 | if (action.payload?.exists()) { |
| 49 | let prevKey = null; |
| 50 | action.payload.forEach(payload => { |
| 51 | const action = { payload, type: 'value', prevKey, key: payload.key }; |
| 52 | prevKey = payload.key; |
| 53 | current = [...current, action]; |
| 54 | return false; |
| 55 | }); |
| 56 | } |
| 57 | return current; |
| 58 | case 'child_added': |
| 59 | if (currentKeyPosition > -1) { |
| 60 | // check that the previouskey is what we expect, else reorder |
| 61 | const previous = current[currentKeyPosition - 1]; |
| 62 | if ((previous?.key || null) !== prevKey) { |
| 63 | current = current.filter(x => x.payload.key !== payload.key); |
| 64 | current.splice(afterPreviousKeyPosition, 0, action); |
| 65 | } |
| 66 | } else if (prevKey == null) { |
| 67 | return [action, ...current]; |
| 68 | } else { |
| 69 | current = current.slice(); |
| 70 | current.splice(afterPreviousKeyPosition, 0, action); |
| 71 | } |
| 72 | return current; |
| 73 | case 'child_removed': |
| 74 | return current.filter(x => x.payload.key !== payload.key); |
| 75 | case 'child_changed': |
| 76 | return current.map(x => x.payload.key === key ? action : x); |
| 77 | case 'child_moved': |
| 78 | if (currentKeyPosition > -1) { |
| 79 | const data = current.splice(currentKeyPosition, 1)[0]; |
| 80 | current = current.slice(); |
| 81 | current.splice(afterPreviousKeyPosition, 0, data); |
| 82 | return current; |
| 83 | } |
| 84 | return current; |
| 85 | // default will also remove null results |
| 86 | default: |
| 87 | return current; |
| 88 | } |
| 89 | } |
nothing calls this directly
no test coverage detected