Skip to content

Commit

Permalink
refactor(core): Make some minor changes to facilitate testing and usa…
Browse files Browse the repository at this point in the history
…ge of event-dispatch. (angular#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 angular#55411
  • Loading branch information
iteriani authored and alxhub committed Apr 23, 2024
1 parent f84d7dd commit 3d84c98
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/primitives/event-dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
14 changes: 6 additions & 8 deletions packages/core/primitives/event-dispatch/src/register_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 3d84c98

Please sign in to comment.