-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add storage scoping #1517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
projects/common/src/utilities/browser/storage/dictionary-storage-impl.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { DictionaryStorageImpl } from './dictionary-storage-impl'; | ||
|
||
describe('Dictionary Storage impl', () => { | ||
test('can read and write values', () => { | ||
const storage = new DictionaryStorageImpl(); | ||
|
||
storage.setItem('foo', 'bar'); | ||
expect(storage.getItem('foo')).toBe('bar'); | ||
expect(storage.getItem('non-existent')).toBeNull(); | ||
storage.setItem('foo', 'bar-update'); | ||
expect(storage.getItem('foo')).toBe('bar-update'); | ||
}); | ||
|
||
test('can detect length', () => { | ||
const storage = new DictionaryStorageImpl(); | ||
expect(storage.length).toBe(0); | ||
storage.setItem('foo', 'bar'); | ||
expect(storage.length).toBe(1); | ||
|
||
storage.setItem('a', 'b'); | ||
expect(storage.length).toBe(2); | ||
storage.removeItem('a'); | ||
expect(storage.length).toBe(1); | ||
storage.clear(); | ||
expect(storage.length).toBe(0); | ||
}); | ||
|
||
test('can be initialized from a dictionary', () => { | ||
const storage = new DictionaryStorageImpl({ foo: 'bar', a: 'b' }); | ||
storage.setItem('x', 'y'); | ||
expect(storage.length).toBe(3); | ||
|
||
expect(storage.getItem('foo')).toBe('bar'); | ||
expect(storage.getItem('a')).toBe('b'); | ||
expect(storage.getItem('x')).toBe('y'); | ||
}); | ||
|
||
test('can serialize to and from JSON string', () => { | ||
const storageFromDictionary = new DictionaryStorageImpl({ foo: 'bar', a: 'b' }); | ||
storageFromDictionary.setItem('c', 'd'); | ||
storageFromDictionary.removeItem('a'); | ||
expect(storageFromDictionary.toJsonString()).toBe('{"foo":"bar","c":"d"}'); | ||
|
||
const storageFromString = DictionaryStorageImpl.fromString('{"foo":"bar","a":"b"}'); | ||
expect(storageFromString.length).toBe(2); | ||
expect(storageFromString.getItem('foo')).toBe('bar'); | ||
expect(storageFromString.getItem('a')).toBe('b'); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
projects/common/src/utilities/browser/storage/dictionary-storage-impl.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Dictionary } from '../../types/types'; | ||
|
||
export class DictionaryStorageImpl implements Storage { | ||
private readonly data: Dictionary<string>; | ||
|
||
public static fromString(jsonString: string): DictionaryStorageImpl { | ||
return new DictionaryStorageImpl(JSON.parse(jsonString)); | ||
} | ||
|
||
public constructor(data: Dictionary<string> = {}) { | ||
this.data = { ...data }; | ||
} | ||
|
||
public get length(): number { | ||
return Object.keys(this.data).length; | ||
} | ||
|
||
public clear(): void { | ||
Object.keys(this.data).forEach(key => this.removeItem(key)); | ||
} | ||
|
||
public getItem(key: string): string | null { | ||
// tslint:disable-next-line: no-null-keyword | ||
return this.data[key] ?? null; | ||
} | ||
|
||
public key(index: number): string | null { | ||
// tslint:disable-next-line: no-null-keyword | ||
return Object.keys(this.data)[index] ?? null; | ||
} | ||
|
||
public removeItem(key: string): void { | ||
// tslint:disable-next-line:no-dynamic-delete | ||
delete this.data[key]; | ||
} | ||
|
||
public setItem(key: string, value: string): void { | ||
this.data[key] = value; | ||
} | ||
|
||
public toJsonString(): string { | ||
return JSON.stringify(this.data); | ||
} | ||
} |
33 changes: 2 additions & 31 deletions
33
projects/common/src/utilities/browser/storage/in-memory-storage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,10 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { AbstractStorage } from './abstract-storage'; | ||
import { DictionaryStorageImpl } from './dictionary-storage-impl'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class InMemoryStorage extends AbstractStorage { | ||
public constructor() { | ||
super( | ||
new (class { | ||
private readonly data: Map<string, string> = new Map(); | ||
|
||
public get length(): number { | ||
return this.data.size; | ||
} | ||
|
||
public clear(): void { | ||
this.data.clear(); | ||
} | ||
|
||
public getItem(key: string): string | null { | ||
// tslint:disable-next-line: no-null-keyword | ||
return this.data.get(key) ?? null; | ||
} | ||
|
||
public key(index: number): string | null { | ||
// tslint:disable-next-line: no-null-keyword | ||
return Array.from(this.data.keys())[index] ?? null; | ||
} | ||
|
||
public removeItem(key: string): void { | ||
this.data.delete(key); | ||
} | ||
|
||
public setItem(key: string, value: string): void { | ||
this.data.set(key, value); | ||
} | ||
})() | ||
); | ||
super(new DictionaryStorageImpl()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this redundant ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant with what? Not sure I follow - if you mean the type portion, we have a lint rule that requires all class members to have a declared type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant, this
changeSubject
. I saw we're usingchangeSubject.next
but apart from that this is not being used, or am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in
watch
- it's not visible by default in this PR because it didn't change (neither did the subject, just the line below it 😉 )