oddsdk / ts-odd

An SDK for building apps with decentralized identity and storage.
https://odd.dev/
Apache License 2.0
178 stars 24 forks source link

Add file and directory event listener hooks #136

Closed bgins closed 1 year ago

bgins commented 4 years ago

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 commented 4 years ago

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

jeffgca commented 2 years ago

@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 commented 2 years ago

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:

bgins commented 2 years ago

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

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 commented 1 year ago

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") { ... }
})