|
1 | | -import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants" |
2 | | -import { ActiveBar } from "../types" |
| 1 | +import { ActiveBar, AnimateFunctionParams } from "../types" |
3 | 2 | import { changeBarsColor, getAllBars, makeBarsActive, postSortAnimation } from "../utils" |
4 | 3 |
|
5 | 4 | const selectionSort = (inputArray: number[]) => { |
@@ -44,11 +43,8 @@ const selectionSort = (inputArray: number[]) => { |
44 | 43 | return [arrayState, animationSequence] |
45 | 44 | } |
46 | 45 |
|
47 | | -export const animateSelectionSort = ( |
48 | | - barHeights: number[], |
49 | | - sortingSpeed: number, |
50 | | - callback?: () => void |
51 | | -) => { |
| 46 | +export const animateSelectionSort = (params: AnimateFunctionParams) => { |
| 47 | + const { barHeights, palette, sortingSpeed, callback } = params |
52 | 48 | const [arrayStates, animationSequence] = selectionSort(barHeights) |
53 | 49 | const bars = getAllBars() |
54 | 50 |
|
@@ -81,23 +77,23 @@ export const animateSelectionSort = ( |
81 | 77 | let prevAnimationSequence = animationSequence[i - 1] |
82 | 78 | let [prevBar1Idx, prevBar2Idx] = prevAnimationSequence |
83 | 79 | let previousActiveBars = [bars[prevBar1Idx], bars[prevBar2Idx]] |
84 | | - changeBarsColor(previousActiveBars, INACTIVE_BAR_COLOR) |
| 80 | + changeBarsColor(previousActiveBars, palette.inactive) |
85 | 81 | } |
86 | 82 |
|
87 | | - makeBarsActive(barsToActiveBars) |
| 83 | + makeBarsActive(barsToActiveBars, palette.active) |
88 | 84 |
|
89 | 85 | if (lastIteration) { |
90 | 86 | // Revert the last bar to an inactive bar |
91 | 87 | setTimeout(() => { |
92 | 88 | let lastBar = activeBar1 |
93 | | - changeBarsColor(lastBar, INACTIVE_BAR_COLOR) |
| 89 | + changeBarsColor(lastBar, palette.inactive) |
94 | 90 |
|
95 | 91 | // Because this animate function executes asynchronous function (setTimeout) |
96 | 92 | // if we wanted to do something right after this function is done running, |
97 | 93 | // we have to put the code inside the last setTimeout. |
98 | 94 | // Otherwise, the code will get executed without waiting for the setTimeouts |
99 | 95 | // to get executed. |
100 | | - postSortAnimation(bars, ACTIVE_BAR_COLOR) |
| 96 | + postSortAnimation(bars, palette.active) |
101 | 97 | if (callback) callback() |
102 | 98 | }, sortingSpeed) |
103 | 99 | } |
|
0 commit comments