@etsoo/shared 1.0.90 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,12 @@
1
1
  import { WindowStorage } from '../src/storage/WindowStorage';
2
2
 
3
3
  // Arrange
4
- const storage = new WindowStorage([], (_field, data, _index) => data);
4
+ localStorage.setItem('test', 'test');
5
+ localStorage.setItem('a', 'a');
6
+ const storage = new WindowStorage(['test', 'a'], (field, data, index) => {
7
+ if (index === 0 && field === 'test') return null;
8
+ return data;
9
+ });
5
10
 
6
11
  test('Tests for getInstanceCount', () => {
7
12
  expect(storage.getInstanceCount()).toBe(0);
@@ -14,3 +19,17 @@ test('Tests for updateInstanceCount / getInstanceCount', () => {
14
19
  // Always make sure it starts with 0
15
20
  expect(storage.getInstanceCount()).toBe(0);
16
21
  });
22
+
23
+ test('Tests for getData', () => {
24
+ expect(storage.getData('test')).toBeUndefined();
25
+ expect(storage.getPersistedData('test')).toBe('test');
26
+
27
+ expect(storage.getData('a')).toBe('a');
28
+ expect(storage.getPersistedData('a')).toBe('a');
29
+ });
30
+
31
+ test('Tests for setData', () => {
32
+ storage.setData('test', 'test2');
33
+ expect(storage.getData('test')).toBe('test2');
34
+ expect(storage.getPersistedData('test')).toBe('test2');
35
+ });
@@ -9,29 +9,47 @@ export interface IStorage {
9
9
  /**
10
10
  * Get data
11
11
  * @param key Key name
12
- * @param persistance From the persisted data
13
12
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
13
+ getData<T>(key: string): T | undefined;
15
14
  /**
16
15
  * Get data with default value
17
16
  * @param key Key name
18
17
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
18
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
19
+ getData<T>(key: string, defaultValue: T): T;
20
+ /**
21
+ * Get persisted data
22
+ * @param key Key name
23
+ */
24
+ getPersistedData<T>(key: string): T | undefined;
25
+ /**
26
+ * Get persisted data with default value
27
+ * @param key Key name
28
+ * @param defaultValue Default value
29
+ */
30
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
31
  /**
23
32
  * Get object data
24
33
  * @param key Key name
25
- * @param persistance From the persisted data
26
34
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
35
+ getObject<T extends {}>(key: string): T | undefined;
36
+ /**
37
+ * Get persisted object data
38
+ * @param key Key name
39
+ */
40
+ getPersistedObject<T extends {}>(key: string): T | undefined;
28
41
  /**
29
42
  * Set data
30
43
  * @param key Key name
31
44
  * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
45
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
46
+ setData(key: string, data: unknown): void;
47
+ /**
48
+ * Set persisted data
49
+ * @param key Key name
50
+ * @param data Data, null for removal
51
+ */
52
+ setPersistedData(key: string, data: unknown): void;
35
53
  /**
36
54
  * Get current instance count
37
55
  * @returns Current instance count
@@ -4,7 +4,7 @@ import { IStorage, IStorageInitCallback } from './IStorage';
4
4
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
5
5
  */
6
6
  export declare class WindowStorage implements IStorage {
7
- private globalFields;
7
+ protected persistedFields: string[];
8
8
  /**
9
9
  * Instance count field name
10
10
  */
@@ -16,36 +16,54 @@ export declare class WindowStorage implements IStorage {
16
16
  get instanceIndex(): number;
17
17
  /**
18
18
  * Constructor
19
- * @param globalFields Global fields
19
+ * @param persistedFields Persisted fields
20
20
  * @param callback Field and data callback
21
21
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
22
+ constructor(persistedFields: string[], callback: IStorageInitCallback);
23
23
  /**
24
24
  * Get data
25
25
  * @param key Key name
26
- * @param persistance From the persisted data
27
26
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
27
+ getData<T>(key: string): T | undefined;
29
28
  /**
30
29
  * Get data with default value
31
30
  * @param key Key name
32
31
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
32
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
33
+ getData<T>(key: string, defaultValue: T): T;
34
+ /**
35
+ * Get persisted data
36
+ * @param key Key name
37
+ */
38
+ getPersistedData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get persisted data with default value
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
45
  /**
37
46
  * Get object data
38
47
  * @param key Key name
39
- * @param persistance From the persisted data
40
48
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
49
+ getObject<T extends {}>(key: string): T | undefined;
50
+ /**
51
+ * Get persisted object data
52
+ * @param key Key name
53
+ */
54
+ getPersistedObject<T extends {}>(key: string): T | undefined;
42
55
  /**
43
56
  * Set data
44
57
  * @param key Key name
45
58
  * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
59
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
60
+ setData(key: string, data: unknown): void;
61
+ /**
62
+ * Set persisted data
63
+ * @param key Key name
64
+ * @param data Data, null for removal
65
+ */
66
+ setPersistedData(key: string, data: unknown): void;
49
67
  /**
50
68
  * Get current instance count
51
69
  * @returns Current instance count
@@ -10,11 +10,11 @@ const Utils_1 = require("../Utils");
10
10
  class WindowStorage {
11
11
  /**
12
12
  * Constructor
13
- * @param globalFields Global fields
13
+ * @param persistedFields Persisted fields
14
14
  * @param callback Field and data callback
15
15
  */
16
- constructor(globalFields, callback) {
17
- this.globalFields = globalFields;
16
+ constructor(persistedFields, callback) {
17
+ this.persistedFields = persistedFields;
18
18
  /**
19
19
  * Instance count field name
20
20
  */
@@ -22,7 +22,7 @@ class WindowStorage {
22
22
  // Init instance index
23
23
  this._instanceIndex = this.getInstanceCount();
24
24
  // Copy global fields to session storage
25
- globalFields.forEach((field) => {
25
+ persistedFields.forEach((field) => {
26
26
  const data = callback(field, localStorage.getItem(field), this._instanceIndex);
27
27
  if (data == null)
28
28
  sessionStorage.removeItem(field);
@@ -40,13 +40,24 @@ class WindowStorage {
40
40
  * Get data
41
41
  * @param key Key name
42
42
  * @param defaultValue Default value
43
- * @param persistance From the persisted data
44
43
  */
45
- getData(key, defaultValue, persistance) {
44
+ getData(key, defaultValue) {
46
45
  // Get storage
47
- const data = persistance
48
- ? localStorage.getItem(key)
49
- : sessionStorage.getItem(key);
46
+ const data = sessionStorage.getItem(key);
47
+ // No default value
48
+ if (defaultValue == null)
49
+ return Utils_1.Utils.parseString(data);
50
+ // Return
51
+ return Utils_1.Utils.parseString(data, defaultValue);
52
+ }
53
+ /**
54
+ * Get persisted data
55
+ * @param key Key name
56
+ * @param defaultValue Default value
57
+ */
58
+ getPersistedData(key, defaultValue) {
59
+ // Get storage
60
+ const data = localStorage.getItem(key);
50
61
  // No default value
51
62
  if (defaultValue == null)
52
63
  return Utils_1.Utils.parseString(data);
@@ -56,13 +67,21 @@ class WindowStorage {
56
67
  /**
57
68
  * Get object data
58
69
  * @param key Key name
59
- * @param persistance From the persisted data
60
70
  */
61
- getObject(key, persistance) {
71
+ getObject(key) {
62
72
  // Get storage
63
- const data = persistance
64
- ? localStorage.getItem(key)
65
- : sessionStorage.getItem(key);
73
+ const data = sessionStorage.getItem(key);
74
+ if (data == null)
75
+ return undefined;
76
+ return JSON.parse(data);
77
+ }
78
+ /**
79
+ * Get persisted object data
80
+ * @param key Key name
81
+ */
82
+ getPersistedObject(key) {
83
+ // Get storage
84
+ const data = localStorage.getItem(key);
66
85
  if (data == null)
67
86
  return undefined;
68
87
  return JSON.parse(data);
@@ -71,24 +90,27 @@ class WindowStorage {
71
90
  * Set data
72
91
  * @param key Key name
73
92
  * @param data Data, null for removal
74
- * @param persistance To the persisted data, false will stop persistance
75
93
  */
76
- setData(key, data, persistance) {
77
- if (persistance) {
78
- StorageUtils_1.StorageUtils.setLocalData(key, data);
79
- return;
80
- }
94
+ setData(key, data) {
81
95
  StorageUtils_1.StorageUtils.setSessionData(key, data);
82
- if (persistance !== false && this.globalFields.includes(key)) {
83
- StorageUtils_1.StorageUtils.setLocalData(key, data);
96
+ if (this.persistedFields.includes(key)) {
97
+ this.setPersistedData(key, data);
84
98
  }
85
99
  }
100
+ /**
101
+ * Set persisted data
102
+ * @param key Key name
103
+ * @param data Data, null for removal
104
+ */
105
+ setPersistedData(key, data) {
106
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
107
+ }
86
108
  /**
87
109
  * Get current instance count
88
110
  * @returns Current instance count
89
111
  */
90
112
  getInstanceCount() {
91
- const count = this.getData(this.instanceCountField, 0, true);
113
+ const count = this.getPersistedData(this.instanceCountField, 0);
92
114
  // Make sure starting from 0
93
115
  if (count < 0)
94
116
  return 0;
@@ -101,7 +123,7 @@ class WindowStorage {
101
123
  */
102
124
  updateInstanceCount(removed) {
103
125
  const count = this.getInstanceCount() + (removed ? -1 : 1);
104
- this.setData(this.instanceCountField, count, true);
126
+ this.setPersistedData(this.instanceCountField, count);
105
127
  return count;
106
128
  }
107
129
  }
@@ -9,29 +9,47 @@ export interface IStorage {
9
9
  /**
10
10
  * Get data
11
11
  * @param key Key name
12
- * @param persistance From the persisted data
13
12
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
13
+ getData<T>(key: string): T | undefined;
15
14
  /**
16
15
  * Get data with default value
17
16
  * @param key Key name
18
17
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
18
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
19
+ getData<T>(key: string, defaultValue: T): T;
20
+ /**
21
+ * Get persisted data
22
+ * @param key Key name
23
+ */
24
+ getPersistedData<T>(key: string): T | undefined;
25
+ /**
26
+ * Get persisted data with default value
27
+ * @param key Key name
28
+ * @param defaultValue Default value
29
+ */
30
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
31
  /**
23
32
  * Get object data
24
33
  * @param key Key name
25
- * @param persistance From the persisted data
26
34
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
35
+ getObject<T extends {}>(key: string): T | undefined;
36
+ /**
37
+ * Get persisted object data
38
+ * @param key Key name
39
+ */
40
+ getPersistedObject<T extends {}>(key: string): T | undefined;
28
41
  /**
29
42
  * Set data
30
43
  * @param key Key name
31
44
  * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
45
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
46
+ setData(key: string, data: unknown): void;
47
+ /**
48
+ * Set persisted data
49
+ * @param key Key name
50
+ * @param data Data, null for removal
51
+ */
52
+ setPersistedData(key: string, data: unknown): void;
35
53
  /**
36
54
  * Get current instance count
37
55
  * @returns Current instance count
@@ -4,7 +4,7 @@ import { IStorage, IStorageInitCallback } from './IStorage';
4
4
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
5
5
  */
6
6
  export declare class WindowStorage implements IStorage {
7
- private globalFields;
7
+ protected persistedFields: string[];
8
8
  /**
9
9
  * Instance count field name
10
10
  */
@@ -16,36 +16,54 @@ export declare class WindowStorage implements IStorage {
16
16
  get instanceIndex(): number;
17
17
  /**
18
18
  * Constructor
19
- * @param globalFields Global fields
19
+ * @param persistedFields Persisted fields
20
20
  * @param callback Field and data callback
21
21
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
22
+ constructor(persistedFields: string[], callback: IStorageInitCallback);
23
23
  /**
24
24
  * Get data
25
25
  * @param key Key name
26
- * @param persistance From the persisted data
27
26
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
27
+ getData<T>(key: string): T | undefined;
29
28
  /**
30
29
  * Get data with default value
31
30
  * @param key Key name
32
31
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
32
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
33
+ getData<T>(key: string, defaultValue: T): T;
34
+ /**
35
+ * Get persisted data
36
+ * @param key Key name
37
+ */
38
+ getPersistedData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get persisted data with default value
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
45
  /**
37
46
  * Get object data
38
47
  * @param key Key name
39
- * @param persistance From the persisted data
40
48
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
49
+ getObject<T extends {}>(key: string): T | undefined;
50
+ /**
51
+ * Get persisted object data
52
+ * @param key Key name
53
+ */
54
+ getPersistedObject<T extends {}>(key: string): T | undefined;
42
55
  /**
43
56
  * Set data
44
57
  * @param key Key name
45
58
  * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
59
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
60
+ setData(key: string, data: unknown): void;
61
+ /**
62
+ * Set persisted data
63
+ * @param key Key name
64
+ * @param data Data, null for removal
65
+ */
66
+ setPersistedData(key: string, data: unknown): void;
49
67
  /**
50
68
  * Get current instance count
51
69
  * @returns Current instance count
@@ -7,11 +7,11 @@ import { Utils } from '../Utils';
7
7
  export class WindowStorage {
8
8
  /**
9
9
  * Constructor
10
- * @param globalFields Global fields
10
+ * @param persistedFields Persisted fields
11
11
  * @param callback Field and data callback
12
12
  */
13
- constructor(globalFields, callback) {
14
- this.globalFields = globalFields;
13
+ constructor(persistedFields, callback) {
14
+ this.persistedFields = persistedFields;
15
15
  /**
16
16
  * Instance count field name
17
17
  */
@@ -19,7 +19,7 @@ export class WindowStorage {
19
19
  // Init instance index
20
20
  this._instanceIndex = this.getInstanceCount();
21
21
  // Copy global fields to session storage
22
- globalFields.forEach((field) => {
22
+ persistedFields.forEach((field) => {
23
23
  const data = callback(field, localStorage.getItem(field), this._instanceIndex);
24
24
  if (data == null)
25
25
  sessionStorage.removeItem(field);
@@ -37,13 +37,24 @@ export class WindowStorage {
37
37
  * Get data
38
38
  * @param key Key name
39
39
  * @param defaultValue Default value
40
- * @param persistance From the persisted data
41
40
  */
42
- getData(key, defaultValue, persistance) {
41
+ getData(key, defaultValue) {
43
42
  // Get storage
44
- const data = persistance
45
- ? localStorage.getItem(key)
46
- : sessionStorage.getItem(key);
43
+ const data = sessionStorage.getItem(key);
44
+ // No default value
45
+ if (defaultValue == null)
46
+ return Utils.parseString(data);
47
+ // Return
48
+ return Utils.parseString(data, defaultValue);
49
+ }
50
+ /**
51
+ * Get persisted data
52
+ * @param key Key name
53
+ * @param defaultValue Default value
54
+ */
55
+ getPersistedData(key, defaultValue) {
56
+ // Get storage
57
+ const data = localStorage.getItem(key);
47
58
  // No default value
48
59
  if (defaultValue == null)
49
60
  return Utils.parseString(data);
@@ -53,13 +64,21 @@ export class WindowStorage {
53
64
  /**
54
65
  * Get object data
55
66
  * @param key Key name
56
- * @param persistance From the persisted data
57
67
  */
58
- getObject(key, persistance) {
68
+ getObject(key) {
59
69
  // Get storage
60
- const data = persistance
61
- ? localStorage.getItem(key)
62
- : sessionStorage.getItem(key);
70
+ const data = sessionStorage.getItem(key);
71
+ if (data == null)
72
+ return undefined;
73
+ return JSON.parse(data);
74
+ }
75
+ /**
76
+ * Get persisted object data
77
+ * @param key Key name
78
+ */
79
+ getPersistedObject(key) {
80
+ // Get storage
81
+ const data = localStorage.getItem(key);
63
82
  if (data == null)
64
83
  return undefined;
65
84
  return JSON.parse(data);
@@ -68,24 +87,27 @@ export class WindowStorage {
68
87
  * Set data
69
88
  * @param key Key name
70
89
  * @param data Data, null for removal
71
- * @param persistance To the persisted data, false will stop persistance
72
90
  */
73
- setData(key, data, persistance) {
74
- if (persistance) {
75
- StorageUtils.setLocalData(key, data);
76
- return;
77
- }
91
+ setData(key, data) {
78
92
  StorageUtils.setSessionData(key, data);
79
- if (persistance !== false && this.globalFields.includes(key)) {
80
- StorageUtils.setLocalData(key, data);
93
+ if (this.persistedFields.includes(key)) {
94
+ this.setPersistedData(key, data);
81
95
  }
82
96
  }
97
+ /**
98
+ * Set persisted data
99
+ * @param key Key name
100
+ * @param data Data, null for removal
101
+ */
102
+ setPersistedData(key, data) {
103
+ StorageUtils.setLocalData(key, data);
104
+ }
83
105
  /**
84
106
  * Get current instance count
85
107
  * @returns Current instance count
86
108
  */
87
109
  getInstanceCount() {
88
- const count = this.getData(this.instanceCountField, 0, true);
110
+ const count = this.getPersistedData(this.instanceCountField, 0);
89
111
  // Make sure starting from 0
90
112
  if (count < 0)
91
113
  return 0;
@@ -98,7 +120,7 @@ export class WindowStorage {
98
120
  */
99
121
  updateInstanceCount(removed) {
100
122
  const count = this.getInstanceCount() + (removed ? -1 : 1);
101
- this.setData(this.instanceCountField, count, true);
123
+ this.setPersistedData(this.instanceCountField, count);
102
124
  return count;
103
125
  }
104
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.90",
3
+ "version": "1.0.91",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -10,32 +10,54 @@ export interface IStorage {
10
10
  /**
11
11
  * Get data
12
12
  * @param key Key name
13
- * @param persistance From the persisted data
14
13
  */
15
- getData<T>(key: string, persistance?: boolean): T | undefined;
14
+ getData<T>(key: string): T | undefined;
16
15
 
17
16
  /**
18
17
  * Get data with default value
19
18
  * @param key Key name
20
19
  * @param defaultValue Default value
21
- * @param persistance From the persisted data
22
20
  */
23
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
21
+ getData<T>(key: string, defaultValue: T): T;
22
+
23
+ /**
24
+ * Get persisted data
25
+ * @param key Key name
26
+ */
27
+ getPersistedData<T>(key: string): T | undefined;
28
+
29
+ /**
30
+ * Get persisted data with default value
31
+ * @param key Key name
32
+ * @param defaultValue Default value
33
+ */
34
+ getPersistedData<T>(key: string, defaultValue: T): T;
24
35
 
25
36
  /**
26
37
  * Get object data
27
38
  * @param key Key name
28
- * @param persistance From the persisted data
29
39
  */
30
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
40
+ getObject<T extends {}>(key: string): T | undefined;
41
+
42
+ /**
43
+ * Get persisted object data
44
+ * @param key Key name
45
+ */
46
+ getPersistedObject<T extends {}>(key: string): T | undefined;
31
47
 
32
48
  /**
33
49
  * Set data
34
50
  * @param key Key name
35
51
  * @param data Data, null for removal
36
- * @param persistance Persist the data, false will stop persistance
37
52
  */
38
- setData(key: string, data: unknown, persistance?: boolean): void;
53
+ setData(key: string, data: unknown): void;
54
+
55
+ /**
56
+ * Set persisted data
57
+ * @param key Key name
58
+ * @param data Data, null for removal
59
+ */
60
+ setPersistedData(key: string, data: unknown): void;
39
61
 
40
62
  /**
41
63
  * Get current instance count
@@ -22,18 +22,18 @@ export class WindowStorage implements IStorage {
22
22
 
23
23
  /**
24
24
  * Constructor
25
- * @param globalFields Global fields
25
+ * @param persistedFields Persisted fields
26
26
  * @param callback Field and data callback
27
27
  */
28
28
  constructor(
29
- private globalFields: string[],
29
+ protected persistedFields: string[],
30
30
  callback: IStorageInitCallback
31
31
  ) {
32
32
  // Init instance index
33
33
  this._instanceIndex = this.getInstanceCount();
34
34
 
35
35
  // Copy global fields to session storage
36
- globalFields.forEach((field) => {
36
+ persistedFields.forEach((field) => {
37
37
  const data = callback(
38
38
  field,
39
39
  localStorage.getItem(field),
@@ -47,33 +47,53 @@ export class WindowStorage implements IStorage {
47
47
  /**
48
48
  * Get data
49
49
  * @param key Key name
50
- * @param persistance From the persisted data
51
50
  */
52
- getData<T>(key: string, persistance?: boolean): T | undefined;
51
+ getData<T>(key: string): T | undefined;
53
52
 
54
53
  /**
55
54
  * Get data with default value
56
55
  * @param key Key name
57
56
  * @param defaultValue Default value
58
- * @param persistance From the persisted data
59
57
  */
60
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
58
+ getData<T>(key: string, defaultValue: T): T;
61
59
 
62
60
  /**
63
61
  * Get data
64
62
  * @param key Key name
65
63
  * @param defaultValue Default value
66
- * @param persistance From the persisted data
67
64
  */
68
- getData<T>(
69
- key: string,
70
- defaultValue?: T,
71
- persistance?: boolean
72
- ): T | undefined {
65
+ getData<T>(key: string, defaultValue?: T): T | undefined {
73
66
  // Get storage
74
- const data = persistance
75
- ? localStorage.getItem(key)
76
- : sessionStorage.getItem(key);
67
+ const data = sessionStorage.getItem(key);
68
+
69
+ // No default value
70
+ if (defaultValue == null) return Utils.parseString<T>(data);
71
+
72
+ // Return
73
+ return Utils.parseString<T>(data, defaultValue);
74
+ }
75
+
76
+ /**
77
+ * Get persisted data
78
+ * @param key Key name
79
+ */
80
+ getPersistedData<T>(key: string): T | undefined;
81
+
82
+ /**
83
+ * Get persisted data with default value
84
+ * @param key Key name
85
+ * @param defaultValue Default value
86
+ */
87
+ getPersistedData<T>(key: string, defaultValue: T): T;
88
+
89
+ /**
90
+ * Get persisted data
91
+ * @param key Key name
92
+ * @param defaultValue Default value
93
+ */
94
+ getPersistedData<T>(key: string, defaultValue?: T): T | undefined {
95
+ // Get storage
96
+ const data = localStorage.getItem(key);
77
97
 
78
98
  // No default value
79
99
  if (defaultValue == null) return Utils.parseString<T>(data);
@@ -85,13 +105,23 @@ export class WindowStorage implements IStorage {
85
105
  /**
86
106
  * Get object data
87
107
  * @param key Key name
88
- * @param persistance From the persisted data
89
108
  */
90
- getObject<T extends {}>(key: string, persistance?: boolean) {
109
+ getObject<T extends {}>(key: string) {
110
+ // Get storage
111
+ const data = sessionStorage.getItem(key);
112
+
113
+ if (data == null) return undefined;
114
+
115
+ return <T>JSON.parse(data);
116
+ }
117
+
118
+ /**
119
+ * Get persisted object data
120
+ * @param key Key name
121
+ */
122
+ getPersistedObject<T extends {}>(key: string) {
91
123
  // Get storage
92
- const data = persistance
93
- ? localStorage.getItem(key)
94
- : sessionStorage.getItem(key);
124
+ const data = localStorage.getItem(key);
95
125
 
96
126
  if (data == null) return undefined;
97
127
 
@@ -102,26 +132,29 @@ export class WindowStorage implements IStorage {
102
132
  * Set data
103
133
  * @param key Key name
104
134
  * @param data Data, null for removal
105
- * @param persistance To the persisted data, false will stop persistance
106
135
  */
107
- setData(key: string, data: unknown, persistance?: boolean) {
108
- if (persistance) {
109
- StorageUtils.setLocalData(key, data);
110
- return;
111
- }
112
-
136
+ setData(key: string, data: unknown) {
113
137
  StorageUtils.setSessionData(key, data);
114
- if (persistance !== false && this.globalFields.includes(key)) {
115
- StorageUtils.setLocalData(key, data);
138
+ if (this.persistedFields.includes(key)) {
139
+ this.setPersistedData(key, data);
116
140
  }
117
141
  }
118
142
 
143
+ /**
144
+ * Set persisted data
145
+ * @param key Key name
146
+ * @param data Data, null for removal
147
+ */
148
+ setPersistedData(key: string, data: unknown) {
149
+ StorageUtils.setLocalData(key, data);
150
+ }
151
+
119
152
  /**
120
153
  * Get current instance count
121
154
  * @returns Current instance count
122
155
  */
123
156
  getInstanceCount() {
124
- const count = this.getData(this.instanceCountField, 0, true);
157
+ const count = this.getPersistedData(this.instanceCountField, 0);
125
158
  // Make sure starting from 0
126
159
  if (count < 0) return 0;
127
160
  return count;
@@ -134,7 +167,7 @@ export class WindowStorage implements IStorage {
134
167
  */
135
168
  updateInstanceCount(removed: boolean) {
136
169
  const count = this.getInstanceCount() + (removed ? -1 : 1);
137
- this.setData(this.instanceCountField, count, true);
170
+ this.setPersistedData(this.instanceCountField, count);
138
171
  return count;
139
172
  }
140
173
  }