Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 amp-story: Use listen and remove listeners on unlayoutcallback. #17004

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 60 additions & 51 deletions extensions/amp-story/0.1/amp-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import {debounce} from '../../../src/utils/rate-limit';
import {dev, user} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {findIndex} from '../../../src/utils/array';
import {getDetail} from '../../../src/event-helper';
import {getDetail, listen} from '../../../src/event-helper';
import {getMode} from '../../../src/mode';
import {getSourceOrigin, parseUrlDeprecated} from '../../../src/url';
import {isExperimentOn, toggleExperiment} from '../../../src/experiments';
Expand Down Expand Up @@ -290,6 +290,9 @@ export class AmpStory extends AMP.BaseElement {

/** @private @const {!../../../src/service/platform-impl.Platform} */
this.platform_ = Services.platformFor(this.win);

/** @private {Array<Function>} */
this.unlisteners_ = [];
}


Expand Down Expand Up @@ -343,13 +346,57 @@ export class AmpStory extends AMP.BaseElement {

/** @private */
initializeListeners_() {
this.element.addEventListener(EventType.NEXT_PAGE, () => {
this.next_();
});
this.unlisteners_.push(
listen(this.element, EventType.NEXT_PAGE, () => this.next_()),

this.element.addEventListener(EventType.PREVIOUS_PAGE, () => {
this.previous_();
});
listen(this.element, EventType.PREVIOUS_PAGE, () => this.previous_()),

listen(this.element, EventType.SWITCH_PAGE, e => {
if (this.storeService_.get(StateProperty.BOOKEND_STATE)) {
// Disallow switching pages while the bookend is active.
return;
}

this.switchTo_(getDetail(e)['targetPageId']);
this.ampStoryHint_.hideAllNavigationHint();
}),

listen(this.element, EventType.PAGE_PROGRESS, e => {
const detail = getDetail(e);
const pageId = detail['pageId'];
const progress = detail['progress'];

if (pageId !== this.activePage_.element.id) {
// Ignore progress update events from inactive pages.
return;
}

if (!this.activePage_.isAd()) {
this.systemLayer_.updateProgress(pageId, progress);
}
}),

listen(this.element, EventType.REPLAY, () => {
this.replay_();
}),

listen(this.element, EventType.SHOW_NO_PREVIOUS_PAGE_HELP, () => {
if (
this.storeService_.get(StateProperty.CAN_SHOW_PREVIOUS_PAGE_HELP)
) {
this.ampStoryHint_.showFirstPageHintOverlay();
}
}),

listen(this.element, EventType.TAP_NAVIGATION, e => {
const direction = getDetail(e)['direction'];
this.performTapNavigation_(direction);
}),

listen(this.win.document, 'keydown', e => {
this.onKeyDown_(e);
}, {capture: true})
);

this.storeService_.subscribe(StateProperty.MUTED_STATE, isMuted => {
this.onMutedStateUpdate_(isMuted);
Expand All @@ -367,46 +414,6 @@ export class AmpStory extends AMP.BaseElement {
this.onSupportedBrowserStateUpdate_(isBrowserSupported);
});

this.element.addEventListener(EventType.SWITCH_PAGE, e => {
if (this.storeService_.get(StateProperty.BOOKEND_STATE)) {
// Disallow switching pages while the bookend is active.
return;
}

this.switchTo_(getDetail(e)['targetPageId']);
this.ampStoryHint_.hideAllNavigationHint();
});

this.element.addEventListener(EventType.PAGE_PROGRESS, e => {
const detail = getDetail(e);
const pageId = detail['pageId'];
const progress = detail['progress'];

if (pageId !== this.activePage_.element.id) {
// Ignore progress update events from inactive pages.
return;
}

if (!this.activePage_.isAd()) {
this.systemLayer_.updateProgress(pageId, progress);
}
});

this.element.addEventListener(EventType.REPLAY, () => {
this.replay_();
});

this.element.addEventListener(EventType.SHOW_NO_PREVIOUS_PAGE_HELP, () => {
if (this.storeService_.get(StateProperty.CAN_SHOW_PREVIOUS_PAGE_HELP)) {
this.ampStoryHint_.showFirstPageHintOverlay();
}
});

this.element.addEventListener(EventType.TAP_NAVIGATION, e => {
const direction = getDetail(e)['direction'];
this.performTapNavigation_(direction);
});

this.storeService_.subscribe(StateProperty.BOOKEND_STATE, isActive => {
this.onBookendStateUpdate_(isActive);
});
Expand All @@ -415,10 +422,6 @@ export class AmpStory extends AMP.BaseElement {
this.onDesktopStateUpdate_(isDesktop);
});

this.win.document.addEventListener('keydown', e => {
this.onKeyDown_(e);
}, true);

this.storeService_.subscribe(StateProperty.CURRENT_PAGE_ID, pageId => {
this.onCurrentPageIdUpdate_(pageId);
});
Expand Down Expand Up @@ -611,6 +614,12 @@ export class AmpStory extends AMP.BaseElement {
return storyLayoutPromise;
}

/** @override */
unlayoutCallback() {
this.unlisteners_.forEach(unlisten => unlisten());

return true;
}

/**
* @param {number} timeoutMs The maximum amount of time to wait, in
Expand Down