-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from codex-team/note-settings-style-fix
feat(store): session and persistant stores implemented
- Loading branch information
Showing
9 changed files
with
154 additions
and
54 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
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,82 @@ | ||
/* eslint-disable-next-line boundaries/element-types */ | ||
import { SubscribableStore } from './subscribable'; | ||
|
||
export class PersistantStore<StoreData extends Record<string, unknown>> extends SubscribableStore<StoreData> { | ||
/** | ||
* Keys of the StoreData type | ||
* Used to separate the needed local storage information | ||
*/ | ||
protected readonly keysStored: string[]; | ||
|
||
/** | ||
* Proxy for data stored | ||
*/ | ||
protected data: StoreData; | ||
|
||
/** | ||
* Storage that would retain information when proxy is cleared | ||
*/ | ||
private storage = window.localStorage; | ||
|
||
constructor(keysStored: string[]) { | ||
super(); | ||
this.keysStored = keysStored; | ||
this.data = new Proxy<StoreData>({} as StoreData, this._data); | ||
|
||
/** | ||
* Load data from local store to proxy | ||
*/ | ||
this.loadInitialData(); | ||
}; | ||
|
||
protected get _data(): ProxyHandler<StoreData> { | ||
return { | ||
get: (target, prop) => { | ||
const item = this.storage.getItem(prop as string); | ||
|
||
return item !== null ? JSON.parse(item) : undefined; | ||
}, | ||
|
||
set: (target, prop, value, receiver) => { | ||
/** | ||
* Set new property value for proxy usage | ||
*/ | ||
Reflect.set(target, prop, value, receiver); | ||
|
||
/** | ||
* Set new property value for storage | ||
*/ | ||
this.storage.setItem(prop as string, JSON.stringify(value)); | ||
|
||
this.onDataChange([{ prop: prop as keyof StoreData, | ||
newValue: value }]); | ||
|
||
return true; | ||
}, | ||
|
||
deleteProperty: (target, prop) => { | ||
this.storage.removeItem(prop as string); | ||
|
||
return Reflect.deleteProperty(target, prop); | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Funciton for loading initial data from window.localStorage to proxy object | ||
* Data stored in localStorage would be normalized for proxy | ||
*/ | ||
private loadInitialData(): void { | ||
for (let i = 0; i < this.storage.length; i++) { | ||
const key = this.storage.key(i); | ||
|
||
if (key !== null && this.keysStored.includes(key)) { | ||
const storedValue = this.storage.getItem(key); | ||
|
||
if (storedValue !== null) { | ||
this.data[key as keyof StoreData] = JSON.parse(storedValue); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
/* eslint-disable-next-line boundaries/element-types */ | ||
import { SubscribableStore } from './subscribable'; | ||
|
||
export class SessionStore<StoreData extends Record<string, unknown>> extends SubscribableStore<StoreData> { | ||
/** | ||
* Proxy for data stored in store. | ||
* Used to watch data changes | ||
*/ | ||
protected data = new Proxy<StoreData>({} as StoreData, this._data); | ||
|
||
/** | ||
* Data proxy handler | ||
*/ | ||
protected get _data(): ProxyHandler<StoreData> { | ||
return { | ||
set: (target, prop, value, receiver) => { | ||
Reflect.set(target, prop, value, receiver); | ||
|
||
this.onDataChange([{ prop: prop as keyof StoreData, | ||
newValue: value }]); | ||
|
||
return true; | ||
}, | ||
get(target, prop, receiver) { | ||
return Reflect.get(target, prop, receiver); | ||
}, | ||
|
||
deleteProperty: (target, prop) => { | ||
return Reflect.deleteProperty(target, prop); | ||
}, | ||
}; | ||
} | ||
} |
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,38 +1,39 @@ | ||
/** | ||
* Stores auth data in local storage | ||
*/ | ||
export default class AuthStorage { | ||
/** | ||
* Storage implementation | ||
*/ | ||
private readonly storage: Storage; | ||
import { PersistantStore } from './abstract/persistant'; | ||
|
||
export type AuthStoreData = { | ||
/** | ||
* Creates storage instance | ||
* If user is authorized refresh token will be stored | ||
*/ | ||
refreshToken?: string; | ||
}; | ||
|
||
/** | ||
* Stores auth data in local storage | ||
*/ | ||
export default class AuthStorage extends PersistantStore<AuthStoreData> { | ||
constructor() { | ||
this.storage = window.localStorage; | ||
super(['refreshToken']); | ||
} | ||
|
||
/** | ||
* Returns refresh token | ||
*/ | ||
public getRefreshToken(): string | null { | ||
return this.storage.getItem('refreshToken'); | ||
return this.data.refreshToken === undefined ? null : this.data.refreshToken; | ||
} | ||
|
||
/** | ||
* Save refresh token | ||
* @param refreshToken - refresh token to save | ||
*/ | ||
public setRefreshToken(refreshToken: string): void { | ||
this.storage.setItem('refreshToken', refreshToken); | ||
this.data.refreshToken = refreshToken; | ||
} | ||
|
||
/** | ||
* Removes refresh token | ||
*/ | ||
public removeRefreshToken(): void { | ||
this.storage.removeItem('refreshToken'); | ||
delete this.data.refreshToken; | ||
} | ||
} |
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