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

Add file and directory event listener hooks #136

Closed
Tracked by #501
bgins opened this issue Oct 13, 2020 · 5 comments
Closed
Tracked by #501

Add file and directory event listener hooks #136

bgins opened this issue Oct 13, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@bgins
Copy link
Member

bgins commented Oct 13, 2020

Summary

It would be nice to be able to add event listeners to files and directories to detect when they have changed. This would permit event-driven programming as an alternative to polling on the filesystem for changes.

Problem

To detect a change to a file or directory, we have to ask if a change has occurred.

Impact

Polling has its CPU and memory costs and opens the door for weird timing issues.

Solution

Add hooks that permit event-driven programming. That way we can do things when and only when a change occurs.

Detail

Is your feature request related to a problem? Please describe.

I have written a simple app where a user can add two numbers and see the result on another device. A user will see the result when they first open the app on the second device, but it would be nice if the result would also update when two new numbers are added.

The app could periodically check the WinFS file for new results, but updating when an event signals a change would be better.

Describe the solution you'd like

Add hooks for event listeners on files and directories.

Describe alternatives you've considered

Polling. A hook on the root of the filesystem has also been suggested (wnfs.publishHooks.push(hook)), but this calls for filtering for the desired file or directory.

@bmann
Copy link
Contributor

bmann commented Oct 14, 2020

Thanks @bgins! There’s a brief sketch in the whitepaper around pieces of this https://whitepaper.fission.codes/file-system/file-system-basics/journal

@icidasset icidasset added the enhancement New feature or request label Oct 15, 2020
@jeffgca
Copy link
Contributor

jeffgca commented Aug 11, 2022

@walkah I want to revisit this in the next sprint - it would be ideal if tooling such as a browser extension could listen to webnative events and react to them.

@jeffgca
Copy link
Contributor

jeffgca commented Aug 11, 2022

More notes from discussing this with @bgins -

If I am building an app and want to have my UI update based on state in webnative, i think I might actually have to poll properties currently to get more info on the login state, etc. My use case is I'm building a browser extension that will represent webnative internals in the browser devtools in order to help developers debug their use of webnative. It woudl be great to be able to get events from webnative when changes happen:

  • auth events - logout, etc
  • filesystem events, eg a file is added, deleted, etc
  • what is the ( ipfs ) status of a file, eg get an event when we add it locally, then again when it's been passed into js-ipfs and maybe later when we can confirm it is published and available via a gateway.

@bgins
Copy link
Member Author

bgins commented Aug 11, 2022

Adding a few notes as well, the events that I think would be useful from WNFS are around mutations:

  • Something changed in the filesystem (any mutation)
  • Something changed in a specific directory or a file was changed

One can imagine event emitters that subscribe to changes to the whole filesystem, a directory, or a file. Starting from an event emitter, it's easy for developers to build other abstractions, such as RxJS observables.

@icidasset
Copy link
Contributor

This has been implemented using the local-change file system event.

In the new file system class there will no longer be any File or Tree classes that are exposed (and no more get method). So it doesn't make sense to events directly to these. Instead you can currently do the following:

program.fileSystem.on("local-change", ({ path }) => {
  // Webnative will have better comparison functions in later releases
  if (Path.toPosix(path) === "private/file") { ... }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants