-
-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Invoke
setToStart
on child effect controller of wrapping effec…
…t controllers (#3168) `DelayedEffectController` was not calling `setToStart` on its child, causing it to not behave as expected when used with `InfiniteEffectController`. This was reported by a discord member [in this message](https://discord.com/channels/509714518008528896/516639688581316629/1242258377766080666). This PR introduces a `HasSingleChildEffectController` mixin which can be added to any effect controller that wraps a single child effect controller. This mixin makes sure that the `setToStart`, `setToEnd` and `onMount` methods always get invoked on the child controllers.
- Loading branch information
1 parent
ffba0f9
commit 217c95f
Showing
8 changed files
with
131 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/flame/lib/src/effects/controllers/mixins/has_single_child_effect_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:flame/effects.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// This mixin must be used with [EffectController]s that wrap a single child | ||
/// effect controller of type [T]. | ||
mixin HasSingleChildEffectController<T extends EffectController> | ||
on EffectController { | ||
/// Returns the wrapped child effect controller. | ||
T get child; | ||
|
||
@mustCallSuper | ||
@override | ||
void setToStart() { | ||
child.setToStart(); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void setToEnd() { | ||
child.setToEnd(); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void onMount(Effect parent) { | ||
child.onMount(parent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/flame/test/effects/controllers/mixins/has_single_child_effect_controller_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:flame/effects.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('HasSingleChildEffectController', () { | ||
test('child getter should return the wrapped child effect controller', () { | ||
final childController = _MockEffectController(); | ||
final controller = _TestEffectController(childController); | ||
|
||
expect(controller.child, equals(childController)); | ||
}); | ||
|
||
test('setToStart should call setToStart on the child effect controller', | ||
() { | ||
final childController = _MockEffectController(); | ||
final controller = _TestEffectController(childController); | ||
controller.setToStart(); | ||
verify(childController.setToStart).called(1); | ||
}); | ||
|
||
test('setToEnd should call setToEnd on the child effect controller', () { | ||
final childController = _MockEffectController(); | ||
final controller = _TestEffectController(childController); | ||
controller.setToEnd(); | ||
verify(childController.setToEnd).called(1); | ||
}); | ||
|
||
test('onMount should call onMount on the child effect controller', () { | ||
final childController = _MockEffectController(); | ||
final controller = _TestEffectController(childController); | ||
final parentEffect = _MockEffect(); | ||
controller.onMount(parentEffect); | ||
verify(() => childController.onMount(parentEffect)).called(1); | ||
}); | ||
}); | ||
} | ||
|
||
class _TestEffectController extends _MockEffectController | ||
with HasSingleChildEffectController<_MockEffectController> { | ||
_TestEffectController(_MockEffectController child) : _child = child; | ||
|
||
final _MockEffectController _child; | ||
|
||
@override | ||
_MockEffectController get child => _child; | ||
} | ||
|
||
class _MockEffectController extends Mock implements EffectController {} | ||
|
||
class _MockEffect extends Mock implements Effect {} |