* Returns the promise for service with `id` on the given window if available. * Otherwise, resolves with null (service was not registered). * @param {!Window} win * @param {string} id * @param {string} extension * @param {string} version * @param {boolean=} opt_element * @return {!Promise<Obj
( win, id, extension, version, opt_element )
| 173 | * @private |
| 174 | */ |
| 175 | function getElementServicePromiseOrNull( |
| 176 | win, |
| 177 | id, |
| 178 | extension, |
| 179 | version, |
| 180 | opt_element |
| 181 | ) { |
| 182 | return dom |
| 183 | .waitForBodyOpenPromise(win.document) |
| 184 | .then(() => { |
| 185 | // If there is an extension script wait for it to load before trying |
| 186 | // to get the service. Prevents a race condition when everything but |
| 187 | // the extensions is in cache. If there is no script then it's either |
| 188 | // not present, or the service was defined by a test. In those cases |
| 189 | // we don't wait around for an extension that does not exist. |
| 190 | const extensions = getService(win, 'extensions'); |
| 191 | |
| 192 | // TODO(jpettitt) investigate registerExtension to short circuit |
| 193 | // the dom call in extensionScriptsInNode() |
| 194 | if (!extensionScriptInNode(extensions.win, extension, version)) { |
| 195 | return null; |
| 196 | } |
| 197 | return extensions.waitForExtension(extension, version); |
| 198 | }) |
| 199 | .then((ext) => { |
| 200 | if (!ext) { |
| 201 | return null; |
| 202 | } |
| 203 | // If this service is provided by an element, then we can't depend on |
| 204 | // the service (they may not use the element). |
| 205 | if (opt_element) { |
| 206 | return getServicePromiseOrNull(win, id); |
| 207 | } |
| 208 | return getServicePromise(win, id); |
| 209 | }); |
| 210 | } |
no test coverage detected