|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +/// Extension type replacing [FileSystemEvent] for `package:watcher` internal |
| 8 | +/// use. |
| 9 | +/// |
| 10 | +/// The [FileSystemDeleteEvent] subclass of [FileSystemEvent] does something |
| 11 | +/// surprising for `isDirectory`: it always returns `false`. The constructor |
| 12 | +/// accepts a boolean called `isDirectory` but discards it. |
| 13 | +/// |
| 14 | +/// So, this extension type hides `isDirectory` and instead provides an |
| 15 | +/// [EventType] enum with the seven types of event actually used. |
| 16 | +extension type Event(FileSystemEvent event) { |
| 17 | + /// A create event for a file at [path]. |
| 18 | + static Event createFile(String path) => |
| 19 | + Event(FileSystemCreateEvent(path, false)); |
| 20 | + |
| 21 | + /// A create event for a directory at [path]. |
| 22 | + static Event createDirectory(String path) => |
| 23 | + Event(FileSystemCreateEvent(path, true)); |
| 24 | + |
| 25 | + /// A delete event for [path]. |
| 26 | + /// |
| 27 | + /// Delete events do not specify whether they are for files or directories. |
| 28 | + static Event delete(String path) => Event(FileSystemDeleteEvent( |
| 29 | + path, |
| 30 | + // `FileSystemDeleteEvent` just discards `isDirectory`. |
| 31 | + false /* isDirectory */)); |
| 32 | + |
| 33 | + /// A modify event for the file at [path]. |
| 34 | + static Event modifyFile(String path) => Event(FileSystemModifyEvent( |
| 35 | + path, |
| 36 | + false /* isDirectory */, |
| 37 | + // Don't set contentChanged, even pass through from the OS, as it's not |
| 38 | + // used in `package:watcher`. |
| 39 | + false /* contentChanged */)); |
| 40 | + |
| 41 | + /// A modify event for the directory at [path]. |
| 42 | + static Event modifyDirectory(String path) => Event(FileSystemModifyEvent( |
| 43 | + path, |
| 44 | + true /* isDirectory */, |
| 45 | + // `contentChanged` is not used by `package:watcher`, don't set it. |
| 46 | + false)); |
| 47 | + |
| 48 | + /// See [FileSystemEvent.path]. |
| 49 | + String get path => event.path; |
| 50 | + |
| 51 | + EventType get type { |
| 52 | + switch (event.type) { |
| 53 | + case FileSystemEvent.create: |
| 54 | + return event.isDirectory |
| 55 | + ? EventType.createDirectory |
| 56 | + : EventType.createFile; |
| 57 | + case FileSystemEvent.delete: |
| 58 | + return EventType.delete; |
| 59 | + case FileSystemEvent.modify: |
| 60 | + return event.isDirectory |
| 61 | + ? EventType.modifyDirectory |
| 62 | + : EventType.modifyFile; |
| 63 | + case FileSystemEvent.move: |
| 64 | + return event.isDirectory ? EventType.moveDirectory : EventType.moveFile; |
| 65 | + default: |
| 66 | + throw StateError('Invalid event type ${event.type}.'); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// See [FileSystemMoveEvent.destination]. |
| 71 | + /// |
| 72 | + /// Move events with `null` destination are invalid according to [isValid] and |
| 73 | + /// should be dropped. |
| 74 | + /// |
| 75 | + /// For other types of event, always `null`. |
| 76 | + String? get destination => event.type == FileSystemEvent.move |
| 77 | + ? (event as FileSystemMoveEvent).destination |
| 78 | + : null; |
| 79 | + |
| 80 | + /// Whether the event is valid and can be processed. |
| 81 | + /// |
| 82 | + /// If not, it should be dropped. |
| 83 | + bool get isValid { |
| 84 | + if (type == EventType.moveDirectory || type == EventType.moveFile) { |
| 85 | + if (destination == null) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// See [FileSystemEvent.type]. |
| 94 | +/// |
| 95 | +/// This additionally encodes [FileSystemEvent.isDirectory], which is specified |
| 96 | +/// for all event types except deletes. |
| 97 | +enum EventType { |
| 98 | + delete, |
| 99 | + createFile, |
| 100 | + createDirectory, |
| 101 | + modifyFile, |
| 102 | + modifyDirectory, |
| 103 | + moveFile, |
| 104 | + moveDirectory; |
| 105 | +} |
0 commit comments