WICG Observableの話
tc39/proposal-observable
かつてECMAScriptにObservableを入れようとした
宣言的にイベントストリームを扱うための汎用データ構造
code:js
function listen(element, eventName) {
return new Observable(observer => {
// Create an event handler which sends data to the sink
let handler = event => observer.next(event);
// Attach the event handler
element.addEventListener(eventName, handler, true);
// Return a cleanup function which will cancel the event stream
return () => {
// Detach the event handler from the element
element.removeEventListener(eventName, handler, true);
};
});
}
const subscription = listen(element, "keydown")
.filter(event => event.keyCode in keyCommands)
.subscribe(command => { /*...*/ });
subscription.unsubscribe();
賛同が得られず終了
wicg/observable
ブラウザ(DOM)APIとしてObservableを導入しようという提案
EventTarget.when() メソッド
イベントをストリーム処理しやすくする
code:js
element
.when('click')
.filter((e) => e.target.matches('.foo'))
.map((e) => ({ x: e.clientX, y: e.clientY }))
.subscribe({ next: handleClickAtPoint });
経緯
Observables were first proposed to the platform in TC39 in May of 2015. The proposal failed to gain traction, in part due to some opposition that the API was suitable to be a language-level primitive. In an attempt to renew the proposal at a higher level of abstraction, a WHATWG DOM issue was filed in December of 2017. Despite ample developer demand, lots of discussion, and no strong objectors, the DOM Observables proposal sat mostly still for several years (with some flux in the API design) due to a lack of implementer prioritization.
Later in 2019, an attempt at reviving the proposal was made back at the original TC39 repository, which involved some API simplifications and added support for the synchronous "firehose" problem.
This repository is an attempt to again breathe life into the Observable proposal with the hope of shipping a version of it to the Web Platform.
既存のデータモデルとの関係
https://scrapbox.io/files/67568937482a60fb845d88cd.png
標準化に向けて
いったんはWHATWGが主導してDOM APIへの採用を目指す
ECMAScriptでの採用はその先の目標
現状
Chrome: In development
Firefox: No signal
Safari: No signal
https://www.youtube.com/watch?v=IMIVmCkwOT4