From c9f731fa58bc87fd876973640768fca2845be6a8 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 4 Jan 2021 12:10:05 -0500 Subject: [PATCH] LP1901760 Improve SharedWorker non-support handling (Angular) Reject requests to SharedWorker resources when shared workers are not supported. The caller is required to handle the rejection in whatever way makes sense for the calling code. Signed-off-by: Bill Erickson Signed-off-by: Terran McCanna Signed-off-by: Jason Boyer Signed-off-by: Jason Stephenson Signed-off-by: John Amundson --- .../src/eg2/src/app/core/db-store.service.ts | 41 +++++++++++++------ Open-ILS/src/eg2/src/app/core/org.service.ts | 6 +-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/core/db-store.service.ts b/Open-ILS/src/eg2/src/app/core/db-store.service.ts index 259e478f41..b786eefe45 100644 --- a/Open-ILS/src/eg2/src/app/core/db-store.service.ts +++ b/Open-ILS/src/eg2/src/app/core/db-store.service.ts @@ -1,6 +1,18 @@ import {Injectable} from '@angular/core'; -/** Service to relay requests to/from our IndexedDB shared worker */ +/** Service to relay requests to/from our IndexedDB shared worker + * Beware requests will be rejected when SharedWorker's are not supported. + + this.db.request( + schema: 'cache', + table: 'Setting', + action: 'selectWhereIn', + field: 'name', + value: ['foo'] + ).then(value => console.log('my value', value) + ).catch(_ => console.log('SharedWorker's not supported)); + + */ // TODO: move to a more generic location. const WORKER_URL = '/js/ui/default/staff/offline-db-worker.js'; @@ -68,15 +80,17 @@ export class DbStoreService { constructor() {} - private connectToWorker() { - if (this.worker || this.cannotConnect) { return; } + // Returns true if connection is successful, false otherwise + private connectToWorker(): boolean { + if (this.worker) { return true; } + if (this.cannotConnect) { return false; } try { this.worker = new SharedWorker(WORKER_URL); } catch (E) { console.warn('SharedWorker() not supported', E); this.cannotConnect = true; - return; + return false; } this.worker.onerror = err => { @@ -89,6 +103,7 @@ export class DbStoreService { 'message', evt => this.handleMessage(evt)); this.worker.port.start(); + return true; } private handleMessage(evt: MessageEvent) { @@ -116,7 +131,13 @@ export class DbStoreService { // for future resolution. Store the request ID in the request // arguments, so it's included in the response, and in the // activeRequests list for linking. + // Returns a rejected promise if shared workers are not supported. private relayRequest(req: DbStoreRequest): Promise { + + if (!this.connectToWorker()) { + return Promise.reject('Shared Workers not supported'); + } + return new Promise((resolve, reject) => { const id = req.id = this.autoId++; this.activeRequests[id] = {id: id, resolve: resolve, reject: reject}; @@ -168,16 +189,10 @@ export class DbStoreService { return this.schemasInProgress[schema] = promise; } + // Request may be rejected if SharedWorker's are not supported. + // All calls to this method should include an error handler in + // the .then() or a .cache() handler after the .then(). request(req: DbStoreRequest): Promise { - - // NO-OP if we're already connected. - this.connectToWorker(); - - // If we are unable to connect, it means we are in an - // environment that does not support shared workers. - // Treat all requests as a NO-OP. - if (this.cannotConnect) { return Promise.resolve(); } - return this.connectToSchemas().then(_ => this.relayRequest(req)); } } diff --git a/Open-ILS/src/eg2/src/app/core/org.service.ts b/Open-ILS/src/eg2/src/app/core/org.service.ts index 456f93f6f9..8caf1d9db6 100644 --- a/Open-ILS/src/eg2/src/app/core/org.service.ts +++ b/Open-ILS/src/eg2/src/app/core/org.service.ts @@ -235,7 +235,7 @@ export class OrgService { }); return batch; - }); + }).catch(_ => batch); } // Add values for the list of named settings from the 'batch' to @@ -257,7 +257,7 @@ export class OrgService { table: 'Setting', action: 'insertOrReplace', rows: rows - }).then(_ => batch); + }).then(_ => batch).catch(_ => batch); } /** @@ -346,6 +346,6 @@ export class OrgService { schema: 'cache', table: 'Setting', action: 'deleteAll' - }); + }).catch(_ => null) } } -- 2.43.2