Skip to content

Commit

Permalink
Added GamepadController.dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches committed Sep 22, 2024
1 parent d5717f2 commit 2d01eb3
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:async';

import 'package:gamepads_platform_interface/api/gamepad_event.dart';
import 'package:gamepads_platform_interface/api/gamepad_state.dart';
import 'package:gamepads_platform_interface/gamepads_platform_interface.dart';

Expand All @@ -14,12 +17,14 @@ class GamepadController {

final state = GamepadState();

StreamSubscription<GamepadEvent>? _subscription;

GamepadController({
required this.id,
required this.name,
required GamepadsPlatformInterface plugin,
}) {
plugin.eventsByGamepad(id).listen(state.update);
_subscription = plugin.eventsByGamepad(id).listen(state.update);
}

factory GamepadController.parse(
Expand All @@ -30,4 +35,9 @@ class GamepadController {
final name = map['name'] as String;
return GamepadController(id: id, name: name, plugin: plugin);
}

Future<void> dispose() async {
await _subscription?.cancel();
_subscription = null;
}
}

0 comments on commit 2d01eb3

Please sign in to comment.