mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
59 lines
1.6 KiB
Dart
59 lines
1.6 KiB
Dart
import 'dart:async';
|
|
import 'package:syncrow_web/pages/access_management/booking_system/domain/models/paginated_bookable_spaces.dart';
|
|
import 'package:syncrow_web/pages/access_management/booking_system/domain/services/booking_system_service.dart';
|
|
|
|
class DebouncedBookingSystemService implements BookingSystemService {
|
|
final BookingSystemService _inner;
|
|
final Duration debounceDuration;
|
|
|
|
Timer? _debounceTimer;
|
|
Completer<PaginatedBookableSpaces>? _lastCompleter;
|
|
|
|
// Store last parameters
|
|
int? _lastPage;
|
|
int? _lastSize;
|
|
bool? _lastIncludeSpaces;
|
|
String? _lastSearch;
|
|
|
|
DebouncedBookingSystemService(
|
|
this._inner, {
|
|
this.debounceDuration = const Duration(milliseconds: 500),
|
|
});
|
|
|
|
@override
|
|
Future<PaginatedBookableSpaces> getBookableSpaces({
|
|
required int page,
|
|
required int size,
|
|
required String search,
|
|
}) {
|
|
_debounceTimer?.cancel();
|
|
_lastCompleter?.completeError(StateError("Cancelled by new search"));
|
|
|
|
final completer = Completer<PaginatedBookableSpaces>();
|
|
_lastCompleter = completer;
|
|
|
|
_lastPage = page;
|
|
_lastSize = size;
|
|
_lastSearch = search;
|
|
|
|
_debounceTimer = Timer(debounceDuration, () async {
|
|
try {
|
|
final result = await _inner.getBookableSpaces(
|
|
page: _lastPage!,
|
|
size: _lastSize!,
|
|
search: _lastSearch!,
|
|
);
|
|
if (!completer.isCompleted) {
|
|
completer.complete(result);
|
|
}
|
|
} catch (e, st) {
|
|
if (!completer.isCompleted) {
|
|
completer.completeError(e, st);
|
|
}
|
|
}
|
|
});
|
|
|
|
return completer.future;
|
|
}
|
|
}
|