|
| 1 | +--- |
| 2 | +title: FileSystemChangeRecord |
| 3 | +slug: Web/API/FileSystemChangeRecord |
| 4 | +page-type: web-api-interface |
| 5 | +--- |
| 6 | + |
| 7 | +{{APIRef("File System API")}} |
| 8 | + |
| 9 | +The **`FileSystemChangeRecord`** dictionary of the {{domxref("File System API", "File System API", "", "nocode")}} contains details of a single change observed by a {{domxref("FileSystemObserver")}}. |
| 10 | + |
| 11 | +The `records` argument passed into the {{domxref("FileSystemObserver.FileSystemObserver", "FileSystemObserver()")}} constructor's callback function is an array of `FileSystemChangeRecord` objects. |
| 12 | + |
| 13 | +## Instance properties |
| 14 | + |
| 15 | +- `changedHandle` |
| 16 | + |
| 17 | + - : A reference to the file system handle that the change was observed on. |
| 18 | + |
| 19 | + - For the user-observable file system, this can be a {{domxref("FileSystemFileHandle")}} or a {{domxref("FileSystemDirectoryHandle")}}. |
| 20 | + - For the [Origin Private File System](/en-US/docs/Web/API/File_System_API/Origin_private_file_system) (OPFS), it can be a {{domxref("FileSystemFileHandle")}}, a {{domxref("FileSystemDirectoryHandle")}}, or a {{domxref("FileSystemSyncAccessHandle")}}. |
| 21 | + |
| 22 | + This property will be `null` for records with a `"disappeared"`, `"errored"`, or `"unknown"` type. |
| 23 | + |
| 24 | +- `relativePathComponents` |
| 25 | + - : An array containing the path components that make up the relative file path from the `root` to the `changedHandle`, including the `changedHandle` filename. |
| 26 | +- `relativePathMovedFrom` |
| 27 | + - : An array containing the path components that make up the relative file path from the `root` to the `changedHandle`'s former location, in the case of observations with a `"moved"` type. If the type is not `"moved"`, this property will be `null`. |
| 28 | +- `root` |
| 29 | + - : A reference to the root file system handle, that is, the one passed to the `observe()` call that started the observation. Again, this can be a {{domxref("FileSystemFileHandle")}}, {{domxref("FileSystemDirectoryHandle")}}, or {{domxref("FileSystemSyncAccessHandle")}}. |
| 30 | +- `type` |
| 31 | + - : A string representing the type of change that was observed. Possible values are: |
| 32 | + - `appeared` |
| 33 | + - : The file or directory was created or moved into the `root` file structure. |
| 34 | + - `disappeared` |
| 35 | + - : The file or directory was deleted or moved out of the `root` file structure. To find out which file or directory disappeared, you can query the `relativePathComponents` property. |
| 36 | + - `errored` |
| 37 | + - : An error state occured in the observed directory. This can result when: |
| 38 | + - The observation is no longer valid. This can occur when the observed handle (that is, the `root` of the observation) is deleted or moved. In this case, a `"disappeared"` observation will be recorded, followed by an `"errored"` observation. In such cases, you may wish to stop observing the file system using {{domxref("FileSystemObserver.disconnect()")}}. |
| 39 | + - The maximum limit of per-origin observations is reached. This limit is dependent on the operating system and not known beforehand. If this happens, the site may decide to retry, though there's no guarantee that the operating system will have freed up enough resources. |
| 40 | + - Permission to access the directory or file handle is removed. |
| 41 | + - `modified` |
| 42 | + - : The file or directory was modified. |
| 43 | + - `moved` |
| 44 | + - : The file or directory was moved within the root file structure. |
| 45 | + > [!NOTE] |
| 46 | + > On Windows, `"moved"` observations aren't supported between directories. They are reported as a `"disappeared"` observation in the source directory and an `"appeared"` observation in the destination directory. |
| 47 | + - `unknown` |
| 48 | + - : Indicates that some observations were missed. If you wish to find out information on what changed in the missed observations, you could fall back to polling the observed directory. |
| 49 | + |
| 50 | +Depending on the operating system, not all observations will be reported with the same level of detail, for example, when the contents of a directory change recursively. At best, the website will receive a detailed change record containing the type of change and a handle to the affected path. At worst, the website will receive a more generic change record (that is, an `"unknown"` type) that still requires it to enumerate the directory to figure out which handle changed. |
| 51 | + |
| 52 | +This is still an improvement over polling, since the directory enumeration can be kicked off on-demand from the callback function, rather than needing to poll for changes periodically. |
| 53 | + |
| 54 | +## Examples |
| 55 | + |
| 56 | +### Initialize a `FileSystemObserver` |
| 57 | + |
| 58 | +Before you can start observing file or directory changes, you need to initialize a `FileSystemObserver` to handle the observations. This is done using the {{domxref("FileSystemObserver.FileSystemObserver", "FileSystemObserver()")}} constructor, which takes a callback function as an argument: |
| 59 | + |
| 60 | +```js |
| 61 | +const observer = new FileSystemObserver(callback); |
| 62 | +``` |
| 63 | + |
| 64 | +The [callback function](/en-US/docs/Web/API/FileSystemObserver/FileSystemObserver#callback) body can be specified to return and process file change observations in any way you want. Each object inside the `records` array is a `FileSystemChangeRecord` object: |
| 65 | + |
| 66 | +```js |
| 67 | +const callback = (records, observer) => { |
| 68 | + for (const record of records) { |
| 69 | + console.log("Change detected:", record); |
| 70 | + const reportContent = `Change observed to ${record.changedHandle.kind} ${record.changedHandle.name}. Type: ${record.type}.`; |
| 71 | + sendReport(reportContent); // Some kind of user-defined reporting function |
| 72 | + } |
| 73 | + |
| 74 | + observer.disconnect(); |
| 75 | +}; |
| 76 | +``` |
| 77 | + |
| 78 | +## Specifications |
| 79 | + |
| 80 | +Not currently part of a specification. See [https://github.com/whatwg/fs/pull/165](https://github.com/whatwg/fs/pull/165) for the relevant specification PR. |
| 81 | + |
| 82 | +## See also |
| 83 | + |
| 84 | +- {{domxref("FileSystemObserver.FileSystemObserver", "FileSystemObserver()")}} constructor |
| 85 | +- [File System API](/en-US/docs/Web/API/File_System_API) |
| 86 | +- [The File System Observer API origin trial](https://developer.chrome.com/blog/file-system-observer#stop-observing-the-file-system) on developer.chrome.com (2024) |
0 commit comments