mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 09:09:43 +00:00
LibWeb: Make FileReader fire progress event
Fixes wpt/FileAPI/reading-data-section/filereader_events.any.html :)
This commit is contained in:
parent
c0102aa818
commit
7a92b47a35
Notes:
github-actions[bot]
2024-10-15 06:43:27 +00:00
Author: https://github.com/justus2510
Commit: 7a92b47a35
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1805
3 changed files with 44 additions and 1 deletions
2
Tests/LibWeb/Text/expected/FileAPI/filereader-events.txt
Normal file
2
Tests/LibWeb/Text/expected/FileAPI/filereader-events.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
PASS
|
||||||
|
PASS
|
27
Tests/LibWeb/Text/input/FileAPI/filereader-events.html
Normal file
27
Tests/LibWeb/Text/input/FileAPI/filereader-events.html
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<script src="../include.js"></script>
|
||||||
|
<script>
|
||||||
|
asyncTest(async (done) => {
|
||||||
|
let testsDone = 0;
|
||||||
|
|
||||||
|
const eventTestDone = (events, expect) => {
|
||||||
|
println(events === expect ? "PASS" : "FAIL");
|
||||||
|
if (++testsDone == 2) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const runEventTest = (blob, expect) => {
|
||||||
|
let reader = new FileReader();
|
||||||
|
let events = "";
|
||||||
|
reader.onloadstart = () => events += "loadstart";
|
||||||
|
reader.onprogress = () => events += ", progress";
|
||||||
|
reader.onload = () => events += ", load";
|
||||||
|
reader.onloadend = () => { events += ", loadend"; eventTestDone(events, expect); };
|
||||||
|
reader.readAsText(blob);
|
||||||
|
};
|
||||||
|
|
||||||
|
runEventTest(new Blob(["someRandomData"]), "loadstart, progress, load, loadend");
|
||||||
|
runEventTest(new Blob([]), "loadstart, load, loadend");
|
||||||
|
});
|
||||||
|
</script>
|
|
@ -7,6 +7,7 @@
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/Base64.h>
|
#include <AK/Base64.h>
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/Time.h>
|
||||||
#include <LibJS/Heap/Heap.h>
|
#include <LibJS/Heap/Heap.h>
|
||||||
#include <LibJS/Runtime/Promise.h>
|
#include <LibJS/Runtime/Promise.h>
|
||||||
#include <LibJS/Runtime/Realm.h>
|
#include <LibJS/Runtime/Realm.h>
|
||||||
|
@ -147,6 +148,8 @@ WebIDL::ExceptionOr<void> FileReader::read_operation(Blob& blob, Type type, Opti
|
||||||
// 10. In parallel, while true:
|
// 10. In parallel, while true:
|
||||||
Platform::EventLoopPlugin::the().deferred_invoke([this, chunk_promise, reader, bytes, is_first_chunk, &realm, type, encoding_name, blobs_type]() mutable {
|
Platform::EventLoopPlugin::the().deferred_invoke([this, chunk_promise, reader, bytes, is_first_chunk, &realm, type, encoding_name, blobs_type]() mutable {
|
||||||
HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
|
HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
|
||||||
|
Optional<MonotonicTime> progress_timer;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto& vm = realm.vm();
|
auto& vm = realm.vm();
|
||||||
|
|
||||||
|
@ -180,7 +183,18 @@ WebIDL::ExceptionOr<void> FileReader::read_operation(Blob& blob, Type type, Opti
|
||||||
// 2. Append bs to bytes.
|
// 2. Append bs to bytes.
|
||||||
bytes.append(byte_sequence.data());
|
bytes.append(byte_sequence.data());
|
||||||
|
|
||||||
// FIXME: 3. If roughly 50ms have passed since these steps were last invoked, queue a task to fire a progress event called progress at fr.
|
// 3. If roughly 50ms have passed since these steps were last invoked, queue a task to fire a progress event called progress at fr.
|
||||||
|
auto now = MonotonicTime::now();
|
||||||
|
bool enough_time_passed = !progress_timer.has_value() || (now - progress_timer.value() >= AK::Duration::from_milliseconds(50));
|
||||||
|
// WPT tests for this and expects no progress event to fire when there isn't any data.
|
||||||
|
// See http://wpt.live/FileAPI/reading-data-section/filereader_events.any.html
|
||||||
|
bool contained_data = byte_sequence.array_length().length() > 0;
|
||||||
|
if (enough_time_passed && contained_data) {
|
||||||
|
HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [this, &realm]() {
|
||||||
|
dispatch_event(DOM::Event::create(realm, HTML::EventNames::progress));
|
||||||
|
}));
|
||||||
|
progress_timer = now;
|
||||||
|
}
|
||||||
|
|
||||||
// 4. Set chunkPromise to the result of reading a chunk from stream with reader.
|
// 4. Set chunkPromise to the result of reading a chunk from stream with reader.
|
||||||
chunk_promise = reader->read();
|
chunk_promise = reader->read();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue