From 69f36802985e6e09dbcebea29ef0b704c4c8b666 Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Fri, 8 Feb 2019 21:43:46 +0100 Subject: [PATCH 1/8] feat: new IDB storing behavior with no wrapping --- README.md | 2 +- .../src/lib/databases/indexeddb-database.ts | 51 +++++++++++-------- .../lib/databases/localstorage-database.ts | 6 +-- .../local-storage/src/lib/lib.service.spec.ts | 51 +++++++++---------- .../local-storage/src/lib/lib.service.ts | 2 +- .../ngx-pwa/local-storage/src/lib/tokens.ts | 31 +++++++---- 6 files changed, 79 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 21a5177b..c89105a5 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ import { LocalStorage } from '@ngx-pwa/local-storage'; @Injectable() export class YourService { - constructor(protected localStorage: LocalStorage) {} + constructor(private localStorage: LocalStorage) {} } ``` diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index 946660fe..782b73ed 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -1,9 +1,9 @@ -import { Injectable, Optional, Inject } from '@angular/core'; +import { Injectable, Inject } from '@angular/core'; import { Observable, ReplaySubject, fromEvent, of, throwError, race } from 'rxjs'; import { map, mergeMap, first, tap, filter } from 'rxjs/operators'; import { LocalDatabase } from './local-database'; -import { PREFIX, IDB_DB_NAME, DEFAULT_IDB_DB_NAME, IDB_STORE_NAME, DEFAULT_IDB_STORE_NAME } from '../tokens'; +import { PREFIX, IDB_DB_NAME, DEFAULT_IDB_DB_NAME, IDB_STORE_NAME, DEFAULT_IDB_STORE_NAME, COMPATIBILITY_PRIOR_TO_V8 } from '../tokens'; import { IDBBrokenError } from '../exceptions'; @Injectable({ @@ -14,18 +14,23 @@ export class IndexedDBDatabase implements LocalDatabase { /** * `indexedDB` database name */ - protected dbName: string; + private readonly dbName: string; /** * `indexedDB` object store name */ - protected storeName: string; + private readonly storeName: string; /** * `indexedDB` data path name for local storage (where items' value will be stored) */ private readonly dataPath = 'value'; + /** + * Flag to keep storing behavior prior to version 8. + */ + private readonly compatibilityPriorToV8: boolean; + /** * `indexedDB` database connection, wrapped in a RxJS `ReplaySubject` to be able to access the connection * even after the connection success event happened @@ -59,11 +64,13 @@ export class IndexedDBDatabase implements LocalDatabase { * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain * @param dbName `indexedDB` database name * @param storeName `indexedDB` store name + * @param compatibilityPriorToV8 Flag to keep storing behavior prior to version 8 */ constructor( - @Optional() @Inject(PREFIX) prefix: string | null = null, - @Optional() @Inject(IDB_DB_NAME) dbName = DEFAULT_IDB_DB_NAME, - @Optional() @Inject(IDB_STORE_NAME) storeName = DEFAULT_IDB_STORE_NAME, + @Inject(PREFIX) prefix: string | null = null, + @Inject(IDB_DB_NAME) dbName = DEFAULT_IDB_DB_NAME, + @Inject(IDB_STORE_NAME) storeName = DEFAULT_IDB_STORE_NAME, + @Inject(COMPATIBILITY_PRIOR_TO_V8) compatibilityPriorToV8 = false, ) { /* Initialize `indexedDB` database name, with prefix if provided by the user */ @@ -72,6 +79,8 @@ export class IndexedDBDatabase implements LocalDatabase { /* Initialize `indexedDB` store name */ this.storeName = storeName; + this.compatibilityPriorToV8 = compatibilityPriorToV8; + /* Creating the RxJS ReplaySubject */ this.database = new ReplaySubject(); @@ -97,24 +106,20 @@ export class IndexedDBDatabase implements LocalDatabase { /* Manage success and error events, and get the result */ return this.requestEventsAndMapTo(request, () => { - /* Currently, the lib is wrapping the value in a `{ value: ... }` object, so test this case */ - // TODO: add a check to see if the object has only one key - // TODO: stop wrapping - if ((request.result !== undefined) + if (!this.compatibilityPriorToV8 && (request.result !== undefined) && (request.result !== null)) { + + /* Cast to the wanted type */ + return request.result as T; + + } else if (this.compatibilityPriorToV8 + && (request.result !== undefined) && (request.result !== null) - && (typeof request.result === 'object') - && (this.dataPath in request.result) && (request.result[this.dataPath] !== undefined) && (request.result[this.dataPath] !== null)) { - /* If so, unwrap the value and cast it to the wanted type */ + /* Prior to v8, the value was wrapped in an `{ value: ...}` object */ return (request.result[this.dataPath] as T); - } else if ((request.result !== undefined) && (request.result !== null)) { - - /* Otherwise, return the value directly, casted to the wanted type */ - return request.result as T; - } /* Return `null` if the value is `null` or `undefined` */ @@ -165,11 +170,13 @@ export class IndexedDBDatabase implements LocalDatabase { * otherwise it could lead to concurrency failures * Avoid https://github.com/cyrilletuzi/angular-async-local-storage/issues/47 */ + /* Prior to v8, data was wrapped in a `{ value: ... }` object */ + const dataToStore = !this.compatibilityPriorToV8 ? data : { [this.dataPath]: data }; + /* Add if the item is not existing yet, or update otherwise */ - // TODO: stop wrapping const request2 = (existingEntry === undefined) ? - store.add({ [this.dataPath]: data }, key) : - store.put({ [this.dataPath]: data }, key); + store.add(dataToStore, key) : + store.put(dataToStore, key); /* Manage success and error events, and map to `true` */ return this.requestEventsAndMapTo(request2, () => true); diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts index 6830a6f1..3559a02d 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts @@ -1,4 +1,4 @@ -import { Injectable, Optional, Inject } from '@angular/core'; +import { Injectable, Inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { LocalDatabase } from './local-database'; @@ -12,7 +12,7 @@ export class LocalStorageDatabase implements LocalDatabase { /** * Optional user prefix to avoid collision for multiple apps on the same subdomain */ - protected prefix = ''; + private prefix = ''; /** * Number of items in `localStorage` @@ -28,7 +28,7 @@ export class LocalStorageDatabase implements LocalDatabase { * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain */ - constructor(@Optional() @Inject(PREFIX) userPrefix: string | null = null) { + constructor(@Inject(PREFIX) userPrefix: string | null = null) { if (userPrefix) { this.prefix = `${userPrefix}_`; diff --git a/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts b/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts index 76489847..f8047955 100755 --- a/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts +++ b/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts @@ -738,7 +738,7 @@ describe('IndexedDB', () => { request.addEventListener('success', () => { - expect(request.result).toEqual({ value }); + expect(request.result).toEqual(value); done(); @@ -766,21 +766,32 @@ describe('IndexedDB', () => { }); +describe('IndexedDB with compatibilityPriorToV8', () => { + + const localStorageService = new LocalStorage(new IndexedDBDatabase(null, undefined, undefined, true), new JSONValidator()); + + beforeEach((done) => { + + /* Clear `localStorage` for some browsers private mode which fallbacks to `localStorage` */ + localStorage.clear(); + + clearIndexedDB(done); + + }); + + tests(localStorageService); + +}); + describe('localStorage and a prefix', () => { const prefix = 'myapp'; it('check prefix', () => { - class LocalStorageDatabasePrefix extends LocalStorageDatabase { - getPrefix() { - return this.prefix; - } - } - - const localStorageServicePrefix = new LocalStorageDatabasePrefix(prefix); + const localStorageServicePrefix = new LocalStorageDatabase(prefix); - expect(localStorageServicePrefix.getPrefix()).toBe(`${prefix}_`); + expect(localStorageServicePrefix['prefix']).toBe(`${prefix}_`); }); @@ -800,15 +811,9 @@ describe('IndexedDB and a prefix', () => { it('check prefix', () => { - class IndexedDBDatabasePrefix extends IndexedDBDatabase { - getDbBame() { - return this.dbName; - } - } + const indexedDBService = new IndexedDBDatabase(prefix); - const indexedDBService = new IndexedDBDatabasePrefix(prefix); - - expect(indexedDBService.getDbBame()).toBe(`${prefix}_${DEFAULT_IDB_DB_NAME}`); + expect(indexedDBService['dbName']).toBe(`${prefix}_${DEFAULT_IDB_DB_NAME}`); }); @@ -816,17 +821,9 @@ describe('IndexedDB and a prefix', () => { const dbName = 'customDb'; - class IndexedDBDatabasePrefix extends IndexedDBDatabase { - - getDbBame() { - return this.dbName; - } - - } - - const indexedDBService = new IndexedDBDatabasePrefix(prefix, dbName); + const indexedDBService = new IndexedDBDatabase(prefix, dbName); - expect(indexedDBService.getDbBame()).toBe(`${prefix}_${dbName}`); + expect(indexedDBService['dbName']).toBe(`${prefix}_${dbName}`); }); diff --git a/projects/ngx-pwa/local-storage/src/lib/lib.service.ts b/projects/ngx-pwa/local-storage/src/lib/lib.service.ts index 47bc21aa..107af110 100755 --- a/projects/ngx-pwa/local-storage/src/lib/lib.service.ts +++ b/projects/ngx-pwa/local-storage/src/lib/lib.service.ts @@ -49,7 +49,7 @@ export class LocalStorage { constructor( private database: LocalDatabase, private jsonValidator: JSONValidator, - @Optional() @Inject(PREFIX) protected prefix: string | null = null, + @Inject(PREFIX) private prefix: string | null = null, ) {} /** diff --git a/projects/ngx-pwa/local-storage/src/lib/tokens.ts b/projects/ngx-pwa/local-storage/src/lib/tokens.ts index 01ce4d5a..390a693b 100644 --- a/projects/ngx-pwa/local-storage/src/lib/tokens.ts +++ b/projects/ngx-pwa/local-storage/src/lib/tokens.ts @@ -1,18 +1,13 @@ import { InjectionToken, Provider } from '@angular/core'; /** - * Internal. Use the `localStorageProviders()` helper function to provide options. + * Token to provide a prefix to avoid collision when multiple apps on the same subdomain. */ export const PREFIX = new InjectionToken('localStoragePrefix', { providedIn: 'root', factory: () => '' }); -/** - * @deprecated Use the `localStorageProviders()` helper function to provide options. Will be removed in v9. - */ -export const LOCAL_STORAGE_PREFIX = PREFIX; - /** * Default name used for `indexedDB` database. * *Use only for interoperability with other APIs.* @@ -20,9 +15,9 @@ export const LOCAL_STORAGE_PREFIX = PREFIX; export const DEFAULT_IDB_DB_NAME = 'ngStorage'; /** - * Internal. Use the `localStorageProviders()` helper function to provide options. + * Token to provide `indexedDB` database name. */ -export const IDB_DB_NAME = new InjectionToken('localStorageIndexedDbName', { +export const IDB_DB_NAME = new InjectionToken('localStorageIDBDBName', { providedIn: 'root', factory: () => DEFAULT_IDB_DB_NAME }); @@ -34,13 +29,21 @@ export const IDB_DB_NAME = new InjectionToken('localStorageIndexedDbName export const DEFAULT_IDB_STORE_NAME = 'localStorage'; /** - * Internal. Use the `localStorageProviders()` helper function to provide options. + * Token to provide `indexedDB` store name. */ -export const IDB_STORE_NAME = new InjectionToken('localStorageIndexedDbStoreName', { +export const IDB_STORE_NAME = new InjectionToken('localStorageIDBStoreName', { providedIn: 'root', factory: () => DEFAULT_IDB_STORE_NAME }); +/** + * Token to keep storing behavior prior to version 8. + */ +export const COMPATIBILITY_PRIOR_TO_V8 = new InjectionToken('localStorageCompatibilityPriorToV8', { + providedIn: 'root', + factory: () => false +}); + export interface LocalStorageProvidersConfig { /** @@ -64,6 +67,13 @@ export interface LocalStorageProvidersConfig { */ IDBStoreName?: string; + /** + * Flag to keep storing behavior prior to version 8. + * Not needed for new installs, + * **must be `true` for upgrades from versions prior to V8, otherwise previously stored data will be lost.** + */ + compatibilityPriorToV8?: boolean; + } /** @@ -77,6 +87,7 @@ export function localStorageProviders(config: LocalStorageProvidersConfig): Prov config.prefix ? { provide: PREFIX, useValue: config.prefix } : [], config.IDBDBName ? { provide: IDB_DB_NAME, useValue: config.IDBDBName } : [], config.IDBStoreName ? { provide: IDB_STORE_NAME, useValue: config.IDBStoreName } : [], + config.compatibilityPriorToV8 ? { provide: COMPATIBILITY_PRIOR_TO_V8, useValue: config.compatibilityPriorToV8 } : [], ]; } From 8cd938c6308a3ca216c815f4023df5c3c15d878e Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Sun, 10 Feb 2019 10:40:00 +0100 Subject: [PATCH 2/8] fix: consistent token injection --- .../local-storage/src/lib/databases/indexeddb-database.ts | 2 +- .../local-storage/src/lib/databases/local-database.ts | 6 +++--- .../src/lib/databases/localstorage-database.ts | 2 +- projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts | 4 ++-- projects/ngx-pwa/local-storage/src/lib/lib.service.ts | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index 782b73ed..f4924ff0 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -67,7 +67,7 @@ export class IndexedDBDatabase implements LocalDatabase { * @param compatibilityPriorToV8 Flag to keep storing behavior prior to version 8 */ constructor( - @Inject(PREFIX) prefix: string | null = null, + @Inject(PREFIX) prefix = '', @Inject(IDB_DB_NAME) dbName = DEFAULT_IDB_DB_NAME, @Inject(IDB_STORE_NAME) storeName = DEFAULT_IDB_STORE_NAME, @Inject(COMPATIBILITY_PRIOR_TO_V8) compatibilityPriorToV8 = false, diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/local-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/local-database.ts index f061de55..909a7aec 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/local-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/local-database.ts @@ -1,4 +1,4 @@ -import { Injectable, PLATFORM_ID, Optional } from '@angular/core'; +import { Injectable, PLATFORM_ID } from '@angular/core'; import { isPlatformBrowser, isPlatformWorkerApp, isPlatformWorkerUi } from '@angular/common'; import { Observable } from 'rxjs'; @@ -13,7 +13,7 @@ import { PREFIX } from '../tokens'; * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain * @see https://github.com/cyrilletuzi/angular-async-local-storage/blob/master/docs/BROWSERS_SUPPORT.md */ -export function localDatabaseFactory(platformId: Object, prefix: string | null): LocalDatabase { +export function localDatabaseFactory(platformId: Object, prefix: string): LocalDatabase { // Do not explicit `window` here, as the global object is not the same in web workers if ((isPlatformBrowser(platformId) || isPlatformWorkerApp(platformId) || isPlatformWorkerUi(platformId)) @@ -63,7 +63,7 @@ export function localDatabaseFactory(platformId: Object, prefix: string | null): useFactory: localDatabaseFactory, deps: [ PLATFORM_ID, - [new Optional(), PREFIX] + PREFIX ] }) export abstract class LocalDatabase { diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts index 3559a02d..dde89b6c 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts @@ -28,7 +28,7 @@ export class LocalStorageDatabase implements LocalDatabase { * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain */ - constructor(@Inject(PREFIX) userPrefix: string | null = null) { + constructor(@Inject(PREFIX) userPrefix = '') { if (userPrefix) { this.prefix = `${userPrefix}_`; diff --git a/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts b/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts index f8047955..b138ad8c 100755 --- a/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts +++ b/projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts @@ -768,7 +768,7 @@ describe('IndexedDB', () => { describe('IndexedDB with compatibilityPriorToV8', () => { - const localStorageService = new LocalStorage(new IndexedDBDatabase(null, undefined, undefined, true), new JSONValidator()); + const localStorageService = new LocalStorage(new IndexedDBDatabase(undefined, undefined, undefined, true), new JSONValidator()); beforeEach((done) => { @@ -847,7 +847,7 @@ describe('IndexedDB with custom database and store names', () => { const dbName = 'dBcustom'; const storeName = 'storeCustom'; - const localStorageService = new LocalStorage(new IndexedDBDatabase(null, dbName, storeName), new JSONValidator()); + const localStorageService = new LocalStorage(new IndexedDBDatabase(undefined, dbName, storeName), new JSONValidator()); beforeEach((done) => { diff --git a/projects/ngx-pwa/local-storage/src/lib/lib.service.ts b/projects/ngx-pwa/local-storage/src/lib/lib.service.ts index 107af110..5262ebf5 100755 --- a/projects/ngx-pwa/local-storage/src/lib/lib.service.ts +++ b/projects/ngx-pwa/local-storage/src/lib/lib.service.ts @@ -1,4 +1,4 @@ -import { Injectable, Optional, Inject } from '@angular/core'; +import { Injectable, Inject } from '@angular/core'; import { Observable, throwError, of, OperatorFunction } from 'rxjs'; import { mergeMap, catchError } from 'rxjs/operators'; @@ -49,7 +49,7 @@ export class LocalStorage { constructor( private database: LocalDatabase, private jsonValidator: JSONValidator, - @Inject(PREFIX) private prefix: string | null = null, + @Inject(PREFIX) private prefix = '', ) {} /** From 936dc0ac632a1f3d88865815b5128219bbd895b1 Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Sun, 10 Feb 2019 10:45:00 +0100 Subject: [PATCH 3/8] fix: consistent option management --- .../src/lib/databases/localstorage-database.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts index dde89b6c..66560953 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts @@ -12,7 +12,7 @@ export class LocalStorageDatabase implements LocalDatabase { /** * Optional user prefix to avoid collision for multiple apps on the same subdomain */ - private prefix = ''; + private readonly prefix: string; /** * Number of items in `localStorage` @@ -30,9 +30,7 @@ export class LocalStorageDatabase implements LocalDatabase { */ constructor(@Inject(PREFIX) userPrefix = '') { - if (userPrefix) { - this.prefix = `${userPrefix}_`; - } + this.prefix = userPrefix ? `${userPrefix}_` : ''; } From 311945ab9d4d46d3dac198adf182d757b05fa9a4 Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Sun, 10 Feb 2019 10:49:05 +0100 Subject: [PATCH 4/8] fix: keep only the last replaysubject value --- .../local-storage/src/lib/databases/indexeddb-database.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index f4924ff0..36ed3dfe 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -82,7 +82,7 @@ export class IndexedDBDatabase implements LocalDatabase { this.compatibilityPriorToV8 = compatibilityPriorToV8; /* Creating the RxJS ReplaySubject */ - this.database = new ReplaySubject(); + this.database = new ReplaySubject(1); /* Connect to `indexedDB`, with prefix if provided by the user */ this.connect(); From 955a40340b82e52833f8322429bcbb1da249684d Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Sun, 10 Feb 2019 11:11:56 +0100 Subject: [PATCH 5/8] fix: consistent option management --- .../local-storage/src/lib/databases/localstorage-database.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts index 66560953..ab74ce7a 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts @@ -28,9 +28,9 @@ export class LocalStorageDatabase implements LocalDatabase { * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain */ - constructor(@Inject(PREFIX) userPrefix = '') { + constructor(@Inject(PREFIX) prefix = '') { - this.prefix = userPrefix ? `${userPrefix}_` : ''; + this.prefix = prefix ? `${prefix}_` : ''; } From 5a15b3155c45707594bb7772d3b025bab0d2dc2d Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Mon, 11 Feb 2019 09:36:57 +0100 Subject: [PATCH 6/8] docs: todo --- .../local-storage/src/lib/databases/indexeddb-database.ts | 1 + projects/ngx-pwa/local-storage/src/lib/tokens.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index 36ed3dfe..11924572 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -59,6 +59,7 @@ export class IndexedDBDatabase implements LocalDatabase { } + // TODO: revert compatibilityPriorToV8 to true by default if ng update is not possible /** * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain diff --git a/projects/ngx-pwa/local-storage/src/lib/tokens.ts b/projects/ngx-pwa/local-storage/src/lib/tokens.ts index 390a693b..d42b8b3e 100644 --- a/projects/ngx-pwa/local-storage/src/lib/tokens.ts +++ b/projects/ngx-pwa/local-storage/src/lib/tokens.ts @@ -36,6 +36,7 @@ export const IDB_STORE_NAME = new InjectionToken('localStorageIDBStoreNa factory: () => DEFAULT_IDB_STORE_NAME }); +// TODO: revert to true by default if ng update is not possible /** * Token to keep storing behavior prior to version 8. */ From de857e78582f68db06374fcd8632b3aa8d19dde8 Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Mon, 11 Feb 2019 09:42:46 +0100 Subject: [PATCH 7/8] fix: consistent token management --- .../src/lib/databases/indexeddb-database.ts | 9 ++++++--- .../src/lib/databases/localstorage-database.ts | 7 ++++--- projects/ngx-pwa/local-storage/src/lib/tokens.ts | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index 11924572..e804b4b6 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -3,7 +3,10 @@ import { Observable, ReplaySubject, fromEvent, of, throwError, race } from 'rxjs import { map, mergeMap, first, tap, filter } from 'rxjs/operators'; import { LocalDatabase } from './local-database'; -import { PREFIX, IDB_DB_NAME, DEFAULT_IDB_DB_NAME, IDB_STORE_NAME, DEFAULT_IDB_STORE_NAME, COMPATIBILITY_PRIOR_TO_V8 } from '../tokens'; +import { + PREFIX, IDB_DB_NAME, DEFAULT_IDB_DB_NAME, IDB_STORE_NAME, DEFAULT_IDB_STORE_NAME, + COMPATIBILITY_PRIOR_TO_V8, DEFAULT_PREFIX, DEFAULT_COMPATIBILITY_PRIOR_TO_V8 +} from '../tokens'; import { IDBBrokenError } from '../exceptions'; @Injectable({ @@ -68,10 +71,10 @@ export class IndexedDBDatabase implements LocalDatabase { * @param compatibilityPriorToV8 Flag to keep storing behavior prior to version 8 */ constructor( - @Inject(PREFIX) prefix = '', + @Inject(PREFIX) prefix = DEFAULT_PREFIX, @Inject(IDB_DB_NAME) dbName = DEFAULT_IDB_DB_NAME, @Inject(IDB_STORE_NAME) storeName = DEFAULT_IDB_STORE_NAME, - @Inject(COMPATIBILITY_PRIOR_TO_V8) compatibilityPriorToV8 = false, + @Inject(COMPATIBILITY_PRIOR_TO_V8) compatibilityPriorToV8 = DEFAULT_COMPATIBILITY_PRIOR_TO_V8, ) { /* Initialize `indexedDB` database name, with prefix if provided by the user */ diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts index ab74ce7a..8f175cb9 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { LocalDatabase } from './local-database'; -import { PREFIX } from '../tokens'; +import { PREFIX, DEFAULT_PREFIX } from '../tokens'; @Injectable({ providedIn: 'root' @@ -28,9 +28,10 @@ export class LocalStorageDatabase implements LocalDatabase { * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain */ - constructor(@Inject(PREFIX) prefix = '') { + constructor(@Inject(PREFIX) prefix = DEFAULT_PREFIX) { - this.prefix = prefix ? `${prefix}_` : ''; + /* Add `_` after prefix only if not empty */ + this.prefix = prefix ? `${prefix}_` : prefix; } diff --git a/projects/ngx-pwa/local-storage/src/lib/tokens.ts b/projects/ngx-pwa/local-storage/src/lib/tokens.ts index d42b8b3e..30feef7a 100644 --- a/projects/ngx-pwa/local-storage/src/lib/tokens.ts +++ b/projects/ngx-pwa/local-storage/src/lib/tokens.ts @@ -1,11 +1,17 @@ import { InjectionToken, Provider } from '@angular/core'; +/** + * Default prefix. + * *Use only to avoid conflict in multiple apps on the same subdomain.* + */ +export const DEFAULT_PREFIX = ''; + /** * Token to provide a prefix to avoid collision when multiple apps on the same subdomain. */ export const PREFIX = new InjectionToken('localStoragePrefix', { providedIn: 'root', - factory: () => '' + factory: () => DEFAULT_PREFIX }); /** @@ -37,12 +43,17 @@ export const IDB_STORE_NAME = new InjectionToken('localStorageIDBStoreNa }); // TODO: revert to true by default if ng update is not possible +/** + * Default compatibility mode. + */ +export const DEFAULT_COMPATIBILITY_PRIOR_TO_V8 = false; + /** * Token to keep storing behavior prior to version 8. */ export const COMPATIBILITY_PRIOR_TO_V8 = new InjectionToken('localStorageCompatibilityPriorToV8', { providedIn: 'root', - factory: () => false + factory: () => DEFAULT_COMPATIBILITY_PRIOR_TO_V8 }); export interface LocalStorageProvidersConfig { From d961ad52871bd613722760a863a65a3113cf1a03 Mon Sep 17 00:00:00 2001 From: Cyrille Tuzi Date: Mon, 11 Feb 2019 09:44:31 +0100 Subject: [PATCH 8/8] docs: remove todo --- .../local-storage/src/lib/databases/indexeddb-database.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts index e804b4b6..2231c5b5 100644 --- a/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts +++ b/projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts @@ -62,7 +62,6 @@ export class IndexedDBDatabase implements LocalDatabase { } - // TODO: revert compatibilityPriorToV8 to true by default if ng update is not possible /** * Constructor params are provided by Angular (but can also be passed manually in tests) * @param prefix Optional user prefix to avoid collision for multiple apps on the same subdomain