@@ -6,6 +6,9 @@ import 'dart:typed_data';
66import 'package:path_provider/path_provider.dart' ;
77import '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.}
912Future <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.
2331Future <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 {
0 commit comments