init
This commit is contained in:
44
lib/models/printer_port_handle.dart
Normal file
44
lib/models/printer_port_handle.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
/// Close callback type for [PrinterPortHandle].
|
||||
typedef ClosePortCallback = Future<bool> Function(int handle);
|
||||
|
||||
/// Represents an open printer port handle.
|
||||
///
|
||||
/// Wraps an integer handle and provides a [close] method to release
|
||||
/// the underlying native port resource. Once closed, the handle
|
||||
/// becomes invalid.
|
||||
class PrinterPortHandle {
|
||||
/// The integer handle returned by the native layer.
|
||||
final int handle;
|
||||
|
||||
/// Callback to close the port. If null, close() is a no-op.
|
||||
final ClosePortCallback? _closeCallback;
|
||||
|
||||
/// Whether the port is still valid (not closed).
|
||||
bool _isValid;
|
||||
|
||||
/// Creates a [PrinterPortHandle] with the given [handle] and optional [closeCallback].
|
||||
PrinterPortHandle({
|
||||
required this.handle,
|
||||
ClosePortCallback? closeCallback,
|
||||
}) : _closeCallback = closeCallback,
|
||||
_isValid = true;
|
||||
|
||||
/// Returns true if the port is still open and not closed.
|
||||
bool get isValid => _isValid;
|
||||
|
||||
/// Closes the port and marks the handle as invalid.
|
||||
///
|
||||
/// Subsequent calls to [close] are no-ops if the port is already closed.
|
||||
Future<void> close() async {
|
||||
if (!_isValid) {
|
||||
return;
|
||||
}
|
||||
_isValid = false;
|
||||
if (_closeCallback != null) {
|
||||
await _closeCallback(handle);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'PrinterPortHandle(handle: $handle, valid: $_isValid)';
|
||||
}
|
||||
Reference in New Issue
Block a user