@@ -6,6 +6,9 @@ import 'dart:typed_data';
6
6
import 'package:path_provider/path_provider.dart' ;
7
7
import 'package:path/path.dart' as p;
8
8
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.}
9
12
Future <String > _applicationConfigDirectory () async {
10
13
final String dbPath;
11
14
if (Platform .isAndroid) {
@@ -20,6 +23,11 @@ Future<String> _applicationConfigDirectory() async {
20
23
return dbPath;
21
24
}
22
25
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.
23
31
Future <bool > unixSingleInstance (List <String > arguments,
24
32
void Function (List <dynamic > args) cmdProcessor, {
25
33
bool kDebugMode = false
@@ -37,7 +45,7 @@ Future<bool> unixSingleInstance(List<String> arguments,
37
45
if (kDebugMode) {
38
46
print ("Found existing instance!" );
39
47
}
40
- var messageSent = await sendArgsToUixSocket (arguments, host, kDebugMode: kDebugMode);
48
+ var messageSent = await _sendArgsToUixSocket (arguments, host, kDebugMode: kDebugMode);
41
49
if (messageSent) {
42
50
if (kDebugMode) {
43
51
print ("Message sent" );
@@ -54,7 +62,7 @@ Future<bool> unixSingleInstance(List<String> arguments,
54
62
// TODO manage socket subscription, technically not required because OS clean up does the work "for" us but good practices.
55
63
// StreamSubscription<Socket>? socket;
56
64
try {
57
- /*socket = */ await createUnixSocket (host, cmdProcessor, kDebugMode: kDebugMode);
65
+ /*socket = */ await _createUnixSocket (host, cmdProcessor, kDebugMode: kDebugMode);
58
66
} catch (e) {
59
67
print ("Socket create error" );
60
68
print (e);
@@ -64,7 +72,8 @@ Future<bool> unixSingleInstance(List<String> arguments,
64
72
return true ;
65
73
}
66
74
67
- Future <bool > sendArgsToUixSocket (
75
+ // JSON serializes the args, and sends across "the wire"
76
+ Future <bool > _sendArgsToUixSocket (
68
77
List <String > args, InternetAddress host, {
69
78
bool kDebugMode = false
70
79
}) async {
@@ -82,7 +91,10 @@ Future<bool> sendArgsToUixSocket(
82
91
}
83
92
}
84
93
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,
86
98
void Function (List <dynamic > args) cmdProcessor, {
87
99
bool kDebugMode = false
88
100
}) async {
0 commit comments