Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce 'fs.watch' and 'fs.Watcher' #736

Merged
merged 3 commits into from
Oct 30, 2023
Merged

Introduce 'fs.watch' and 'fs.Watcher' #736

merged 3 commits into from
Oct 30, 2023

Conversation

jwerle
Copy link
Member

@jwerle jwerle commented Oct 30, 2023

This PR introduces the fs.watch() and fs.Watcher APIs. It is implemented to give as much parity as possible with node's implementation.

Examples

EventEmitter:

import process from 'socket:process'
import fs from 'socket:fs'

// watch resources directory
const watcher  = fs.watch(process.cwd())

watcher.on('change', (eventType, filename) => {
  console.log(eventType) // 'change' or 'rename'
  console.log(filename) // file name relative to `process.cwd()`
})

AsyncIterator (with filename encoding):

import process from 'socket:process'
import fs from 'socket:fs/promises'

// output `filename` as `Buffer` instance
const events = fs.watch(process.cwd(), { encoding: 'buffer' })

for await (const event of events) {
  console.log(event) // { eventType: 'change' | 'rename' , filename: <Buffer ...> }
}

AbortSignal

import process from 'socket:process'
import fs from 'socket:fs/promises'

const controller = new AbortController()
// output `filename` as `Buffer` instance
const events = fs.watch(process.cwd(), { signal: controller.signal })

// watch files for 5000 ms
setTimeout(() => controller.abort(), 5000)

for await (const event of events) {
  console.log(event) // { eventType: 'change' | 'rename' , filename: 'index.js' }
}

Closes #595

@jwerle jwerle added enhancement New feature or request javascript An issue, discussion, or task related to the runtime javascript api An issue, task, or discussion related to public runtime APIs labels Oct 30, 2023
@jwerle jwerle self-assigned this Oct 30, 2023
api/fs/index.js Outdated Show resolved Hide resolved
api/fs/dir.js Outdated Show resolved Hide resolved
* @ignore
* @type {string}
*/
id = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random TS question:

If you don't set this to null in the instance value field, and you guarantee set them in the constructor, typescript will let you type it as never null and just the value its typed as. Or maybe this is a strict null check option, I'm not sure. Typescript is a million variants of itself depending on how you configure it.

What about not setting this to null initially, or if there is a reason, why do we set this to null here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just by habit
@chicoxyzzy what do you think we should set it to here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not setting null here (unless I'm mistaken), and also setting in the constructor narrows the fields slightly to be a single type instead of also possibly null.

Copy link
Contributor

@bcomnes bcomnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@jwerle jwerle merged commit f006877 into master Oct 30, 2023
2 checks passed
@jwerle jwerle deleted the fs-watcher branch October 30, 2023 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api An issue, task, or discussion related to public runtime APIs enhancement New feature or request javascript An issue, discussion, or task related to the runtime javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'fs.watch()' API for Desktop
4 participants