Skip to content

Commit aeeea42

Browse files
DOM: Observable EventTarget integration 1/N
This CL implements "limited" and "leaky" EventTarget integration with the Observable API. See below for what "limited" and "leaky" mean. Concretely, this involves introducing the `on()` method to the EventTarget interface, so that all EventTargets can return Observables that listen for events. This is the part that really makes Observables a "better addEventListener()". This is the first instance of a natively-constructed Observable, as opposed to a JS-constructed Observable. This means the subscription callback passed to the Observable constructor is not just a JS callback function with user-defined code, but instead is a C++ delegate class, called `SubscribeDelegate` which has its first concrete implementation provided by EventTarget (in event_target.cc). The concrete implementation of this interface that this CL introduces, adds an event listener to the given EventTarget, upon subscription. The events are forwarded to the Subscriber's `next()` method. This is what unlocks more ergonomic event handling with the composable Observable primitive and all of its (coming) operators. 1. The EventTarget integration is considered "limited" because we do not support any of the `AddEventListenerOptions` yet, as of this CL. A subsequent CL will add support for a more restricted version of the `AddEventListenerOptions`, called `ObservableEventListenerOptions`, which does not include a `once` option, or an `AbortSignal`, since Observable operators and subscription is responsible for managing those aspects. Concretely, an `ObservableEventListenerOptions` will resolve to an `AddEventListenerOptionsResolved` accordingly. See: - WICG/observable#66 - WICG/observable#67 - WICG/observable#65 2. The EventTarget integration is considered "leaky" as of this CL, because there is currently no way to remove an event listener added by an EventTarget-vended Observable. This will come in a subsequent CL, which will pass the test that is currently failing in this CL. See WICG/observable#75 for discussion about tying the subscription termination to removing an event listener. From a technical perspective, this is pretty easy — it involves adding an abort algorithm to `Subscriber#signal` (which has already been wired up properly by now!) that removes the given per-Subscription `ObservableEventListener` NativeEventListener from the associated EventTarget. That implementation has already been sketched out in https://crrev.com/c/4262153 and the design doc. It will included in a follow-up CL, to reduce the complexity of this one. For WPTs: Co-authored-by: [email protected] [email protected] Bug: 1485981 Change-Id: Iafeddb0894b8eed2be1d95c181fc44d7650c0d47 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5073394 Reviewed-by: Mason Freed <[email protected]> Commit-Queue: Dominic Farolino <[email protected]> Cr-Commit-Position: refs/heads/main@{#1237501}
1 parent d18aca5 commit aeeea42

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

dom/observable/tentative/observable-constructor.window.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,30 @@ promise_test(async t => {
9898

9999
assert_array_equals(results, ["detached"], "Subscribe callback is never invoked");
100100
}, "Cannot subscribe to an Observable in a detached document");
101+
102+
promise_test(async t => {
103+
// Make this available off the global so the child can reach it.
104+
window.results = [];
105+
const contentWin = await loadIframeAndReturnContentWindow();
106+
107+
contentWin.eval(`
108+
const parentResults = parent.results;
109+
const event_target = new EventTarget();
110+
// Set up two event listeners, both of which will mutate |parentResults|:
111+
// 1. A traditional event listener
112+
// 2. An observable
113+
event_target.addEventListener('customevent', e => parentResults.push(e));
114+
const source = event_target.on('customevent');
115+
source.subscribe(e => parentResults.push(e));
116+
117+
// Detach the iframe and fire an event at the event target. The parent will
118+
// confirm that the observable's next handler did not get invoked, because
119+
// the window is detached.
120+
const event = new Event('customevent');
121+
window.frameElement.remove();
122+
parentResults.push('detached');
123+
event_target.dispatchEvent(event);
124+
`);
125+
126+
assert_array_equals(results, ["detached"], "Subscribe callback is never invoked");
127+
}, "Observable from EventTarget does not get notified for events in detached documents");
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
test(() => {
2+
const target = new EventTarget();
3+
assert_implements(target.on, "The EventTarget interface has an `on` method");
4+
assert_equals(typeof target.on, "function",
5+
"EventTarget should have the on method");
6+
7+
const testEvents = target.on("test");
8+
assert_true(testEvents instanceof Observable,
9+
"EventTarget.on returns an Observable");
10+
11+
const results = [];
12+
testEvents.subscribe({
13+
next: value => results.push(value),
14+
error: () => results.push("error"),
15+
complete: () => results.push("complete"),
16+
});
17+
18+
assert_array_equals(results, [],
19+
"Observable does not emit events until event is fired");
20+
21+
const event = new Event("test");
22+
target.dispatchEvent(event);
23+
assert_array_equals(results, [event]);
24+
25+
target.dispatchEvent(event);
26+
assert_array_equals(results, [event, event]);
27+
}, "EventTarget.on() returns an Observable");
28+
29+
test(() => {
30+
const target = new EventTarget();
31+
const testEvents = target.on("test");
32+
const ac = new AbortController();
33+
const results = [];
34+
testEvents.subscribe({
35+
next: (value) => results.push(value),
36+
error: () => results.push('error'),
37+
complete: () => results.complete('complete'),
38+
}, { signal: ac.signal });
39+
40+
assert_array_equals(results, [],
41+
"Observable does not emit events until event is fired");
42+
43+
const event1 = new Event("test");
44+
const event2 = new Event("test");
45+
const event3 = new Event("test");
46+
target.dispatchEvent(event1);
47+
target.dispatchEvent(event2);
48+
49+
assert_array_equals(results, [event1, event2]);
50+
51+
ac.abort();
52+
target.dispatchEvent(event3);
53+
54+
assert_array_equals(results, [event1, event2],
55+
"Aborting the subscription removes the event listener and stops the " +
56+
"emission of events");
57+
}, "Aborting the subscription should stop the emission of events");
58+
59+
test(() => {
60+
const target = new EventTarget();
61+
const testEvents = target.on("test");
62+
const results = [];
63+
testEvents.subscribe(e => results.push(e));
64+
testEvents.subscribe(e => results.push(e));
65+
66+
const event1 = new Event("test");
67+
const event2 = new Event("test");
68+
target.dispatchEvent(event1);
69+
target.dispatchEvent(event2);
70+
assert_array_equals(results, [event1, event1, event2, event2]);
71+
}, "EventTarget Observables can multicast subscriptions for event handling");

trusted-types/trusted-types-event-handlers.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
// element about which attributes it knows.
3838
const div = document.createElement("div");
3939
for(name in div.__proto__) {
40-
const should_be_event_handler = name.startsWith("on");
40+
// This captures all "on{foo}" handlers, but not "on" itself, which is an IDL
41+
// attribute that returns an Observable.
42+
const should_be_event_handler = name.startsWith("on") && name !== "on";
4143
if (should_be_event_handler) {
4244
test(t => {
4345
assert_throws_js(TypeError,

0 commit comments

Comments
 (0)