Skip to content

Commit b53c2cb

Browse files
committed
fix: clear winner and winning animation when updating name list
1 parent da4dbea commit b53c2cb

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/assets/js/Slot.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ interface SlotConfigurations {
99
onSpinStart?: () => void;
1010
/** User configuration for callback function that runs after spinning reel */
1111
onSpinEnd?: () => void;
12+
13+
/** User configuration for callback function that runs after user updates the name list */
14+
onNameListChanged?: () => void;
1215
}
1316

1417
// commitStyles() is not yet supported by TypeScript
@@ -39,21 +42,25 @@ export default class Slot {
3942
/** Callback function that runs after spinning reel */
4043
private onSpinEnd?: NonNullable<SlotConfigurations['onSpinEnd']>;
4144

45+
/** Callback function that runs after spinning reel */
46+
private onNameListChanged?: NonNullable<SlotConfigurations['onNameListChanged']>;
47+
4248
/**
4349
* Constructor of Slot
4450
* @param maxReelItems Maximum item inside a reel
4551
* @param removeWinner Whether winner should be removed from name list
4652
* @param reelContainerSelector The element ID of reel items to be appended
4753
* @param onSpinStart Callback function that runs before spinning reel
48-
* @param onSpinEnd Callback function that runs after spinning reel
54+
* @param onNameListChanged Callback function that runs when user updates the name list
4955
*/
5056
constructor(
5157
{
5258
maxReelItems = 30,
5359
removeWinner = true,
5460
reelContainerSelector,
5561
onSpinStart,
56-
onSpinEnd
62+
onSpinEnd,
63+
onNameListChanged
5764
}: SlotConfigurations
5865
) {
5966
this.nameList = [];
@@ -62,6 +69,7 @@ export default class Slot {
6269
this.shouldRemoveWinner = removeWinner;
6370
this.onSpinStart = onSpinStart;
6471
this.onSpinEnd = onSpinEnd;
72+
this.onNameListChanged = onNameListChanged;
6573

6674
// Create reel animation
6775
this.reelAnimation = this.reelContainer?.animate(
@@ -93,6 +101,17 @@ export default class Slot {
93101
*/
94102
set names(names: string[]) {
95103
this.nameList = names;
104+
105+
const reelItemsToRemove = this.reelContainer?.children
106+
? Array.from(this.reelContainer.children)
107+
: [];
108+
109+
reelItemsToRemove
110+
.forEach((element) => element.remove());
111+
112+
if (this.onNameListChanged) {
113+
this.onNameListChanged();
114+
}
96115
}
97116

98117
/** Getter for name list */

src/assets/js/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ import SoundEffects from '@js/SoundEffects';
6969
confettiAnimationId = window.requestAnimationFrame(confettiAnimation);
7070
};
7171

72-
/** Function to be trigger before spinning */
73-
const onSpinStart = () => {
72+
/** Function to stop the winning animation */
73+
const stopWinningAnimation = () => {
7474
if (confettiAnimationId) {
7575
window.cancelAnimationFrame(confettiAnimationId);
7676
}
7777
sunburstSvg.style.display = 'none';
78+
};
79+
80+
/** Function to be trigger before spinning */
81+
const onSpinStart = () => {
82+
stopWinningAnimation();
7883
drawButton.disabled = true;
7984
settingsButton.disabled = true;
8085
soundEffects.spin((MAX_REEL_ITEMS - 1) / 10);
@@ -94,7 +99,8 @@ import SoundEffects from '@js/SoundEffects';
9499
reelContainerSelector: '#reel',
95100
maxReelItems: MAX_REEL_ITEMS,
96101
onSpinStart,
97-
onSpinEnd
102+
onSpinEnd,
103+
onNameListChanged: stopWinningAnimation
98104
});
99105

100106
/** To open the setting page */

0 commit comments

Comments
 (0)