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 a Random Values Generator Demonstration #844

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

abdulhannanali
Copy link

@abdulhannanali abdulhannanali commented Oct 17, 2017

Adding a random values readable stream that generates a finites amount of random values using WebCrypto's getRandomValues method. This is a small demo, any suggestions to improve this demo are welcomed.

Here's a link to the demo's html page https://rawgit.com/abdulhannanali/streams/master/demos/random-values-stream/index.html

@abdulhannanali
Copy link
Author

abdulhannanali commented Oct 17, 2017

I am also working on another demo right now which is going to transform text into emojis, and would want to give a demos section a rehaul arranging the demos in a more friendlier, extensible and helpful format.

* Starting the random values generation again after a certain period
* @param {*} controller
*/
async pull(controller) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessarily complicated. You don't need to track desiredSize yourself. As long as you have called enqueue() you can rely on the ReadableStream to call you back when there is no backpressure.

It would be simpler to do something like

pull(controller) {
  if (maxValues && this.totalEnqueuedItemsCount >= maxValues) {
    controller.close();
    return;
  }
  let resolve;
  const promise = new Promise(r => {
    resolve = r;
  });
  setTimeout(() => {
    controller.enqueue(randomValuesUint8Array(20));
    ++this.totalEnqueuedItemsCount;
    resolve();
  }, valueInterval);
  return promise;
}

(untested).

* Writes a chunk to the span and appends it to the parent element
* @param {*} chunk
*/
async function writeChunk(chunk) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't need to be async as it performs no asynchronous operations. Also, as it is only called within write(), it might as well just be included in that function.

try {
await writeChunk(chunk);
return;
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary to catch the exception. If write() throws, the stream will errored automatically.

@@ -8,14 +8,17 @@
<dl>
<dt><a href="append-child.html">Append child WritableStream</a>
<dd>Piping through a series of transforms to a custom writable stream that appends children to
the DOM.
the DOM.</dd>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end tag is not needed, see https://html.spec.whatwg.org/multipage/grouping-content.html#the-dd-element.

I find it tidier to leave it out.

Base automatically changed from master to main January 15, 2021 07:25
@abdulhannanali
Copy link
Author

abdulhannanali commented May 1, 2021

Hi @ricea

Thank you for reviewing this PR and sorry for not making the changes earlier. I'll make the changes now, I should also have that emoji stream converter on Github somewhere. I'll make a PR with that too.

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

Successfully merging this pull request may close these issues.

2 participants