FileSystemObserver: FileSystemObserver() constructor

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Non-standard: This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The FileSystemObserver() constructor creates a new FileSystemObserver object instance.

Syntax

js
new FileSystemObserver(callback)

Parameters

callback

A user-defined callback function that will be called when the observer has observed a change in the file system entry it has been asked to observe (via FileSystemObserver.observe()). The callback function will be passed the following two parameters:

records

An array of FileSystemChangeRecord objects that contain details of all the observed changes.

observer

A reference to the current FileSystemObserver object, which is made available in case, for example, you want to stop observations after the current records have been received using the FileSystemObserver.disconnect() method.

Return value

A new FileSystemObserver object.

Examples

Note: For a complete working example, check out File System Observer Demo (source code).

Initializing a FileSystemObserver

Before you can start observing file or directory changes, you need to initialize a FileSystemObserver to handle the observations:

js
const observer = new FileSystemObserver(callback);

The callback function body can be specified to return and process file change observations in any way you want:

js
const callback = (records, observer) => {
  for (const record of records) {
    console.log("Change detected:", record);
    const reportContent = `Change observed to ${record.changedHandle.kind} ${record.changedHandle.name}. Type: ${record.type}.`;
    sendReport(reportContent); // Some kind of user-defined reporting function
  }

  observer.disconnect();
};

Specifications

Not currently part of a specification. See https://github.com/whatwg/fs/pull/165 for the relevant specification PR.

Browser compatibility

See also