* Initializes a backend by looking up the backend name in the factory * registry and calling the factory method. Returns a boolean representing * whether the initialization of the backend succeeded. Throws an error if * there is no backend in the factory registry.
(backendName: string)
| 317 | * there is no backend in the factory registry. |
| 318 | */ |
| 319 | private initializeBackend(backendName: string): |
| 320 | {success: boolean|Promise<boolean>, asyncInit: boolean} { |
| 321 | const registryFactoryEntry = this.registryFactory[backendName]; |
| 322 | if (registryFactoryEntry == null) { |
| 323 | throw new Error( |
| 324 | `Cannot initialize backend ${backendName}, no registration found.`); |
| 325 | } |
| 326 | |
| 327 | try { |
| 328 | const backend = registryFactoryEntry.factory(); |
| 329 | /* Test if the factory returns a promise. |
| 330 | Done in a more liberal way than |
| 331 | previous 'Promise.resolve(backend)===backend' |
| 332 | as we needed to account for custom Promise |
| 333 | implementations (e.g. Angular) */ |
| 334 | if (backend && !(backend instanceof KernelBackend) && |
| 335 | typeof backend.then === 'function') { |
| 336 | const promiseId = ++this.pendingBackendInitId; |
| 337 | const success = |
| 338 | backend |
| 339 | .then(backendInstance => { |
| 340 | // Outdated promise. Another backend was set in the meantime. |
| 341 | if (promiseId < this.pendingBackendInitId) { |
| 342 | return false; |
| 343 | } |
| 344 | this.registry[backendName] = backendInstance; |
| 345 | this.pendingBackendInit = null; |
| 346 | return true; |
| 347 | }) |
| 348 | .catch(err => { |
| 349 | // Outdated promise. Another backend was set in the meantime. |
| 350 | if (promiseId < this.pendingBackendInitId) { |
| 351 | return false; |
| 352 | } |
| 353 | this.pendingBackendInit = null; |
| 354 | log.warn(`Initialization of backend ${backendName} failed`); |
| 355 | log.warn(err.stack || err.message); |
| 356 | return false; |
| 357 | }); |
| 358 | this.pendingBackendInit = success; |
| 359 | return {success, asyncInit: true}; |
| 360 | } else { |
| 361 | this.registry[backendName] = backend as KernelBackend; |
| 362 | return {success: true, asyncInit: false}; |
| 363 | } |
| 364 | } catch (err) { |
| 365 | log.warn(`Initialization of backend ${backendName} failed`); |
| 366 | log.warn(err.stack || err.message); |
| 367 | return {success: false, asyncInit: false}; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | removeBackend(backendName: string): void { |
| 372 | if (!(backendName in this.registryFactory)) { |
no outgoing calls
no test coverage detected