Skip to content

Commit

Permalink
♻️ Fixit: dispatchCustomEvent in ActionInvocation#trigger (#32884)
Browse files Browse the repository at this point in the history
* Dispatch custom action in ActionInvocation#trigger

* split out legacy method to allow safe migration

* update amp-accordion to use combined method

* Remove accidental newline
  • Loading branch information
rcebulko committed Feb 26, 2021
1 parent a8b0fb1 commit a265567
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 1 addition & 3 deletions extensions/amp-accordion/1.0/amp-accordion.js
Expand Up @@ -62,14 +62,12 @@ class AmpAccordion extends BaseElement {
`accordionSection.${eventName}`,
detail
);
Services.actionServiceForDoc(section).trigger(
Services.actionServiceForDoc(section).triggerWithDispatch(
section,
eventName,
event,
ActionTrust.HIGH
);

super.triggerEvent(section, eventName, detail);
}

/** @override */
Expand Down
20 changes: 19 additions & 1 deletion src/service/action-impl.js
Expand Up @@ -25,12 +25,12 @@ import {Services} from '../services';
import {debounce, throttle} from '../utils/rate-limit';
import {dev, devAssert, user, userAssert} from '../log';
import {dict, hasOwn, map} from '../utils/object';
import {dispatchCustomEvent, isEnabled} from '../dom';
import {getDetail} from '../event-helper';
import {getMode} from '../mode';
import {getValueForExpr} from '../json';
import {isAmp4Email} from '../format';
import {isArray, isFiniteNumber, toArray, toWin} from '../types';
import {isEnabled} from '../dom';
import {registerServiceBuilderForDoc} from '../service';
import {reportError} from '../error';

Expand Down Expand Up @@ -415,6 +415,24 @@ export class ActionService {

/**
* Triggers the specified event on the target element.
* TODO(#32756): Rename this to `trigger` once all invocations have been
* updated to use the combined method.
* @param {!Element} target
* @param {string} eventType
* @param {?ActionEventDef} event
* @param {!ActionTrust} trust
* @param {?JsonObject=} opt_args
* @return {boolean} true if the target has an action.
*/
triggerWithDispatch(target, eventType, event, trust, opt_args) {
const action = this.trigger(target, eventType, event, trust, opt_args);
dispatchCustomEvent(target, eventType, event.detail);
return action;
}

/**
* Triggers the specified event and dispatches a custom event on the target
* element.
* @param {!Element} target
* @param {string} eventType
* @param {?ActionEventDef} event
Expand Down

0 comments on commit a265567

Please sign in to comment.