Git Product home page Git Product logo

Comments (10)

saschanaz avatar saschanaz commented on July 4, 2024 1

Does the browser call this callback?

Yes. stream.getReader().read() (or C++ equivalent of it) will call the callback. You can take look at Chromium Fetch implementation to see how it works there.

How does the web author generate these bytes?

If the data is string then one can use TextEncoderStream to do the work:

let stream = new ReadableStream({
  pull(controller) {
    controller.enqueue("hello");
  }
}, { highWaterMark: 0 });
let encodedStream = stream.pipeThrough(new TextEncoderStream()); // encodes each data chunk on demand
let reader = encodedStream.getReader();
console.log(await reader.read()); // { done: false, value: Uint8Array([104, 101, 108, 108, 111])

If the data is binary, then you already have it.

from clipboard-apis.

saschanaz avatar saschanaz commented on July 4, 2024

I wonder what the expected use cases look like. If most of the use cases for delayed write includes a time-consuming computation, it would be best for them to use stream to not block the main thread (unless everything happens within a worker) and reduce the delay until the first write.

(It could also be a mitigation for privacy problem for custom formats if the stream consumption immediately starts but with artificial intervals, so that the triggering page have a hard time detecting when the actual paste happens with which format.)

from clipboard-apis.

snianu avatar snianu commented on July 4, 2024

If most of the use cases for delayed write includes a time-consuming computation, it would be best for them to use stream to not block the main thread

Even for formats that are not delayed rendered, this would be a useful addition to the async clipboard API. It would be nice not to force web authors to use delayed clipboard rendering if they want to take advantage of this memory optimization for read/write.

(It could also be a mitigation for privacy problem for custom formats if the stream consumption immediately starts but with artificial intervals, so that the triggering page have a hard time detecting when the actual paste happens with which format.)

Like it was mentioned in this comment, this defeats the purpose of delay rendering the expensive formats as the web authors are forced to trigger the callbacks. We resolved the privacy issue to only support delayed rendering for built-in formats for now, but we will explore options to extend this support to web custom formats with the privacy mitigation in-place in the future.

from clipboard-apis.

saschanaz avatar saschanaz commented on July 4, 2024

Even for formats that are not delayed rendered, this would be a useful addition to the async clipboard API.

๐Ÿ‘

It would be nice not to force web authors to use delayed clipboard rendering if they want to take advantage of this memory optimization for read/write.

My understanding was that delayed rendering won't need special flag, was it? Not sure what you mean by forcing. Authors would just pass readable stream and consuming that would solely depend on user agent implementation, right?

Like it was mentioned in this comment, this defeats the purpose of delay rendering the expensive formats as the web authors are forced to trigger the callbacks. We resolved the privacy issue to only support delayed rendering for built-in formats for now, but we will explore options to extend this support to web custom formats with the privacy mitigation in-place in the future.

Stream sources can produce small chunk for each pull rather than big chunk at once, so I think it doesn't defeat the point as each pull shouldn't take long. ๐Ÿ‘ for doing builtin formats so that we can keep discussing, though!

from clipboard-apis.

snianu avatar snianu commented on July 4, 2024

My understanding was that delayed rendering won't need special flag, was it?

I guess it depends on how we implement the stream support, but if a callback is supplied in the ClipboardItem instead of a Promise to Blob/DOMString, then it will be treated as a delayed rendered format. You could extend the callback to use stream instead, but I think it will benefit the web authors even without this callback.

from clipboard-apis.

saschanaz avatar saschanaz commented on July 4, 2024

Streams wouldn't need callback even with delayed rendering, user agent in that case would just delay consuming it. Requiring steam to be inside callback would be weird as stream itself uses callback already.

from clipboard-apis.

snianu avatar snianu commented on July 4, 2024

Streams wouldn't need callback even with delayed rendering, user agent in that case would just delay consuming it

I'm not sure if I understand this correctly. How would you distinguish between a normal clipboard write operation and delay rendering (not delayed write)? Delayed rendering is tied to the system clipboard. If the system clipboard doesn't call back into the source app because the delay rendered format was never used during paste, then the callback is never triggered. If I understand the stream proposal correctly, then the web authors have to always be prepared with the data to stream it right?

from clipboard-apis.

saschanaz avatar saschanaz commented on July 4, 2024

If I understand the stream proposal correctly, then the web authors have to always be prepared with the data to stream it right?

A proper stream source would generate data on-demand (pull callback will be called when needed). Technically it can prepare before pull callback via start callback but that's not required and not ideal for a large size data.

from clipboard-apis.

snianu avatar snianu commented on July 4, 2024

pull callback will be called when needed

Does the browser call this callback? If not, then how does the browser indicate to the web authors that they need to return data for the format being requested by the clipboard?

I think the confusing thing for me is the below snippet in the example code:
controller.enqueue(bytes);

How does the web author generate these bytes?

from clipboard-apis.

snianu avatar snianu commented on July 4, 2024

Adding few people who would be interested in this feature @sanketj @evanstade @inexorabletash @whsieh @annevk

from clipboard-apis.

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.