RangerMauve / hyper-sdk

Make your own hyper apps!
https://www.youtube.com/watch?v=HyHk4aImd_I&list=PL7sG5SCUNyeYx8wnfMOUpsh7rM_g0w_cu&index=20
MIT License
290 stars 46 forks source link

archive.watch() picks up changes irregularly #52

Closed karlmoubarak closed 4 years ago

karlmoubarak commented 4 years ago

Hey, I'm working on a web implementation of the sdk(promise) that imports an existing dat archive, reads its contents, renders some of its files, and orders them reverse-chronologically, in a 'blog' style. I am just an amateur trying to 'do It myself', so i hope my issue is a simple one.

goal: having multiple 'blogs' served from different devices to a web-client that awaits changes and updates live.

I tried to implement archive.watch() exactly as it is documented in the README. It partly works. Deleting existing files is always picked up, and creating files in terminal within the directory with touch is also always picked up. But it doesn't seem to pick up files being manually pasted into the folder or changes made to files with any program (although the dat-cli is logging those changes). To add to this, when i used archive.download(), (it downloads the archive and) the 'changed' event is fired for all the changes that were previously not picked up.

I saw that this is a recurring issue with fs.watch() as well, and there are some workarounds (chokidar?). Dat-node also has a great .on('put') and .on('del'), but it's not meant for web :(

Where exactly is the problem? Would there be a simple solution for this?

(btw thanks for all the great work!)

karlmoubarak commented 4 years ago

beaker API documentation helped fix this.

RangerMauve commented 4 years ago

Sweet! Glad you could figure it out. Do you think you'd be down to post a snippet of code with what worked for you for others having this issue in the future?

karlmoubarak commented 4 years ago

Sure! I had this going on:

const events = archive.watch(['**/*']) events.addEventListener('changed', ({path}) => { // doing some stuff })

and turns out that "changed" event will only fire for downloaded files that are ready to read. My files were not downloaded, so it wasn't working except when creating an empty file (eg. touch test.txt). So I changed it to this:

const events = archive.watch(['**/*']) events.addEventListener('invalidated', ({path}) => { // doing some stuff })

Which does the same but the files don't need to be downloaded. The event is fired on every edit.

From what I can understand, I think 'invalidated' refers to the history/log, and 'changed' refers to the content of downloaded files.

Again, I am not experienced, so the sdk documentation is not super easy for me to follow. I was glad to find that the beaker DatArchive API has a more extensive documentation of the same functions, which helps a lot for a rookie. I'd been looking for it for weeks!

x