-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow multiple model processors to increase the FPS (#36)
- Loading branch information
Showing
27 changed files
with
328 additions
and
612 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
This file was deleted.
Oops, something went wrong.
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
81 changes: 81 additions & 0 deletions
81
client/lib/core/common/transformers/transform_while_available_transformer.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,81 @@ | ||
import 'dart:async'; | ||
import 'dart:collection'; | ||
|
||
import 'package:mutex/mutex.dart'; | ||
|
||
typedef Processor<T, R> = FutureOr<R> Function(T); | ||
|
||
class ProcessWhileAvailableTransformer<T, R> | ||
extends StreamTransformerBase<T, R> { | ||
var _isClosed = false; | ||
final List<Processor> _availableProcessors; | ||
final Set<Processor> _processors; | ||
|
||
final _mutex = Mutex(); | ||
final Queue<T> _unprocessedQueue = Queue<T>(); | ||
|
||
ProcessWhileAvailableTransformer(Iterable<Processor> processors) | ||
: _processors = processors.toSet(), | ||
_availableProcessors = processors.toList(); | ||
|
||
Future<void> _processValue( | ||
Processor processor, | ||
T value, | ||
EventSink<R> sink, | ||
) async { | ||
try { | ||
T? currentValue = value; | ||
do { | ||
final event = await processor(currentValue); | ||
sink.add(event); | ||
// ignore: avoid-redundant-async | ||
await _mutex.protect(() async { | ||
currentValue = _unprocessedQueue.isEmpty | ||
? null | ||
: _unprocessedQueue.removeFirst(); | ||
}); | ||
} while (currentValue != null && !_isClosed); | ||
} catch (e) { | ||
if (!_isClosed) { | ||
sink.addError(e); | ||
} | ||
} | ||
} | ||
|
||
void _handleData(T value, EventSink<R> sink) { | ||
// ignore: avoid-redundant-async | ||
_mutex.protect(() async { | ||
if (_availableProcessors.isEmpty) { | ||
_unprocessedQueue.addLast(value); | ||
if (_unprocessedQueue.length > _processors.length) { | ||
// Drop the oldest value when the queue is full | ||
_unprocessedQueue.removeFirst(); | ||
} | ||
} else { | ||
final processor = _availableProcessors.removeAt(0); | ||
unawaited( | ||
_processValue(processor, value, sink).then( | ||
(_) => _mutex.protect( | ||
() async => _availableProcessors.add(processor), | ||
), | ||
), | ||
); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
Stream<R> bind(Stream<T> stream) => stream.transform( | ||
StreamTransformer<T, R>.fromHandlers( | ||
handleData: _handleData, | ||
handleError: | ||
(Object error, StackTrace stackTrace, EventSink<R> sink) { | ||
sink.addError(error, stackTrace); | ||
}, | ||
handleDone: (EventSink<R> sink) { | ||
sink.close(); | ||
_isClosed = true; | ||
}, | ||
), | ||
); | ||
} |
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
Oops, something went wrong.