Skip to content

Commit 62fdee6

Browse files
committed
docs: for fs.watchAsync and fs.unwatchAsync APIs
1 parent 14adb17 commit 62fdee6

File tree

4 files changed

+223
-25
lines changed

4 files changed

+223
-25
lines changed

README.md

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,11 @@ fs.readFile("/path/to/file", 'utf8', function(err, data) {
840840
**Returns**:
841841
`void` (This function does not return anything)
842842

843-
### `fs.writeFile(path, data, options?, callback)` Function
843+
## `fs.writeFile(path, data, options?, callback)` Function
844844

845845
Writes data to a file, replacing the file if it already exists.
846846

847-
#### Parameters:
847+
### Parameters:
848848

849849
- **path**: (`string`)
850850
- The path of the file where data should be written.
@@ -865,7 +865,7 @@ Writes data to a file, replacing the file if it already exists.
865865
- The callback function executed once the file write operation concludes.
866866
- Receives one argument: An error object (or `null` if there were no errors).
867867

868-
#### Example:
868+
### Example:
869869

870870
```javascript
871871
fs.writeFile("/path/to/file", "Hello World", { encoding: 'utf8' }, function(err) {
@@ -880,7 +880,7 @@ fs.writeFile("/path/to/file", "Hello World", 'utf8', function(err) {
880880
```
881881

882882

883-
### `fs.setNodeWSEndpoint(websocketEndpoint)`
883+
## `fs.setNodeWSEndpoint(websocketEndpoint)`
884884

885885
Sets the websocket endpoint and returns a promise that resolves
886886
when the tauri node fs connection is open. It ensures the socket remains
@@ -893,7 +893,7 @@ open across failures and automatically reconnects as necessary.
893893
- Promise<void>
894894

895895

896-
### `fs.forceUseNodeWSEndpoint(use)`
896+
## `fs.forceUseNodeWSEndpoint(use)`
897897

898898
Forces the usage of the Node WebSocket endpoint.
899899
Throws an error if the Node WebSocket endpoint is not set.
@@ -908,7 +908,7 @@ Throws an error if the Node WebSocket endpoint is not set.
908908

909909
---
910910

911-
### `fs.preferNodeWSEndpoint(use)`
911+
## `fs.preferNodeWSEndpoint(use)`
912912

913913
Sets the preference to use the Node WebSocket endpoint if available.
914914
Throws an error if the Node WebSocket endpoint is not set.
@@ -923,3 +923,88 @@ To always force the library to use the Node WebSocket endpoint for all FS APIs,
923923
- Throws an error if the Node WebSocket endpoint has not been set.
924924
- Call `fs.setNodeWSEndpoint(websocketEndpoint)` before calling this this API.
925925

926+
## `fs.watchAsync(pathToWatch, gitIgnorePaths)`
927+
928+
Watch a specific path asynchronously for filesystem changes.
929+
930+
This function returns a promise that resolves an `EventEmitter` that will emit the following events:
931+
932+
- `fs.WATCH_EVENTS.ADD_FILE`: When a file is created.
933+
- `fs.WATCH_EVENTS.ADD_DIR`: When a directory is created.
934+
- `fs.WATCH_EVENTS.UNLINK_FILE`: When a file is deleted.
935+
- `fs.WATCH_EVENTS.UNLINK_DIR`: When a directory is deleted.
936+
- `fs.WATCH_EVENTS.CHANGE`: When a file is changed.
937+
938+
The watcher will ignore all files matching patterns in the provided gitignore.
939+
940+
941+
> **NOTE:** Behavior differs between paths within `/tauri/` and other paths. Within Tauri, every file and folder modification is emitted as a distinct event. However, for other paths, the events are aggregated to the nearest discernible parent directory.
942+
> **Examples:**
943+
>
944+
> 1. **Within Tauri Paths:**
945+
> If you rename a parent directory named `parentDir` to `newDir` containing two files (`file1.txt` and `file2.txt`), you will receive six separate events:
946+
> - 2 Events for `UNLINK_DIR` `parentDir` and `ADD_DIR` `newDir`
947+
> - 2 Event for the `UNLINK_FILE` `parentDir/file1.txt` and `parentDir/file2.txt` due to its parent's renaming.
948+
> - 2 Event for the `ADD_FILE` `newDir/file1.txt` and `newDir/file2.txt` due to its parent's renaming.
949+
> 2. **Other Paths:**
950+
> Using the same scenario as above (renaming `parentDir` with two files inside), you will receive just two event(`UNLINK_DIR` and `ADD_DIR`) indicating the change in the `parentDir`. The individual changes to `file1.txt` and `file2.txt` are aggregated under the parent directory's event.
951+
>
952+
> This means developers working with Tauri paths should design their event handlers to accommodate individual events for each file and directory change.
953+
954+
955+
### Parameters
956+
957+
- **pathToWatch** (string): The path to watch for filesystem changes.
958+
959+
- **gitIgnorePaths** (string or Array<string>, optional, default=""): The patterns to ignore, either provided as a string (representing the content of a `.gitignore` file) or an array of individual patterns. The watcher will adhere to the standard `.gitignore` specification as detailed at [git-scm](https://git-scm.com/docs/gitignore). It's important to note that if a parent directory is excluded from watching, its child directories will also be excluded, regardless of any `un-ignore` patterns in git ignore file (e.g., `!node_modules/dont_ignore_dir`).
960+
961+
### Returns
962+
963+
- **EventEmitter**: The event emitter that will notify of filesystem changes.
964+
965+
### Example
966+
967+
```javascript
968+
// In the below watcher, we provide a gitignore formatted text to ignore 'node_modules' folder
969+
// See https://git-scm.com/docs/gitignore for details.
970+
const watcher = await fs.watchAsync('/path/to/watch', 'node_modules');
971+
972+
watcher.on(Constants.WATCH_EVENTS.ADD_FILE, (event) => {
973+
console.log(`File created: ${event.path}`);
974+
});
975+
976+
watcher.on(Constants.WATCH_EVENTS.UNLINK_DIR, (event) => {
977+
console.log(`Directory deleted: ${event.path}`);
978+
});
979+
```
980+
981+
982+
## `fs.unwatchAsync(eventEmitter)`
983+
984+
Stops watching for filesystem changes on a previously set path.
985+
986+
Once you've stopped watching using `unwatchAsync`, any further operations on the event emitter will throw an error. If you wish to start watching again, you will need to call `fs.watchAsync`.
987+
988+
- **Parameters**
989+
- `eventEmitter` - The event emitter returned by `fs.watchAsync` that you wish to stop watching.
990+
- **Throws**
991+
- Throws an error (`Errors.EINVAL`) if the watcher is already closed or if operations are attempted after closing.
992+
993+
**Example**:
994+
995+
```javascript
996+
const watcher = await fs.watchAsync('/path/to/watch', 'node_modules');
997+
998+
// Listen to an event.
999+
watcher.on(fs.WATCH_EVENTS.ADD_FILE, (event) => {
1000+
console.log(`File created: ${event.path}`);
1001+
});
1002+
1003+
// ... After some time, stop watching.
1004+
await unwatchAsync(watcher);
1005+
1006+
// Throws error since the watcher is closed.
1007+
watcher.on(fs.WATCH_EVENTS.ADD_FILE, (event) => {
1008+
console.log(`File created: ${event.path}`);
1009+
});
1010+
```

0 commit comments

Comments
 (0)