From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 4 Mar 2020 11:18:03 -0800 Subject: feat: add onclose to MessagePort This adds the 'onclose' event to MessagePort. This is [proposed](https://github.com/w3ctag/design-reviews/issues/269#issuecomment-407584290) in w3c and has been discussed for years without conclusion. I'd like to get this standardised, but in lieu of that, this makes MessagePort a whole bunch more useful! diff --git a/third_party/blink/renderer/core/messaging/message_port.cc b/third_party/blink/renderer/core/messaging/message_port.cc index 5d7780c48a74fc7d95aeb690c90ab394b1e01a83..15f5f0f6aa05d3b17adae87286c92df9cc26a712 100644 --- a/third_party/blink/renderer/core/messaging/message_port.cc +++ b/third_party/blink/renderer/core/messaging/message_port.cc @@ -195,6 +195,7 @@ void MessagePort::close() { Entangle(pipe.TakePort0(), nullptr); } closed_ = true; + DispatchEvent(*Event::Create(event_type_names::kClose)); } void MessagePort::Entangle(MessagePortDescriptor port_descriptor, diff --git a/third_party/blink/renderer/core/messaging/message_port.h b/third_party/blink/renderer/core/messaging/message_port.h index 3678c9887c1596f813cd9618b1e9728808659fa8..5fc9edb82bf983fee9084d89904e232ecfe66fd9 100644 --- a/third_party/blink/renderer/core/messaging/message_port.h +++ b/third_party/blink/renderer/core/messaging/message_port.h @@ -125,6 +125,13 @@ class CORE_EXPORT MessagePort : public EventTarget, return GetAttributeEventListener(event_type_names::kMessageerror); } + void setOnclose(EventListener* listener) { + SetAttributeEventListener(event_type_names::kClose, listener); + } + EventListener* onclose() { + return GetAttributeEventListener(event_type_names::kClose); + } + // A port starts out its life entangled, and remains entangled until it is // closed or is cloned. bool IsEntangled() const { return !closed_ && !IsNeutered(); } diff --git a/third_party/blink/renderer/core/messaging/message_port.idl b/third_party/blink/renderer/core/messaging/message_port.idl index 6fab27fcdf1c333739b6ffe88b3cc4eed3301ee4..3f1f181d9b8a66997136f870f55c97c08294b6eb 100644 --- a/third_party/blink/renderer/core/messaging/message_port.idl +++ b/third_party/blink/renderer/core/messaging/message_port.idl @@ -40,4 +40,5 @@ // event handlers attribute EventHandler onmessage; attribute EventHandler onmessageerror; + attribute EventHandler onclose; };