From d06a3e781e11f5efb8d17f8c9e6fc61f8fc4e597 Mon Sep 17 00:00:00 2001 From: Elizabeth Mitchell Date: Tue, 19 Dec 2023 16:27:52 -0800 Subject: [PATCH] chore: add event hooks for default prevention behavior PiperOrigin-RevId: 592375327 --- internal/events/dispatch-hooks.ts | 176 ++++++++++++++++++++++++ internal/events/dispatch-hooks_test.ts | 180 +++++++++++++++++++++++++ 2 files changed, 356 insertions(+) create mode 100644 internal/events/dispatch-hooks.ts create mode 100644 internal/events/dispatch-hooks_test.ts diff --git a/internal/events/dispatch-hooks.ts b/internal/events/dispatch-hooks.ts new file mode 100644 index 000000000..e67f491ff --- /dev/null +++ b/internal/events/dispatch-hooks.ts @@ -0,0 +1,176 @@ +/** + * @license + * Copyright 2023 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * A symbol used to access dispatch hooks on an event. + */ +const dispatchHooks = Symbol('dispatchHooks'); + +/** + * An `Event` with additional symbols for dispatch hooks. + */ +interface EventWithDispatchHooks extends Event { + [dispatchHooks]: EventTarget; +} + +/** + * Add a hook for an event that is called after the event is dispatched and + * propagates to other event listeners. + * + * This is useful for behaviors that need to check if an event is canceled. + * + * The callback is invoked synchronously, which allows for better integration + * with synchronous platform APIs (like `
` or `