Skip to content

Commit db5f3ba

Browse files
authored
Update FileSystemObserver explainer (#163)
Update FileSystemObserver explainer This commit renames some FileSystemChangeType values, adds a section about interactions with Back/forward Cache, and updates API position by Gecko.
1 parent b38f13b commit db5f3ba

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

proposals/FileSystemObserver.md

+24-21
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,17 @@ async function markDirty(record) {
189189
// Decide how to mark the file dirty according to the
190190
// `FileSystemChangeType` included in each file system change record.
191191
switch (record.type) {
192-
case 'created':
192+
case 'appeared':
193193
// `record.root` is the handle passed to `observe()`. Note that
194194
// the File System specification does not expose the concept of an
195195
// absolute path, so understanding a file system change is
196196
// inherently relative to some directory.
197-
markCreated(record.root, record.relativePathComponents);
197+
markAppeared(record.root, record.relativePathComponents);
198198
break;
199-
case 'deleted':
199+
case 'disappeared':
200200
// The relative path of the changed handle may be more useful than
201201
// the handle itself, since the file no longer exists.
202-
markDeleted(record.root, record.relativePathComponents);
202+
markDisappeared(record.root, record.relativePathComponents);
203203
break;
204204
case 'modified':
205205
// A handle to the changed path may be more useful than its
@@ -219,8 +219,8 @@ async function markDirty(record) {
219219
markMoved(record.root, record.relativePathMovedFrom,
220220
record.relativePathComponents);
221221
break;
222-
case 'unsupported':
223-
// Change types may not be supported on all platforms.
222+
case 'unknown':
223+
// Unknown change event(s) may have been missed.
224224
if (await checkIfChanged(record.changedHandle)) {
225225
markChanged(record.root, record.relativePathComponents);
226226
}
@@ -264,7 +264,7 @@ async function handleChanges(records) {
264264
const changedHandle = record.changedHandle;
265265

266266
// Take advantage of file-level notifications, if available.
267-
if (changedHandle.kind === 'file' && record.type === 'created') {
267+
if (changedHandle.kind === 'file' && record.type === 'appeared') {
268268
sawFileCreatedRecord = true;
269269
readNewFile(changedHandle);
270270
}
@@ -304,6 +304,10 @@ Likewise, changes which occur before an observer is created should not be report
304304

305305
A `FileSystemObserver` is not [serializable](https://html.spec.whatwg.org/multipage/structured-data.html#serializable) and therefore cannot be persisted across browsing sessions. Websites which wish to watch the same files on each session may store serializable `FileSystemHandle` and `FileSystemObserverObserveOptions` objects in IndexedDB, then create a `FileSystemObserver` and configure it from these objects on page reload.
306306

307+
### Interactions with Back/forward Cache
308+
309+
If changes occurred while the page was not fully active, and the page becomes active again (i.e. back/forward cache), then user agents may use the `"unknown"` `FileSystemChangeType` to indicate that _changes_ have occurred. Specific types and ordering of changes should not be exposed but indicating that some changes have occurred could be useful to the website to perform any special handling.
310+
307311
### Signaling Changes Made via a `FileSystemSyncAccessHandle`
308312

309313
It is assumed that a user agent’s implementation of the `FileSystemObserver` interface will involve coordinating with a centralized browser process. However, unlike most web storage APIs, reading and writing files with a `FileSystemSyncAccessHandle` is commonly implemented largely without coordinating with a centralized browser process. This is critical to the exceptional performance characteristics of this interface. `write()` or `truncate()` operations on a `FileSystemSyncAccessHandle` should trigger a file system change record, but requiring round-trip IPC to complete before synchronously returning would be detrimental to performance.
@@ -343,8 +347,8 @@ However, given the cross-platform differences, this proposal does not attempt to
343347
```javascript
344348
const callback = (records, observer) => {
345349
// What change record will be triggered when the file is created?
346-
// -> 1: { type: "create", relativePathComponents: ["file.txt"], ... }
347-
// 2: { type: "create", relativePathComponents: [], ... }
350+
// -> 1: { type: "appeared", relativePathComponents: ["file.txt"], ... }
351+
// 2: { type: "appeared", relativePathComponents: [], ... }
348352
// 3: { type: "modified", relativePathComponents: [], ... }
349353
}
350354

@@ -355,15 +359,15 @@ await directoryHandle.getFileHandle('file.txt', { create: true });
355359

356360
User agents should attempt to include the most precise information as it can reasonably obtain in the file system change record. In this example, the change record is most useful if it details that a specific file has been added (i.e. option 1) as opposed to mentioning just that the parent directory’s contents were modified - which would require the website to iterate through the directory to figure out which file has changed, and how.
357361

358-
All changes to a Bucket File System should deterministically map to a precise file system change record. In this example, the `getFileHandle()` call should result in a change record with a `create` change type and describe the change as occurring on the created file.
362+
All changes to a Bucket File System should deterministically map to a precise file system change record. In this example, the `getFileHandle()` call should result in a change record with a `appeared` change type and describe the change as occurring on the created file.
359363

360-
However, this level of detail is not realistic on all platforms for local file system changes. For example, Linux has no native support for recursive watches. As such, the details of a file system change record for a given change to the local file system should be regarded as best-effort. In the example below, the user agent may report either a `create` change type describing the created file, a `create` change type describing a creation within the observed directory, or a `”modified”` change type describing that the directory contents were modified.
364+
However, this level of detail is not realistic on all platforms for local file system changes. For example, Linux has no native support for recursive watches. As such, the details of a file system change record for a given change to the local file system should be regarded as best-effort. In the example below, the user agent may report either a `appeared` change type describing the created file, a `appeared` change type describing a creation within the observed directory, or a `”modified”` change type describing that the directory contents were modified.
361365

362366
```javascript
363367
const callback = (records, observer) => {
364368
// What change record will be triggered when the file is created?
365-
// ?? 1: { type: "create", relativePathComponents: ["file.txt"], ... }
366-
// ?? 2: { type: "create", relativePathComponents: [], ... }
369+
// ?? 1: { type: "appeared", relativePathComponents: ["file.txt"], ... }
370+
// ?? 2: { type: "appeared", relativePathComponents: [], ... }
367371
// ?? 3: { type: "modified", relativePathComponents: [], ... }
368372
}
369373

@@ -374,8 +378,6 @@ await observer.observe(directoryHandle, { recursive: true });
374378
// corresponding to `directoryHandle`, then `touch file.txt`)
375379
```
376380

377-
User agents may also use the `"unsupported"` `FileSystemChangeType` to explicitly indicate that change types are not supported.
378-
379381
#### When to Signal Local File System Writes
380382

381383
Writing to a file on the local file system generally looks like the following:
@@ -632,7 +634,8 @@ This method could be useful to see which files and directories are being watched
632634
* https://github.com/WICG/file-system-access/issues/72
633635
* https://github.com/whatwg/fs/issues/123
634636
* https://github.com/w3c/IndexedDB/issues/51
635-
* Gecko: No signals
637+
* Gecko: Positive
638+
* https://github.com/mozilla/standards-positions/issues/942#issuecomment-2113526096
636639
* WebKit: No signals
637640

638641
## Appendix
@@ -654,11 +657,11 @@ callback FileSystemObserverCallback = void (
654657
);
655658

656659
enum FileSystemChangeType {
657-
"created",
658-
"deleted",
660+
"appeared",
661+
"disappeared",
659662
"modified",
660663
"moved",
661-
"unsupported", // Change types are not supported on this platform
664+
"unknown", // Change types are not known
662665
"errored" // This observation is no longer valid
663666
};
664667

@@ -676,6 +679,6 @@ interface FileSystemChangeRecord {
676679
// The type of change
677680
readonly attribute FileSystemChangeType type;
678681
// Former location of a moved handle. Used only when type === "moved"
679-
readonly attribute FileSystemHandle? relativePathMovedFrom;
682+
readonly attribute FrozenArray<USVString>? relativePathMovedFrom;
680683
};
681-
```
684+
```

0 commit comments

Comments
 (0)