* Gets the persisted CID data as a promise. It tries to read from * localStorage first then from viewer if it is in embedded mode. * Returns null if none was found. * @param {!./ampdoc-impl.AmpDoc} ampdoc * @return {!Promise<?BaseCidInfoDef>}
(ampdoc)
| 628 | * @return {!Promise<?BaseCidInfoDef>} |
| 629 | */ |
| 630 | function read(ampdoc) { |
| 631 | const {win} = ampdoc; |
| 632 | let data; |
| 633 | try { |
| 634 | data = win.localStorage.getItem('amp-cid'); |
| 635 | } catch (ignore) { |
| 636 | // If reading from localStorage fails, we assume it is empty. |
| 637 | } |
| 638 | let dataPromise = Promise.resolve(data); |
| 639 | if (!data && isIframed(win)) { |
| 640 | // If we are being embedded, try to get the base cid from the viewer. |
| 641 | dataPromise = viewerBaseCid(ampdoc); |
| 642 | } |
| 643 | return dataPromise.then((data) => { |
| 644 | if (!data) { |
| 645 | return null; |
| 646 | } |
| 647 | const item = parseJson(data); |
| 648 | return { |
| 649 | time: item['time'], |
| 650 | cid: item['cid'], |
| 651 | }; |
| 652 | }); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Whether the retrieved cid object is expired and should be ignored. |
no test coverage detected