Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement singleton logic #22

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/sane/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ final class SaneUnsupportedException extends SaneException {
@override
SANE_Status get _status => SANE_Status.STATUS_UNSUPPORTED;
}

/// SANE has been exited or the device has been closed.
final class SaneDisposedError extends StateError {
SaneDisposedError() : super('SANE has been exited');
}
44 changes: 44 additions & 0 deletions packages/sane/lib/src/sane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ import 'package:sane/src/utils.dart';
typedef AuthCallback = SaneCredentials Function(String resourceName);

class Sane {
factory Sane() => _instance ??= Sane._();

Sane._();

static Sane? _instance;
bool _exited = false;
Jupi007 marked this conversation as resolved.
Show resolved Hide resolved
final Map<SaneHandle, SANE_Handle> _nativeHandles = {};

SANE_Handle _getNativeHandle(SaneHandle handle) => _nativeHandles[handle]!;

Future<int> init({
AuthCallback? authCallback,
}) {
_checkIfExited();

final completer = Completer<int>();

void authCallbackAdapter(
Expand Down Expand Up @@ -65,13 +74,19 @@ class Sane {
}

Future<void> exit() {
if (_exited) return Future.value();

final completer = Completer<void>();

Future(() {
_exited = true;

dylib.sane_exit();
print('sane_exit()');

completer.complete();

_instance = null;
});

return completer.future;
Expand All @@ -80,6 +95,8 @@ class Sane {
Future<List<SaneDevice>> getDevices({
required bool localOnly,
}) {
_checkIfExited();

final completer = Completer<List<SaneDevice>>();

Future(() {
Expand Down Expand Up @@ -108,6 +125,8 @@ class Sane {
}

Future<SaneHandle> open(String deviceName) {
_checkIfExited();

final completer = Completer<SaneHandle>();

Future(() {
Expand All @@ -133,10 +152,14 @@ class Sane {
}

Future<SaneHandle> openDevice(SaneDevice device) {
_checkIfExited();

return open(device.name);
}

Future<void> close(SaneHandle handle) {
_checkIfExited();

final completer = Completer<void>();

Future(() {
Expand All @@ -154,6 +177,8 @@ class Sane {
SaneHandle handle,
int index,
) {
_checkIfExited();

final completer = Completer<SaneOptionDescriptor>();

Future(() {
Expand All @@ -175,6 +200,8 @@ class Sane {
Future<List<SaneOptionDescriptor>> getAllOptionDescriptors(
SaneHandle handle,
) {
_checkIfExited();

final completer = Completer<List<SaneOptionDescriptor>>();

Future(() {
Expand All @@ -201,6 +228,8 @@ class Sane {
required SaneAction action,
T? value,
}) {
_checkIfExited();

final completer = Completer<SaneOptionResult<T>>();

Future(() {
Expand Down Expand Up @@ -393,6 +422,8 @@ class Sane {
}

Future<SaneParameters> getParameters(SaneHandle handle) {
_checkIfExited();

final completer = Completer<SaneParameters>();

Future(() {
Expand All @@ -416,6 +447,8 @@ class Sane {
}

Future<void> start(SaneHandle handle) {
_checkIfExited();

final completer = Completer<void>();

Future(() {
Expand All @@ -431,6 +464,8 @@ class Sane {
}

Future<Uint8List> read(SaneHandle handle, int bufferSize) {
_checkIfExited();

final completer = Completer<Uint8List>();

Future(() {
Expand Down Expand Up @@ -464,6 +499,8 @@ class Sane {
}

Future<void> cancel(SaneHandle handle) {
_checkIfExited();

final completer = Completer<void>();

Future(() {
Expand All @@ -477,6 +514,8 @@ class Sane {
}

Future<void> setIOMode(SaneHandle handle, SaneIOMode mode) {
_checkIfExited();

final completer = Completer<void>();

Future(() {
Expand All @@ -493,4 +532,9 @@ class Sane {

return completer.future;
}

@pragma('vm:prefer-inline')
void _checkIfExited() {
if (_exited) throw SaneDisposedError();
}
}
36 changes: 36 additions & 0 deletions packages/sane/test/sane_singleton_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:sane/sane.dart';
import 'package:test/test.dart';

void main() {
late Sane sane;

test('can instantiate', () {
sane = Sane();
});

test('same instance on repeated instantiation', () {
final newSane = Sane();
expect(sane, equals(newSane));
});

test('can exit', () {
expect(sane.exit, returnsNormally);
});

test('throws upon use', () {
expect(
() => sane.getDevices(localOnly: true),
throwsA(isA<SaneDisposedError>()),
);
});

test('can reinstiate with new instance', () {
final newSane = Sane();
expect(sane, isNot(newSane));
sane = newSane;
});

test('doesn\'t throw upon use', () {
expect(() => sane.getDevices(localOnly: true), returnsNormally);
});
}