Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions projects/common/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { DynamicComponentService } from './utilities/angular/dynamic-component.s
// Browser
export { CookieService } from './utilities/browser/cookies/cookie.service';
export { LocalStorage } from './utilities/browser/storage/local-storage';
export { SessionStorage } from './utilities/browser/storage/session-storage';
export * from './utilities/browser/storage/abstract-storage';

// Coercers
export * from './utilities/coercers/boolean-coercer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { runFakeRxjs } from '@hypertrace/test-utils';
import { of } from 'rxjs';
import { AbstractStorage } from './abstract-storage';
import { DictionaryStorageImpl } from './dictionary-storage-impl';

Expand Down Expand Up @@ -38,9 +39,12 @@ describe('Abstract storage', () => {
test('should support scoped storage with no fallback', () => {
const globalDictionary = new DictionaryStorageImpl({ foo: 'bad-foo' });

const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, {
scopeKey: 'test-scope'
});
const scopedStorage = new (class extends AbstractStorage {})(
globalDictionary,
of({
scopeKey: 'test-scope'
})
);

expect(scopedStorage.get('foo')).toBeUndefined();
expect(scopedStorage.contains('foo')).toBe(false);
Expand All @@ -58,10 +62,13 @@ describe('Abstract storage', () => {
test('should support scoped storage with readonly fallback', () => {
const globalDictionary = new DictionaryStorageImpl({ foo: 'original-foo' });

const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, {
scopeKey: 'test-scope',
fallbackPolicy: 'read-only'
});
const scopedStorage = new (class extends AbstractStorage {})(
globalDictionary,
of({
scopeKey: 'test-scope',
fallbackPolicy: 'read-only'
})
);

expect(scopedStorage.get('foo')).toBe('original-foo');
expect(scopedStorage.contains('foo')).toBe(true);
Expand All @@ -77,10 +84,13 @@ describe('Abstract storage', () => {
test('should migrate on read if configured', () => {
const globalDictionary = new DictionaryStorageImpl({ foo: 'original-foo' });

const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, {
scopeKey: 'test-scope',
fallbackPolicy: 'read-and-migrate'
});
const scopedStorage = new (class extends AbstractStorage {})(
globalDictionary,
of({
scopeKey: 'test-scope',
fallbackPolicy: 'read-and-migrate'
})
);

expect(scopedStorage.get('foo')).toBe('original-foo');
expect(scopedStorage.contains('foo')).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import { DictionaryStorageImpl } from './dictionary-storage-impl';
// A small abstraction on browser's storage for mocking and cleaning up the API a bit
export abstract class AbstractStorage {
private readonly changeSubject: Subject<string> = new Subject();
private readonly scopedStorage?: DictionaryStorageImpl;
private scopeConfig?: ScopedStorageConfiguration;
private scopedStorage?: DictionaryStorageImpl;

public constructor(private readonly storage: Storage, private readonly scopeConfig?: ScopedStorageConfiguration) {
if (scopeConfig) {
this.scopedStorage = DictionaryStorageImpl.fromString(storage.getItem(scopeConfig.scopeKey) ?? '{}');
public constructor(
private readonly storage: Storage,
private readonly scopeConfig$?: Observable<ScopedStorageConfiguration>
) {
if (this.scopeConfig$) {
this.scopeConfig$.subscribe(scopeConfig => {
this.scopeConfig = scopeConfig;
this.scopedStorage = DictionaryStorageImpl.fromString(storage.getItem(scopeConfig.scopeKey) ?? '{}');
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { AbstractStorage } from './abstract-storage';
import { Injectable, Optional } from '@angular/core';
import { Observable } from 'rxjs';
import { AbstractStorage, ScopedStorageConfiguration } from './abstract-storage';

@Injectable({ providedIn: 'root' })
export class LocalStorage extends AbstractStorage {
public constructor() {
super(localStorage);
public constructor(@Optional() scopeConfig$?: Observable<ScopedStorageConfiguration>) {
super(localStorage, scopeConfig$);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { AbstractStorage } from './abstract-storage';
import { Injectable, Optional } from '@angular/core';
import { Observable } from 'rxjs';
import { AbstractStorage, ScopedStorageConfiguration } from './abstract-storage';

@Injectable({ providedIn: 'root' })
export class SessionStorage extends AbstractStorage {
public constructor() {
super(sessionStorage);
public constructor(@Optional() scopeConfig$?: Observable<ScopedStorageConfiguration>) {
super(sessionStorage, scopeConfig$);
}
}