( ampDoc, clientIdScope, opt_clientIdCookieName, opt_timeout )
| 30 | * @return {!Promise<string|undefined>} A promise for a CID or undefined. |
| 31 | */ |
| 32 | export function getOrCreateAdCid( |
| 33 | ampDoc, |
| 34 | clientIdScope, |
| 35 | opt_clientIdCookieName, |
| 36 | opt_timeout |
| 37 | ) { |
| 38 | const timeout = |
| 39 | isNaN(opt_timeout) || opt_timeout == null ? 1000 : opt_timeout; |
| 40 | const cidPromise = Services.cidForDoc(ampDoc).then((cidService) => { |
| 41 | if (!cidService) { |
| 42 | return; |
| 43 | } |
| 44 | return cidService |
| 45 | .get( |
| 46 | { |
| 47 | scope: dev().assertString(clientIdScope), |
| 48 | createCookieIfNotPresent: true, |
| 49 | cookieName: opt_clientIdCookieName, |
| 50 | }, |
| 51 | Promise.resolve(undefined) |
| 52 | ) |
| 53 | .catch((error) => { |
| 54 | // Not getting a CID is not fatal. |
| 55 | dev().error('AD-CID', error); |
| 56 | return undefined; |
| 57 | }); |
| 58 | }); |
| 59 | // The CID should never be crucial for an ad. If it does not come within |
| 60 | // 1 second, assume it will never arrive. |
| 61 | return Services.timerFor(ampDoc.win) |
| 62 | .timeoutPromise(timeout, cidPromise, 'cid timeout') |
| 63 | .catch((error) => { |
| 64 | // Timeout is not fatal. |
| 65 | dev().warn('AD-CID', error); |
| 66 | return undefined; |
| 67 | }); |
| 68 | } |
no test coverage detected