RangerMauve / hyper-sdk

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

Not updating archive? #58

Closed radwouters closed 4 years ago

radwouters commented 4 years ago

Hey guys,

I have some problems with adding files to an archive. I use chokidar to watch a folder as follows:

const watcher = chokidar.watch(filePath, {
    ignored: /(^|[\/\\])\../, // ignore dotfiles
    persistent: true,
    ignoreInitial: true,
});

watcher.on('add', async (addedPath: string) => {
    fs.readFile(filePath, async (err, data) => {
        await archive.writeFile(path.basename(addedPath), data);
        console.log('Wrote ${path.basename(addedPath)}, ${addedPath} to the archive');
        console.log(`Current archive contents: ${await archive.readdir('/')}`);
    });
});

Which works as expected: It adds the file to the archive and lists it in archive.readdir('/').

However, when using the following code to watch for updates, it does not register any updates:

const { Hyperdrive } = sdk;
const archive = Hyperdrive(key, {
    sparse: false,
    sparseMetadata: false,
});
// This registers the initial content of the archive correctly
let files = await archive.readdir('/');
// Just updates do not work
archive.on('update', async () => {
      console.log('archive updated');
});

This is wrapped in a async click listener in a react electron app.

Am I missing something? Is it the async method?

RangerMauve commented 4 years ago

Hey, I think there might be an issue with the update event in hyperdrive right now where the update event isn't firing. https://github.com/hypercore-protocol/hyperdrive/issues/284

I've been temporarily working around it by listening to archive.metadata.on('remote-update', () => {}) which fires sometimes when there aren't updates too, but is better than nothing. 😅

RangerMauve commented 4 years ago

Hey, just figured it out thanks to Mafintosh.

Instead of using the update event, you should use archive.watch('/', () => {}) to watch for changes. 😁