Skip to content

Commit

Permalink
refactor(core): Allow the container and the listenable element to be …
Browse files Browse the repository at this point in the history
…configurable for early event contract.

This will allow a multi-app application to listen to early events from different elements and place them
on a separate field on the window.
  • Loading branch information
iteriani committed Apr 30, 2024
1 parent fd54415 commit bd8cfd2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 13 additions & 6 deletions packages/core/primitives/event-dispatch/src/earlyeventcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export declare interface EarlyJsactionData {

// Early Jsaction handler
h: (event: Event) => void;

// Container for listening to events
c: HTMLElement;
}

/**
Expand All @@ -34,8 +37,12 @@ export declare interface EarlyJsactionData {
* late-loaded EventContract.
*/
export class EarlyEventContract {
constructor() {
window._ejsa = {
constructor(
private readonly container: Window = window,
private readonly documentElement = window.document.documentElement,
) {
this.container._ejsa = {
c: documentElement,
q: [],
et: [],
h: (event: Event) => {
Expand All @@ -46,19 +53,19 @@ export class EarlyEventContract {
window.document.documentElement,
Date.now(),
);
window._ejsa!.q.push(eventInfo);
this.container._ejsa!.q.push(eventInfo);
},
};
}

/**
* Installs a list of event types for window.document.documentElement.
* Installs a list of event types for documentElement.
*/
addEvents(types: string[]) {
for (let idx = 0; idx < types.length; idx++) {
const eventType = types[idx];
window._ejsa!.et.push(eventType);
window.document.documentElement.addEventListener(eventType, window._ejsa!.h);
this.container._ejsa!.et.push(eventType);
this.documentElement.addEventListener(eventType, this.container._ejsa!.h);
}
}
}
8 changes: 4 additions & 4 deletions packages/core/primitives/event-dispatch/src/eventcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ export class EventContract implements UnrenamedEventContract {
* in the provided event contract. Once all the events are replayed, it cleans
* up the early contract.
*/
replayEarlyEvents() {
replayEarlyEvents(earlyJsactionContainer: Window = window) {
// Check if the early contract is present and prevent calling this function
// more than once.
const earlyJsactionData: EarlyJsactionData | undefined = window._ejsa;
const earlyJsactionData: EarlyJsactionData | undefined = earlyJsactionContainer._ejsa;
if (!earlyJsactionData) {
return;
}
Expand All @@ -476,9 +476,9 @@ export class EventContract implements UnrenamedEventContract {
const earlyEventHandler: (event: Event) => void = earlyJsactionData.h;
for (let idx = 0; idx < earlyEventTypes.length; idx++) {
const eventType: string = earlyEventTypes[idx];
window.document.documentElement.removeEventListener(eventType, earlyEventHandler);
earlyJsactionData.c.removeEventListener(eventType, earlyEventHandler);
}
delete window._ejsa;
delete earlyJsactionContainer._ejsa;
}

/**
Expand Down

0 comments on commit bd8cfd2

Please sign in to comment.