Skip to content

Commit

Permalink
loader: Add dispose method
Browse files Browse the repository at this point in the history
In a PWA, the loaderServicePromise cannot be reused across page
navigations; style[amp-extension="amp-loader"] is lost. Add a dispose
method to reset the promise when shadowDoc.close() runs.
  • Loading branch information
mattwomple committed Oct 7, 2019
1 parent da0db59 commit 7253153
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion extensions/amp-loader/0.1/amp-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,15 @@ export class LoaderService {
* @param {number} initDelay
* @param {number} elementWidth
* @param {number} elementHeight
* @param {function} unsetLoaderPromise
*/
initializeLoader(
element,
loaderRoot,
initDelay,
elementWidth,
elementHeight
elementHeight,
unsetLoaderPromise
) {
// Cap the loader delay so that the loader appears immediately, rather than
// starting part way through the animation.
Expand All @@ -410,10 +412,19 @@ export class LoaderService {
);
lb.build();

this.unsetLoaderPromise_ = unsetLoaderPromise;

setImportantStyles(element, {
'--loader-delay-offset': `${loaderDelay}ms`,
});
}

/**
* Unset loaderServicePromise when dispose is called on service
*/
dispose() {
this.unsetLoaderPromise_();
}
}

AMP.extension('amp-loader', '0.1', AMP => {
Expand Down
10 changes: 9 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ function getLoaderServicePromise(ampDoc, element) {
return loaderServicePromise;
}

/**
* Restore loaderServicePromise to its initial state.
*/
function unsetLoaderPromise() {
loaderServicePromise = null;
}

/**
* Creates a default "loading indicator" element based on the new design.
*
Expand Down Expand Up @@ -68,7 +75,8 @@ export function createLoaderElement(
loaderRoot,
initDelay,
elementWidth,
elementHeight
elementHeight,
unsetLoaderPromise
);
});

Expand Down

0 comments on commit 7253153

Please sign in to comment.