|
@@ -8,6 +8,11 @@ interface StateOptions {
|
|
|
* exists in localStorage, the value will be read and used as the initial value.
|
|
* exists in localStorage, the value will be read and used as the initial value.
|
|
|
*/
|
|
*/
|
|
|
persist?: boolean;
|
|
persist?: boolean;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Use a static localStorage key instead of automatically generating one.
|
|
|
|
|
+ */
|
|
|
|
|
+ key?: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -49,10 +54,15 @@ export class StateManager<T extends Dict, K extends keyof T = keyof T> {
|
|
|
private key: string = '';
|
|
private key: string = '';
|
|
|
|
|
|
|
|
constructor(raw: T, options: StateOptions) {
|
|
constructor(raw: T, options: StateOptions) {
|
|
|
- this.key = this.generateStateKey(raw);
|
|
|
|
|
-
|
|
|
|
|
this.options = options;
|
|
this.options = options;
|
|
|
|
|
|
|
|
|
|
+ // Use static key if defined.
|
|
|
|
|
+ if (typeof this.options.key === 'string') {
|
|
|
|
|
+ this.key = this.options.key;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.key = this.generateStateKey(raw);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (this.options.persist) {
|
|
if (this.options.persist) {
|
|
|
const saved = this.retrieve();
|
|
const saved = this.retrieve();
|
|
|
if (saved !== null) {
|
|
if (saved !== null) {
|