Skip to content

Commit d241b6f

Browse files
committed
Improvements.
1 parent d50b620 commit d241b6f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.3
2+
3+
- General/genuine packaging improvements.
4+
15
## 0.0.2
26

37
- Flutter packaging requirements

lib/src/unix_single_instance_base.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import 'dart:typed_data';
66
import 'package:path_provider/path_provider.dart';
77
import 'package:path/path.dart' as p;
88

9+
// Simple function to get the appropriate file location to store using path_provider
10+
// this should be replaced, especially if we are going to allow the user to specify
11+
// if it's a single instance per {user, user&x, x, system, etc.}
912
Future<String> _applicationConfigDirectory() async {
1013
final String dbPath;
1114
if (Platform.isAndroid) {
@@ -20,6 +23,11 @@ Future<String> _applicationConfigDirectory() async {
2023
return dbPath;
2124
}
2225

26+
// Call this at the top of your function, returns a bool. Which is "true" if this is the first instance,
27+
// if this is the second instance (and it has transmitted the arguments across the socket) it returns
28+
// false.
29+
// cmdProcessor is what the first instance does once it receives the command line arguments from the previous
30+
// kDebugMode makes the application noisy.
2331
Future<bool> unixSingleInstance(List<String> arguments,
2432
void Function(List<dynamic> args) cmdProcessor, {
2533
bool kDebugMode = false
@@ -37,7 +45,7 @@ Future<bool> unixSingleInstance(List<String> arguments,
3745
if (kDebugMode) {
3846
print("Found existing instance!");
3947
}
40-
var messageSent = await sendArgsToUixSocket(arguments, host, kDebugMode: kDebugMode);
48+
var messageSent = await _sendArgsToUixSocket(arguments, host, kDebugMode: kDebugMode);
4149
if (messageSent) {
4250
if (kDebugMode) {
4351
print("Message sent");
@@ -54,7 +62,7 @@ Future<bool> unixSingleInstance(List<String> arguments,
5462
// TODO manage socket subscription, technically not required because OS clean up does the work "for" us but good practices.
5563
// StreamSubscription<Socket>? socket;
5664
try {
57-
/*socket = */await createUnixSocket(host, cmdProcessor, kDebugMode: kDebugMode);
65+
/*socket = */await _createUnixSocket(host, cmdProcessor, kDebugMode: kDebugMode);
5866
} catch (e) {
5967
print("Socket create error");
6068
print(e);
@@ -64,7 +72,8 @@ Future<bool> unixSingleInstance(List<String> arguments,
6472
return true;
6573
}
6674

67-
Future<bool> sendArgsToUixSocket(
75+
// JSON serializes the args, and sends across "the wire"
76+
Future<bool> _sendArgsToUixSocket(
6877
List<String> args, InternetAddress host, {
6978
bool kDebugMode = false
7079
}) async {
@@ -82,7 +91,10 @@ Future<bool> sendArgsToUixSocket(
8291
}
8392
}
8493

85-
Future<StreamSubscription<Socket>> createUnixSocket(InternetAddress host,
94+
// Creates the unix socket, or cleans up if it exists but isn't valid and then
95+
// recursively calls itself -- if the socket is valid, sends the args as json.
96+
// Return stream subscription.
97+
Future<StreamSubscription<Socket>> _createUnixSocket(InternetAddress host,
8698
void Function(List<dynamic> args) cmdProcessor, {
8799
bool kDebugMode = false
88100
}) async {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: unix_single_instance
22
description: A library which uses unix sockets to ensure a single instance
3-
version: 0.0.2
3+
version: 0.0.3
44
repository: https://github.com/arran4/dart_unix_single_instance
55
homepage: https://github.com/arran4/dart_unix_single_instance
66

0 commit comments

Comments
 (0)