Skip to content

Commit a51a5d8

Browse files
committed
fix: Add debounce transformer to SignUpBloc form input events to reduce lag.
Signed-off-by: BURG3R5 <77491630+BURG3R5@users.noreply.github.com>
1 parent 432e03f commit a51a5d8

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

lib/presentation/signup/bloc/sign_up_bloc.dart

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import '../../../domain/models/handle.dart';
1414
import '../../../domain/models/sign_up.dart';
1515
import '../../../domain/models/status.dart';
1616
import '../../../domain/repositories/user_repository.dart';
17+
import '../../../utils/bloc_transformer.dart';
1718
import '../../../utils/failures.dart';
1819
import '../widgets/signup_widgets.dart';
1920

@@ -30,12 +31,30 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
3031
on<Back>(_handleBack);
3132
on<Next>(_handleNext);
3233

33-
on<EmailInput>(_updateEmail);
34-
on<InstituteInput>(_updateInstitute);
35-
on<NameInput>(_updateName);
36-
on<PasswordInput>(_updatePassword);
37-
on<PlatformHandleInput>(_updateHandles);
38-
on<UsernameInput>(_updateUsername);
34+
on<EmailInput>(
35+
_updateEmail,
36+
transformer: getDebounce<EmailInput>(),
37+
);
38+
on<InstituteInput>(
39+
_updateInstitute,
40+
transformer: getDebounce<InstituteInput>(),
41+
);
42+
on<NameInput>(
43+
_updateName,
44+
transformer: getDebounce<NameInput>(),
45+
);
46+
on<PasswordInput>(
47+
_updatePassword,
48+
transformer: getDebounce<PasswordInput>(),
49+
);
50+
on<PlatformHandleInput>(
51+
_updateHandles,
52+
transformer: getDebounce<PlatformHandleInput>(),
53+
);
54+
on<UsernameInput>(
55+
_updateUsername,
56+
transformer: getDebounce<UsernameInput>(),
57+
);
3958

4059
on<SelectImage>(_selectImage);
4160
on<ToggleObscure>(_toggleObscurePassword);

lib/utils/bloc_transformer.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:rxdart/rxdart.dart';
2+
3+
/// Provides a custom debounce BLoC transformer that waits till
4+
/// 200ms of inactivity before processing/calling event handlers.
5+
Stream<T> Function(Stream<T>, Stream<T> Function(T)) getDebounce<T>() {
6+
return (Stream<T> events, Stream<T> Function(T) mapper) {
7+
return events
8+
.debounceTime(const Duration(milliseconds: 200))
9+
.switchMap(mapper);
10+
};
11+
}

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ packages:
843843
source: hosted
844844
version: "1.2.0"
845845
rxdart:
846-
dependency: transitive
846+
dependency: "direct main"
847847
description:
848848
name: rxdart
849849
url: "https://pub.dartlang.org"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232
image_picker: ^0.8.4+10
3333
intl: ^0.17.0
3434
json_annotation: ^4.4.0
35+
rxdart: ^0.27.3
3536
sentry_flutter: ^6.2.2
3637

3738
dev_dependencies:

0 commit comments

Comments
 (0)