@@ -8,6 +8,7 @@ import 'dart:io';
8
8
import 'package:path/path.dart' as p;
9
9
10
10
import '../directory_watcher.dart' ;
11
+ import '../event.dart' ;
11
12
import '../path_set.dart' ;
12
13
import '../resubscribable.dart' ;
13
14
import '../utils.dart' ;
@@ -63,7 +64,7 @@ class _MacOSDirectoryWatcher
63
64
///
64
65
/// This is separate from [_listSubscriptions] because this stream
65
66
/// occasionally needs to be resubscribed in order to work around issue 14849.
66
- StreamSubscription <List <FileSystemEvent >>? _watchSubscription;
67
+ StreamSubscription <List <Event >>? _watchSubscription;
67
68
68
69
/// The subscription to the [Directory.list] call for the initial listing of
69
70
/// the directory to determine its initial state.
@@ -109,7 +110,7 @@ class _MacOSDirectoryWatcher
109
110
}
110
111
111
112
/// The callback that's run when [Directory.watch] emits a batch of events.
112
- void _onBatch (List <FileSystemEvent > batch) {
113
+ void _onBatch (List <Event > batch) {
113
114
// If we get a batch of events before we're ready to begin emitting events,
114
115
// it's probable that it's a batch of pre-watcher events (see issue 14373).
115
116
// Ignore those events and re-list the directory.
@@ -132,8 +133,8 @@ class _MacOSDirectoryWatcher
132
133
: [canonicalEvent];
133
134
134
135
for (var event in events) {
135
- if (event is FileSystemCreateEvent ) {
136
- if (! event.isDirectory) {
136
+ if (event.isCreate ) {
137
+ if (! event.isDirectory! ) {
137
138
// If we already know about the file, treat it like a modification.
138
139
// This can happen if a file is copied on top of an existing one.
139
140
// We'll see an ADD event for the latter file when from the user's
@@ -163,11 +164,11 @@ class _MacOSDirectoryWatcher
163
164
});
164
165
subscription.onError (_emitError);
165
166
_listSubscriptions.add (subscription);
166
- } else if (event is FileSystemModifyEvent ) {
167
- assert (! event.isDirectory);
167
+ } else if (event.isModify ) {
168
+ assert (! event.isDirectory! );
168
169
_emitEvent (ChangeType .MODIFY , path);
169
170
} else {
170
- assert (event is FileSystemDeleteEvent );
171
+ assert (event.isDelete );
171
172
for (var removedPath in _files.remove (path)) {
172
173
_emitEvent (ChangeType .REMOVE , removedPath);
173
174
}
@@ -184,8 +185,8 @@ class _MacOSDirectoryWatcher
184
185
///
185
186
/// The returned events won't contain any [FileSystemMoveEvent] s, nor will it
186
187
/// contain any events relating to [path] .
187
- Map <String , Set <FileSystemEvent >> _sortEvents (List <FileSystemEvent > batch) {
188
- var eventsForPaths = < String , Set <FileSystemEvent >> {};
188
+ Map <String , Set <Event >> _sortEvents (List <Event > batch) {
189
+ var eventsForPaths = < String , Set <Event >> {};
189
190
190
191
// FSEvents can report past events, including events on the root directory
191
192
// such as it being created. We want to ignore these. If the directory is
@@ -196,27 +197,21 @@ class _MacOSDirectoryWatcher
196
197
// directory's full contents will be examined anyway, so we ignore such
197
198
// events. Emitting them could cause useless or out-of-order events.
198
199
var directories = unionAll (batch.map ((event) {
199
- if (! event.isDirectory) return < String > {};
200
- if (event is FileSystemMoveEvent ) {
201
- var destination = event.destination;
202
- if (destination != null ) {
203
- return {event.path, destination};
204
- }
205
- }
206
- return {event.path};
200
+ if (event.isDelete || ! event.isDirectory! ) return < String > {};
201
+ return event.paths;
207
202
}));
208
203
209
204
bool isInModifiedDirectory (String path) =>
210
205
directories.any ((dir) => path != dir && p.isWithin (dir, path));
211
206
212
- void addEvent (String path, FileSystemEvent event) {
207
+ void addEvent (String path, Event event) {
213
208
if (isInModifiedDirectory (path)) return ;
214
- eventsForPaths.putIfAbsent (path, () => < FileSystemEvent > {}).add (event);
209
+ eventsForPaths.putIfAbsent (path, () => < Event > {}).add (event);
215
210
}
216
211
217
212
for (var event in batch) {
218
213
// The Mac OS watcher doesn't emit move events. See issue 14806.
219
- assert (event is ! FileSystemMoveEvent );
214
+ assert (! event.isMove );
220
215
addEvent (event.path, event);
221
216
}
222
217
@@ -233,64 +228,66 @@ class _MacOSDirectoryWatcher
233
228
/// If [batch] does contain contradictory events, this returns `null` to
234
229
/// indicate that the state of the path on the filesystem should be checked to
235
230
/// determine what occurred.
236
- FileSystemEvent ? _canonicalEvent (Set <FileSystemEvent > batch) {
231
+ Event ? _canonicalEvent (Set <Event > batch) {
237
232
// An empty batch indicates that we've learned earlier that the batch is
238
233
// contradictory (e.g. because of a move).
239
234
if (batch.isEmpty) return null ;
240
235
241
236
var type = batch.first.type;
242
- var isDir = batch.first.isDirectory;
237
+ var isDirectory = batch.first.isDirectory;
243
238
var hadModifyEvent = false ;
244
239
245
240
for (var event in batch.skip (1 )) {
246
241
// If one event reports that the file is a directory and another event
247
242
// doesn't, that's a contradiction.
248
- if (isDir != event.isDirectory) return null ;
243
+ if (isDirectory != event.isDirectory) return null ;
249
244
250
245
// Modify events don't contradict either CREATE or REMOVE events. We can
251
246
// safely assume the file was modified after a CREATE or before the
252
247
// REMOVE; otherwise there will also be a REMOVE or CREATE event
253
248
// (respectively) that will be contradictory.
254
- if (event is FileSystemModifyEvent ) {
249
+ if (event.isModify ) {
255
250
hadModifyEvent = true ;
256
251
continue ;
257
252
}
258
- assert (event is FileSystemCreateEvent || event is FileSystemDeleteEvent );
253
+ assert (event.isCreate || event.isDelete );
259
254
260
255
// If we previously thought this was a MODIFY, we now consider it to be a
261
256
// CREATE or REMOVE event. This is safe for the same reason as above.
262
- if (type == FileSystemEvent .modify) {
257
+ if (type == EventType .modify) {
263
258
type = event.type;
264
259
continue ;
265
260
}
266
261
267
262
// A CREATE event contradicts a REMOVE event and vice versa.
268
- assert (type == FileSystemEvent .create || type == FileSystemEvent .delete);
263
+ assert (type == EventType .create || type == EventType .delete);
269
264
if (type != event.type) return null ;
270
265
}
271
266
272
267
// If we got a CREATE event for a file we already knew about, that comes
273
268
// from FSEvents reporting an add that happened prior to the watch
274
269
// beginning. If we also received a MODIFY event, we want to report that,
275
270
// but not the CREATE.
276
- if (type == FileSystemEvent .create &&
271
+ if (type == EventType .create &&
277
272
hadModifyEvent &&
278
273
_files.contains (batch.first.path)) {
279
- type = FileSystemEvent .modify;
274
+ type = EventType .modify;
280
275
}
281
276
282
277
switch (type) {
283
- case FileSystemEvent .create:
278
+ case EventType .create:
284
279
// Issue 16003 means that a CREATE event for a directory can indicate
285
280
// that the directory was moved and then re-created.
286
281
// [_eventsBasedOnFileSystem] will handle this correctly by producing a
287
282
// DELETE event followed by a CREATE event if the directory exists.
288
- if (isDir) return null ;
289
- return FileSystemCreateEvent (batch.first.path, false );
290
- case FileSystemEvent .delete:
291
- return FileSystemDeleteEvent (batch.first.path, isDir);
292
- case FileSystemEvent .modify:
293
- return FileSystemModifyEvent (batch.first.path, isDir, false );
283
+ if (isDirectory! ) return null ;
284
+ return Event .createFile (batch.first.path);
285
+ case EventType .delete:
286
+ return Event .delete (batch.first.path);
287
+ case EventType .modify:
288
+ return isDirectory!
289
+ ? Event .modifyDirectory (batch.first.path)
290
+ : Event .modifyFile (batch.first.path);
294
291
default :
295
292
throw StateError ('unreachable' );
296
293
}
@@ -303,35 +300,35 @@ class _MacOSDirectoryWatcher
303
300
/// to the user, unlike the batched events from [Directory.watch] . The
304
301
/// returned list may be empty, indicating that no changes occurred to [path]
305
302
/// (probably indicating that it was created and then immediately deleted).
306
- List <FileSystemEvent > _eventsBasedOnFileSystem (String path) {
303
+ List <Event > _eventsBasedOnFileSystem (String path) {
307
304
var fileExisted = _files.contains (path);
308
305
var dirExisted = _files.containsDir (path);
309
306
var fileExists = File (path).existsSync ();
310
307
var dirExists = Directory (path).existsSync ();
311
308
312
- var events = < FileSystemEvent > [];
309
+ var events = < Event > [];
313
310
if (fileExisted) {
314
311
if (fileExists) {
315
- events.add (FileSystemModifyEvent (path, false , false ));
312
+ events.add (Event . modifyFile (path));
316
313
} else {
317
- events.add (FileSystemDeleteEvent (path, false ));
314
+ events.add (Event . delete (path));
318
315
}
319
316
} else if (dirExisted) {
320
317
if (dirExists) {
321
318
// If we got contradictory events for a directory that used to exist and
322
319
// still exists, we need to rescan the whole thing in case it was
323
320
// replaced with a different directory.
324
- events.add (FileSystemDeleteEvent (path, true ));
325
- events.add (FileSystemCreateEvent (path, true ));
321
+ events.add (Event . delete (path));
322
+ events.add (Event . createDirectory (path));
326
323
} else {
327
- events.add (FileSystemDeleteEvent (path, true ));
324
+ events.add (Event . delete (path));
328
325
}
329
326
}
330
327
331
328
if (! fileExisted && fileExists) {
332
- events.add (FileSystemCreateEvent (path, false ));
329
+ events.add (Event . createFile (path));
333
330
} else if (! dirExisted && dirExists) {
334
- events.add (FileSystemCreateEvent (path, true ));
331
+ events.add (Event . createDirectory (path));
335
332
}
336
333
337
334
return events;
0 commit comments