From 3d84c988cd8325b8c0db91a9dccfa33f07cd3dc4 Mon Sep 17 00:00:00 2001 From: iteriani Date: Thu, 18 Apr 2024 15:26:45 -0700 Subject: [PATCH] refactor(core): Make some minor changes to facilitate testing and usage of event-dispatch. (#55411) The change in the index is to allow the framework to add a type to the object that is passed from the contract to the dispatcher. ie registerDispatcher(contract, (eventInfo: EventInfoWrapper) => ...). The latter is to return the event contract so that tests can clean it up. PR Close #55411 --- packages/core/primitives/event-dispatch/index.ts | 1 + .../event-dispatch/src/register_events.ts | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/core/primitives/event-dispatch/index.ts b/packages/core/primitives/event-dispatch/index.ts index 4e4d53d27fd07..1cb0e0b460c8b 100644 --- a/packages/core/primitives/event-dispatch/index.ts +++ b/packages/core/primitives/event-dispatch/index.ts @@ -10,3 +10,4 @@ export {Dispatcher, registerDispatcher} from './src/dispatcher'; export {EventContractContainer} from './src/event_contract_container'; export {EventContract} from './src/eventcontract'; export {bootstrapEventContract} from './src/register_events'; +export {EventInfoWrapper} from './src/event_info'; diff --git a/packages/core/primitives/event-dispatch/src/register_events.ts b/packages/core/primitives/event-dispatch/src/register_events.ts index c1fe1ba63f8ee..b0b90391c327b 100644 --- a/packages/core/primitives/event-dispatch/src/register_events.ts +++ b/packages/core/primitives/event-dispatch/src/register_events.ts @@ -16,26 +16,24 @@ import {EventContract} from './eventcontract'; * @param container The container that listens to events * @param appId A given identifier for an application. If there are multiple apps on the page * then this is how contracts can be initialized for each one. + * @param events An array of event names that should be listened to. + * @param anyWindow The global window object that should receive the event contract. + * @returns The `event` contract. This is both assigned to `anyWindow` and returned for testing. */ export function bootstrapEventContract( field: string, container: Element, appId: string, events: string[], + anyWindow: any = window, ) { - const contractContainer = new EventContractContainer(container); - // tslint:disable-next-line:no-any - const anyWindow = window as any; if (!anyWindow[field]) { anyWindow[field] = {}; } - const eventContract = new EventContract(contractContainer, /* stopPropagation */ false); + const eventContract = new EventContract(new EventContractContainer(container)); anyWindow[field][appId] = eventContract; for (const ev of events) { eventContract.addEvent(ev); } -} - -export function cleanup() { - (globalThis as any).__ngEventContracts__ = undefined; + return eventContract; }