Skip to content

Commit

Permalink
refactor(core): Add an ActionResolver option to Dispatcher.
Browse files Browse the repository at this point in the history
This will enable internal usages to migrate from ActionResolver in
EventContrat to ActionResolver in Dispatcher.
  • Loading branch information
tbondwilkinson committed May 11, 2024
1 parent c1f95f3 commit af73f76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
19 changes: 16 additions & 3 deletions packages/core/primitives/event-dispatch/src/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {EventType} from './event_type';
import {Restriction} from './restriction';
import {UnrenamedEventContract} from './eventcontract';
import * as eventLib from './event';
import {ActionResolver} from './action_resolver';

/**
* A replayer is a function that is called when there are queued events,
Expand All @@ -35,13 +36,18 @@ export type GlobalHandler = (event: Event) => boolean | void;
* jsaction.
*/
export class Dispatcher {
/** The queue of events. */
private readonly replayEventInfoWrappers: EventInfoWrapper[] = [];
// The ActionResolver to use to resolve actions.
private actionResolver?: ActionResolver;

/** The replayer function to be called when there are queued events. */
private eventReplayer?: Replayer;

/** Whether the event replay is scheduled. */
private eventReplayScheduled = false;

/** The queue of events. */
private readonly replayEventInfoWrappers: EventInfoWrapper[] = [];

/**
* Options are:
* 1. `eventReplayer`: When the event contract dispatches replay events
Expand All @@ -51,8 +57,12 @@ export class Dispatcher {
*/
constructor(
private readonly dispatchDelegate: (eventInfoWrapper: EventInfoWrapper) => void,
{eventReplayer = undefined}: {eventReplayer?: Replayer} = {},
{
actionResolver = undefined,
eventReplayer = undefined,
}: {actionResolver?: ActionResolver; eventReplayer?: Replayer} = {},
) {
this.actionResolver = actionResolver;
this.eventReplayer = eventReplayer;
}

Expand All @@ -78,6 +88,9 @@ export class Dispatcher {
*/
dispatch(eventInfo: EventInfo): void {
const eventInfoWrapper = new EventInfoWrapper(eventInfo);
if (this.actionResolver) {
this.actionResolver.resolve(eventInfo);
}
if (eventInfoWrapper.getIsReplay()) {
if (!this.eventReplayer) {
return;
Expand Down
17 changes: 9 additions & 8 deletions packages/core/primitives/event-dispatch/src/eventcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ export class EventContract implements UnrenamedEventContract {

private containerManager: EventContractContainerManager | null;

private readonly actionResolver = new ActionResolver({
customEventSupport: EventContract.CUSTOM_EVENT_SUPPORT,
syntheticMouseEventSupport: EventContract.MOUSE_SPECIAL_SUPPORT,
});

/**
* The DOM events which this contract covers. Used to prevent double
* registration of event types. The value of the map is the
Expand Down Expand Up @@ -133,7 +128,13 @@ export class EventContract implements UnrenamedEventContract {
populateClickOnlyAction: typeof a11yClickLib.populateClickOnlyAction,
) => void;

constructor(containerManager: EventContractContainerManager) {
constructor(
containerManager: EventContractContainerManager,
readonly actionResolver: ActionResolver | null = new ActionResolver({
customEventSupport: EventContract.CUSTOM_EVENT_SUPPORT,
syntheticMouseEventSupport: EventContract.MOUSE_SPECIAL_SUPPORT,
}),
) {
this.containerManager = containerManager;
if (EventContract.CUSTOM_EVENT_SUPPORT) {
this.addEvent(EventType.CUSTOM);
Expand Down Expand Up @@ -165,7 +166,7 @@ export class EventContract implements UnrenamedEventContract {
this.queuedEventInfos?.push(eventInfo);
return;
}
this.actionResolver.resolve(eventInfo);
this.actionResolver?.resolve(eventInfo);
const action = eventInfoLib.getAction(eventInfo);
if (action) {
if (shouldPreventDefaultBeforeDispatching(eventInfoLib.getActionElement(action), eventInfo)) {
Expand Down Expand Up @@ -363,7 +364,7 @@ export class EventContract implements UnrenamedEventContract {
populateClickOnlyAction: typeof a11yClickLib.populateClickOnlyAction,
) {
this.addA11yClickListener = true;
this.actionResolver.addA11yClickSupport(
this.actionResolver?.addA11yClickSupport(
updateEventInfoForA11yClick,
preventDefaultForA11yClick,
populateClickOnlyAction,
Expand Down

0 comments on commit af73f76

Please sign in to comment.