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

addEventListener support for Reader #5

Open
drzraf opened this issue May 28, 2020 · 1 comment
Open

addEventListener support for Reader #5

drzraf opened this issue May 28, 2020 · 1 comment

Comments

@drzraf
Copy link

drzraf commented May 28, 2020

Couldn't the reader returned by getReader() implement the FileReader interface or is there another way to "track" reader progress outside a while loop?

var { message } = await openpgp.encrypt({
        message: openpgp.message.fromBinary((new File([], "foo.txt")).stream()),
        publicKeys: [KEYS],
        armor: false
    }),
_reader = openpgp.stream.getReader(message.packets.write());
_reader.addEventListener('progress', event => console.log(`${event.type}: ${event.loaded} bytes transferred`));
@twiss
Copy link
Member

twiss commented May 28, 2020

There's no easy way, but.. note that if you put the while loop in an async IIFE:

(async () => {
  for (let chunk; chunk = await reader.readBytes(1 << 20);) console.log(chunk);
  console.log('finished');
})();

then that's pretty similar to having two events, and you're not blocking the outer function's control flow.

If you truly don't want a while loop, you could use stream.transform:

openpgp.stream.transform(message.packets.write(), chunk => {
  console.log(chunk);
}, () => {
  console.log('finished');
});

but it's not really meant for this, and it will create an extra TransformStream object which you don't need.

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

No branches or pull requests

2 participants