Skip to content

Commit 83a4c8a

Browse files
committed
Hacking.
1 parent 367551e commit 83a4c8a

File tree

3 files changed

+44
-116
lines changed

3 files changed

+44
-116
lines changed

pkgs/watcher/lib/src/directory_watcher/mac_os.dart

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class _MacOSDirectoryWatcher
205205
// as the directory's full content will be listed.
206206
var createdDirectories = unionAll(batch.map((event) {
207207
return event.type == EventType.createDirectory
208-
? event.paths
208+
? {event.path}
209209
: const <String>{};
210210
}));
211211

@@ -241,73 +241,35 @@ class _MacOSDirectoryWatcher
241241
/// indicate that the state of the path on the filesystem should be checked to
242242
/// determine what occurred.
243243
Event? _canonicalEvent(Set<Event> batch) {
244-
// An empty batch indicates that we've learned earlier that the batch is
245-
// contradictory (e.g. because of a move).
246244
if (batch.isEmpty) return null;
247245

248-
var path = batch.first.path;
249-
var type = batch.first.type;
250-
var isDirectory = batch.first.isDirectory;
251-
var hadModifyEvent = false;
252-
253-
for (var event in batch.skip(1)) {
254-
// If one event reports that the file is a directory and another event
255-
// doesn't, that's a contradiction.
256-
if (isDirectory != event.isDirectory) return null;
257-
258-
// Modify events don't contradict either CREATE or REMOVE events. We can
259-
// safely assume the file was modified after a CREATE or before the
260-
// REMOVE; otherwise there will also be a REMOVE or CREATE event
261-
// (respectively) that will be contradictory.
262-
if (event.type == EventType.modifyFile) {
263-
hadModifyEvent = true;
264-
continue;
265-
}
266-
assert(event.isCreate || event.isDelete);
246+
var types = batch.map((e) => e.type).toSet();
267247

268-
// If we previously thought this was a MODIFY, we now consider it to be a
269-
// CREATE or REMOVE event. This is safe for the same reason as above.
270-
if (type == EventType.modifyFile) {
271-
type = event.type;
272-
continue;
248+
if (types.length == 2 &&
249+
types.contains(EventType.modifyFile) &&
250+
types.contains(EventType.createFile)) {
251+
if (_files.contains(path)) {
252+
types.remove(EventType.createFile);
253+
} else {
254+
types.remove(EventType.modifyFile);
273255
}
274-
275-
// A CREATE event contradicts a REMOVE event and vice versa.
276-
assert(type == EventType.createFile ||
277-
type == EventType.createDirectory ||
278-
type == EventType.delete);
279-
if (type != event.type) return null;
280256
}
281257

282-
// If we got a CREATE event for a file we already knew about, that comes
283-
// from FSEvents reporting an add that happened prior to the watch
284-
// beginning. If we also received a MODIFY event, we want to report that,
285-
// but not the CREATE.
286-
if (type == EventType.createFile &&
287-
hadModifyEvent &&
288-
_files.contains(path)) {
289-
type = EventType.modifyFile;
258+
if (types.length != 1) {
259+
return null;
290260
}
291261

292-
switch (type) {
293-
case EventType.createDirectory:
294-
// Issue 16003 means that a CREATE event for a directory can indicate
295-
// that the directory was moved and then re-created.
296-
// [_eventsBasedOnFileSystem] will handle this correctly by producing a
297-
// DELETE event followed by a CREATE event if the directory exists.
298-
return null;
299-
300-
case EventType.createFile:
301-
case EventType.delete:
302-
case EventType.modifyFile:
303-
return batch.firstWhere((e) => e.type == type);
304-
305-
// Guaranteed not present by `_sortEvents`.
306-
case EventType.moveFile:
307-
case EventType.moveDirectory:
308-
case EventType.modifyDirectory:
309-
throw StateError(type.name);
262+
final type = types.first;
263+
264+
if (type == EventType.createDirectory) {
265+
// Issue 16003 means that a CREATE event for a directory can indicate
266+
// that the directory was moved and then re-created.
267+
// [_eventsBasedOnFileSystem] will handle this correctly by producing a
268+
// DELETE event followed by a CREATE event if the directory exists.
269+
return null;
310270
}
271+
272+
return batch.firstWhere((e) => e.type == type);
311273
}
312274

313275
/// Returns one or more events that describe the change between the last known

pkgs/watcher/lib/src/directory_watcher/windows.dart

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ class _WindowsDirectoryWatcher
255255
batch.map((event) {
256256
if (event.type == EventType.createDirectory ||
257257
event.type == EventType.moveDirectory) {
258-
return event.paths;
258+
final destination = event.destination;
259+
return {event.path, if (destination != null) destination};
259260
}
260261
return const <String>{};
261262
}),
@@ -273,8 +274,10 @@ class _WindowsDirectoryWatcher
273274
if (event.type == EventType.modifyDirectory) {
274275
continue;
275276
}
276-
for (final path in event.paths) {
277-
addEvent(path, event);
277+
addEvent(path, event);
278+
final destination = event.destination;
279+
if (destination != null) {
280+
addEvent(destination, event);
278281
}
279282
}
280283

@@ -292,57 +295,27 @@ class _WindowsDirectoryWatcher
292295
/// indicate that the state of the path on the filesystem should be checked to
293296
/// determine what occurred.
294297
Event? _canonicalEvent(Set<Event> batch) {
295-
// An empty batch indicates that we've learned earlier that the batch is
296-
// contradictory (e.g. because of a move).
297298
if (batch.isEmpty) return null;
299+
var types = batch.map((e) => e.type).toSet();
298300

299-
var type = batch.first.type;
300-
var isDirectory = batch.first.isDirectory;
301-
302-
for (var event in batch.skip(1)) {
303-
// If one event reports that the file is a directory and another event
304-
// doesn't, that's a contradiction.
305-
if (isDirectory != event.isDirectory) return null;
306-
307-
// Modify events don't contradict either CREATE or REMOVE events. We can
308-
// safely assume the file was modified after a CREATE or before the
309-
// REMOVE; otherwise there will also be a REMOVE or CREATE event
310-
// (respectively) that will be contradictory.
311-
if (event.isModify) continue;
312-
assert(
313-
event.isCreate || event.isDelete || event.isMove,
314-
);
315-
316-
// If we previously thought this was a MODIFY, we now consider it to be a
317-
// CREATE or REMOVE event. This is safe for the same reason as above.
318-
if (type == EventType.modifyFile) {
319-
type = event.type;
320-
continue;
321-
}
322-
323-
// A CREATE event contradicts a REMOVE event and vice versa.
324-
assert(type == EventType.createFile ||
325-
type == EventType.createDirectory ||
326-
type == EventType.delete ||
327-
type == EventType.moveFile);
328-
if (type != event.type) return null;
301+
if (types.length == 2 &&
302+
types.contains(EventType.modifyFile) &&
303+
types.contains(EventType.createFile)) {
304+
types.remove(EventType.modifyFile);
329305
}
330306

331-
switch (type) {
332-
// Move events are always resolved by checking the filesystem state.
333-
case EventType.moveFile:
334-
case EventType.moveDirectory:
335-
return null;
307+
if (types.length != 1) {
308+
return null;
309+
}
336310

337-
case EventType.createFile:
338-
case EventType.createDirectory:
339-
case EventType.delete:
340-
case EventType.modifyFile:
341-
return batch.firstWhere((e) => e.type == type);
311+
final type = types.first;
342312

343-
case EventType.modifyDirectory:
344-
throw StateError(EventType.modifyDirectory.name);
313+
// Move events are always resolved by checking the filesystem state.
314+
if (type == EventType.moveDirectory || type == EventType.moveFile) {
315+
return null;
345316
}
317+
318+
return batch.firstWhere((e) => e.type == type);
346319
}
347320

348321
/// Returns zero or more events that describe the change between the last

pkgs/watcher/lib/src/event.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,12 @@ extension type Event(FileSystemEvent event) {
7676
/// For other types of event, always `null`.
7777
String? get destination =>
7878
isMove ? (event as FileSystemMoveEvent).destination : null;
79-
80-
/// See [FileSystemEvent.isDirectory].
81-
///
82-
/// For delete events, always `null`.
83-
bool? get isDirectory => isDelete ? null : event.isDirectory;
84-
85-
/// All paths mentioned by the event.
86-
///
87-
/// This is [path] plus, for move events, [destination] if it's not `null`.
88-
Set<String> get paths => {event.path, if (destination != null) destination!};
8979
}
9080

9181
/// See [FileSystemEvent.type].
82+
///
83+
/// This additionally encodes [FileSystemEvent.isDirectory], which is specified
84+
/// for all event types except deletes.
9285
enum EventType {
9386
delete,
9487
createFile,

0 commit comments

Comments
 (0)