Git Product home page Git Product logo

Comments (7)

MattiasBuelens avatar MattiasBuelens commented on May 29, 2024 1

Yeah, but those are still quite different from the specced behavior.

I would recommend something like this:

ReadableStream.prototype.values ??= function({ preventCancel = false } = {}) {
    const reader = this.getReader();
    return {
        async next() {
            try {
                const result = await reader.read();
                if (result.done) {
                    reader.releaseLock();
                }
                return result;
            } catch (e) {
                reader.releaseLock();
                throw e;
            }
        },
        async return(value) {
            if (!preventCancel) {
                const cancelPromise = reader.cancel(value);
                reader.releaseLock();
                await cancelPromise;
            } else {
                reader.releaseLock();
            }
            return { done: true, value };
        },
        [Symbol.asyncIterator]() {
            return this;
        }
    };
};

ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;

Now also as a Gist! 😁

from web-streams-polyfill.

jimmywarting avatar jimmywarting commented on May 29, 2024 1

Thanks yet again for recreating the recommendations.

from web-streams-polyfill.

jimmywarting avatar jimmywarting commented on May 29, 2024 1

You know... with this in place combined with a polyfill for ReadableStream.from(iterable) then this would make it a grate solution for converting your polyfilled ReadableStream instance into a native ReadableStream instance that could be transfered with postMessage.

import { ReadableStream as Polyfill } from 'pkg'

var native = globalThis.ReadableStream.from(new Polyfill(...))
// and it would work the same the other way around
var polyfill = Polyfill.from(new globalThis.ReadableStream(...))

it would make the web-streams-adapter a bit obsolete.

kind of solves the conversion between node:streams to web streams as well when having a polyfill for .from() as well.

Perhaps dose not cover all the bits and pieces such as sink and BYOB reader and other stuff (?). But could get the job done for most stuff.

from web-streams-polyfill.

MattiasBuelens avatar MattiasBuelens commented on May 29, 2024

I should really have put that in a Gist...

Did you manage to find it again? I can't seem to find it myself. 😅

from web-streams-polyfill.

jimmywarting avatar jimmywarting commented on May 29, 2024

I should really have put that in a Gist...

dito

Did you manage to find it again?

nope, maybe i have to rewrite it. but i don't remember what your suggestions where to make it more spec compatible.

All i can say is that it started with ReadableStream.prototype[Symbol.asyncIterator] ??= 😅

Gona try searching my computer for it.

from web-streams-polyfill.

jimmywarting avatar jimmywarting commented on May 29, 2024

found this in one readme file.

import 'fast-readable-async-iterator'

// or

if (typeof ReadableStream !== 'undefined' && !ReadableStream.prototype[Symbol.asyncIterator]) {
  ReadableStream.prototype[Symbol.asyncIterator] = function () {
    const reader = this.getReader()
    let last = reader.read()
    return {
      next () {
        const temp = last
        last = reader.read()
        return temp
      },
      return () {
        return reader.releaseLock()
      },
      throw (err) {
        this.return()
        throw err
      },
      [Symbol.asyncIterator] () {
        return this
      }
    }
  }
}

And also this one:

ReadableStream.prototype[Symbol.asyncIterator] ??= function () {
  const reader = this.getReader()
  return {
    next: _ => reader.read(),
    return: _ => { reader.releaseLock() },
    throw: err => {
      this.return()
      throw err
    },
    [Symbol.asyncIterator] () {
      return this
    }
  }
}

from web-streams-polyfill.

MattiasBuelens avatar MattiasBuelens commented on May 29, 2024

Yes, that was deliberate, see whatwg/streams#1083 (comment). 😁

I really need to find some time to work on web-streams-polyfill again and add ReadableStream.from(). Converting from a native ReadableStream or a Node.js Readable has always been painful with the polyfill (even when you use web-streams-adapter), and from() would help a lot.

from web-streams-polyfill.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.