-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Add support of serializers. It also gives an ability to sanitize unknown values against a schema. 2. Preload storage first. The previous implementation used default value as initial value that may misunderstood. 3. Use branded type for birthdate.
- Loading branch information
Showing
16 changed files
with
193 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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,10 @@ | ||
import type { Tagged } from 'type-fest' | ||
|
||
const ISODateRegex = /^\d{4}-\d{2}-\d{2}$/ | ||
|
||
export type ISODate = Tagged<string, 'ISODate'> | ||
|
||
export function isISODate(value: unknown): value is ISODate { | ||
// Soft regexp, but fast. | ||
return typeof value === 'string' && ISODateRegex.test(value) | ||
} |
This file contains 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 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,47 +1,61 @@ | ||
import getPackageName from '@/utils/get-package-name' | ||
import type { IStorageAdapter, Subscriber } from '../types' | ||
import StorageSubscription from '../subscription' | ||
import type { IStorageAdapter, Subscriber, Unsubscribe } from '../types' | ||
|
||
export default class LocalStorageAdapter implements IStorageAdapter { | ||
protected subscription = new StorageSubscription<unknown>() | ||
|
||
export default class LocalStorageAdapter<T> implements IStorageAdapter<T> { | ||
readonly #listener = (e: StorageEvent): void => { | ||
this.handleChanged(e) | ||
} | ||
|
||
constructor( | ||
protected readonly name: string, | ||
protected readonly subscriber: Subscriber<T | null>, | ||
) { | ||
constructor(protected readonly name: string) { | ||
window.addEventListener('storage', this.#listener) | ||
} | ||
|
||
read(): T | null { | ||
read(): unknown { | ||
return this.parse(localStorage.getItem(this.storageKey)) | ||
} | ||
|
||
write(value: T): void { | ||
write(value: unknown): void { | ||
localStorage.setItem(this.storageKey, this.serialize(value)) | ||
} | ||
|
||
dispose(): void { | ||
window.removeEventListener('storage', this.#listener) | ||
this.subscription.dispose() | ||
} | ||
|
||
subscribe(subscriber: Subscriber<unknown>): Unsubscribe { | ||
this.subscription.subscribe(subscriber) | ||
return () => { | ||
this.unsubscribe(subscriber) | ||
} | ||
} | ||
|
||
unsubscribe(subscriber: Subscriber<unknown>): void { | ||
this.subscription.unsubscribe(subscriber) | ||
} | ||
|
||
protected get storageKey(): string { | ||
return `${getPackageName()}:${this.name}` | ||
} | ||
|
||
protected parse(val: unknown): T | null { | ||
protected parse(val: unknown): unknown { | ||
if (typeof val !== 'string') return null | ||
|
||
try { | ||
return (JSON.parse(val as string) as T | null) ?? null | ||
return JSON.parse(val) ?? null | ||
} catch { | ||
return null | ||
} | ||
} | ||
|
||
protected serialize(value: T): string { | ||
protected serialize(value: unknown): string { | ||
return JSON.stringify(value) | ||
} | ||
|
||
protected handleChanged(e: StorageEvent): void { | ||
if (e.key === this.storageKey) this.subscriber(this.parse(e.newValue)) | ||
if (e.key === this.storageKey) this.subscription.notify(this.parse(e.newValue)) | ||
} | ||
} |
This file contains 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,13 +1,23 @@ | ||
import type { IStorageAdapter } from '../types' | ||
import type { IStorageAdapter, Subscriber, Unsubscribe } from '../types' | ||
|
||
export default class MemoryStorageAdapter<T> implements IStorageAdapter<T> { | ||
protected value: T | null = null | ||
export default class MemoryStorageAdapter implements IStorageAdapter { | ||
protected value: unknown | null = null | ||
|
||
read(): T | null { | ||
read(): unknown { | ||
return this.value | ||
} | ||
|
||
write(value: T): void { | ||
write(value: unknown): void { | ||
this.value = value | ||
} | ||
|
||
subscribe(_subscriber: Subscriber<unknown>): Unsubscribe { | ||
return () => { | ||
// Do nothing. | ||
} | ||
} | ||
|
||
unsubscribe(_subscriber: Subscriber<unknown>): void { | ||
// Do nothing. | ||
} | ||
} |
This file contains 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,3 +1,9 @@ | ||
export { default } from './storage' | ||
export { getStorageAdapter as getLazyStorageAdapter } from './utils' | ||
export type { IWritableStorage, IDisposableStorage, IMemorableStorage, ISubscribableStorage } from './types' | ||
export { buildStorageAdapter } from './utils' | ||
export type { | ||
IDisposableStorage, | ||
IMemorableStorage, | ||
ISerializer, | ||
ISubscribableStorage, | ||
IWritableStorage, | ||
} from './types' |
Oops, something went wrong.