@@ -100,76 +100,85 @@ abstract class FlutterSingleInstance {
100
100
/// Returns true if this is the first instance of the app.
101
101
/// Automatically writes a pid file to the temp directory if this is the first instance.
102
102
Future <bool > isFirstInstance () async {
103
- if (debugMode) {
104
- logger.finest ("Debug mode enabled, reporting as first instance" );
105
- return true ;
106
- }
107
-
108
103
var processName = await getProcessName (pid); // get name of current process
109
104
processName! ;
110
105
111
106
var pidFile = await getPidFile (processName);
112
107
pidFile! ;
113
108
114
- if (! pidFile.existsSync ()) {
115
- logger.finest ("No pid file found, activating instance" );
109
+ Future <bool > check () async {
110
+ if (debugMode) {
111
+ logger.finest ("Debug mode enabled, reporting as first instance" );
112
+ return true ;
113
+ }
116
114
117
- // No pid file, so this is the first instance.
118
- await activateInstance (processName);
119
- return true ;
120
- }
115
+ if (! pidFile.existsSync ()) {
116
+ logger.finest ("No pid file found, activating instance" );
121
117
122
- final data = await pidFile.readAsString ();
118
+ // No pid file, so this is the first instance.
119
+ return true ;
120
+ }
123
121
124
- final json ;
122
+ final data = await pidFile. readAsString () ;
125
123
126
- try {
127
- json = jsonDecode (data);
128
- } catch (e, s) {
129
- logger.finest ("Pid file is wrong format, assuming first instance" , e, s);
130
- return true ;
131
- }
124
+ final json;
125
+
126
+ try {
127
+ json = jsonDecode (data);
128
+ } catch (e, s) {
129
+ logger.finest (
130
+ "Pid file is wrong format, assuming first instance" , e, s);
131
+ return true ;
132
+ }
133
+
134
+ _instance = Instance .fromJson (json);
135
+
136
+ logger.finest ("Pid file found, verifying instance: $_instance " );
132
137
133
- _instance = Instance . fromJson (json );
138
+ final pidName = await getProcessName (_instance ! .pid );
134
139
135
- logger.finest ("Pid file found, verifying instance: $_instance " );
140
+ if (processName == pidName) {
141
+ logger.finest (
142
+ "Process name matches $processName , reporting as second instance" ,
143
+ );
136
144
137
- final pidName = await getProcessName (_instance! .pid);
145
+ // Process exists, so this is not the first instance.
146
+ return false ;
147
+ }
138
148
139
- if (processName == pidName) {
140
149
logger.finest (
141
- "Process name matches $processName , reporting as second instance" ,
150
+ "Process name does not match $processName , activating instance" ,
142
151
);
143
-
144
- // Process exists, so this is not the first instance.
145
- return false ;
152
+ return true ;
146
153
}
147
154
148
- logger.finest (
149
- "Process name does not match $processName , activating instance" ,
150
- );
155
+ final isFirstInstance = await check ();
151
156
152
- // Process does not exist, so we can activate this instance.
153
- await activateInstance (processName);
157
+ if (isFirstInstance) await activateInstance (processName);
154
158
155
- return true ;
159
+ return isFirstInstance ;
156
160
}
157
161
158
162
/// Activates the first instance of the app and writes a pid file to the temp directory.
159
163
@protected
160
164
Future <void > activateInstance (String processName) async {
161
165
var pidFile = await getPidFile (processName);
162
166
163
- if (pidFile? .existsSync () == false ) await pidFile? .create ();
167
+ if (pidFile == null ) {
168
+ logger.finest ("Failed to retrieve process name, aborting" );
169
+ return ;
170
+ }
171
+
172
+ if (pidFile.existsSync () == false ) await pidFile.create ();
164
173
165
174
final instance = Instance (
166
175
pid: pid,
167
176
port: await startRpcServer (),
168
177
);
169
178
170
- await pidFile? .writeAsString (jsonEncode (instance.toJson ()));
179
+ await pidFile.writeAsString (jsonEncode (instance.toJson ()));
171
180
172
- logger.finest ("Activated $instance at ${pidFile ? .path }" );
181
+ logger.finest ("Activated $instance at ${pidFile .path }" );
173
182
}
174
183
175
184
/// Returns the pid file.
@@ -183,6 +192,11 @@ abstract class FlutterSingleInstance {
183
192
/// Starts an RPC server that listens for focus requests.
184
193
@protected
185
194
Future <int > startRpcServer () async {
195
+ if (_server != null ) {
196
+ logger.finest ("RPC server already started" );
197
+ return _server! .port! ;
198
+ }
199
+
186
200
logger.finest ("Starting RPC server" );
187
201
188
202
_server = Server .create (
0 commit comments