@etsoo/shared 1.0.89 → 1.0.93

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.
@@ -3,56 +3,53 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Copy keys to session from persisted source
7
+ * @param keys Keys
8
+ * @param removeSource Remove from the source
7
9
  */
8
- readonly instanceIndex: number;
10
+ copy(keys: string[], removeSource?: boolean): void;
9
11
  /**
10
12
  * Get data
11
13
  * @param key Key name
12
- * @param persistance From the persisted data
13
14
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
15
+ getData<T>(key: string): T | undefined;
15
16
  /**
16
17
  * Get data with default value
17
18
  * @param key Key name
18
19
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
20
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
21
+ getData<T>(key: string, defaultValue: T): T;
22
+ /**
23
+ * Get persisted data
24
+ * @param key Key name
25
+ */
26
+ getPersistedData<T>(key: string): T | undefined;
27
+ /**
28
+ * Get persisted data with default value
29
+ * @param key Key name
30
+ * @param defaultValue Default value
31
+ */
32
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
33
  /**
23
34
  * Get object data
24
35
  * @param key Key name
25
- * @param persistance From the persisted data
26
36
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
37
+ getObject<T extends {}>(key: string): T | undefined;
28
38
  /**
29
- * Set data
39
+ * Get persisted object data
30
40
  * @param key Key name
31
- * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
41
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
42
+ getPersistedObject<T extends {}>(key: string): T | undefined;
35
43
  /**
36
- * Get current instance count
37
- * @returns Current instance count
44
+ * Set data
45
+ * @param key Key name
46
+ * @param data Data, null for removal
38
47
  */
39
- getInstanceCount(): number;
48
+ setData(key: string, data: unknown): void;
40
49
  /**
41
- * Update instance count
42
- * @param removed Is removed?
43
- * @returns Current instance count
50
+ * Set persisted data
51
+ * @param key Key name
52
+ * @param data Data, null for removal
44
53
  */
45
- updateInstanceCount(removed: boolean): number;
46
- }
47
- /**
48
- * Storage init callback
49
- */
50
- export interface IStorageInitCallback {
51
- (field: string, data: string | null, instanceIndex: number): string | null;
52
- }
53
- /**
54
- * Storage constructor interface
55
- */
56
- export interface IStorageConstructor {
57
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
54
+ setPersistedData(key: string, data: unknown): void;
58
55
  }
@@ -1,60 +1,57 @@
1
- import { IStorage, IStorageInitCallback } from './IStorage';
1
+ import { IStorage } from './IStorage';
2
2
  /**
3
3
  * Window storage
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;
8
7
  /**
9
- * Instance count field name
8
+ * Copy keys to session from persisted source
9
+ * @param keys Keys
10
+ * @param removeSource Remove from the source
10
11
  */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
12
+ copy(keys: string[], removeSource?: boolean): void;
13
13
  /**
14
- * Current instance index
14
+ * Get data
15
+ * @param key Key name
15
16
  */
16
- get instanceIndex(): number;
17
+ getData<T>(key: string): T | undefined;
17
18
  /**
18
- * Constructor
19
- * @param globalFields Global fields
20
- * @param callback Field and data callback
19
+ * Get data with default value
20
+ * @param key Key name
21
+ * @param defaultValue Default value
21
22
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
23
+ getData<T>(key: string, defaultValue: T): T;
23
24
  /**
24
- * Get data
25
+ * Get persisted data
25
26
  * @param key Key name
26
- * @param persistance From the persisted data
27
27
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
28
+ getPersistedData<T>(key: string): T | undefined;
29
29
  /**
30
- * Get data with default value
30
+ * Get persisted data with default value
31
31
  * @param key Key name
32
32
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
33
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
34
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
35
  /**
37
36
  * Get object data
38
37
  * @param key Key name
39
- * @param persistance From the persisted data
40
38
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
39
+ getObject<T extends {}>(key: string): T | undefined;
42
40
  /**
43
- * Set data
41
+ * Get persisted object data
44
42
  * @param key Key name
45
- * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
43
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
44
+ getPersistedObject<T extends {}>(key: string): T | undefined;
49
45
  /**
50
- * Get current instance count
51
- * @returns Current instance count
46
+ * Set data
47
+ * @param key Key name
48
+ * @param data Data, null for removal
52
49
  */
53
- getInstanceCount(): number;
50
+ setData(key: string, data: unknown): void;
54
51
  /**
55
- * Update instance count
56
- * @param removed Is removed?
57
- * @returns Current instance count
52
+ * Set persisted data
53
+ * @param key Key name
54
+ * @param data Data, null for removal
58
55
  */
59
- updateInstanceCount(removed: boolean): number;
56
+ setPersistedData(key: string, data: unknown): void;
60
57
  }
@@ -9,50 +9,39 @@ const Utils_1 = require("../Utils");
9
9
  */
10
10
  class WindowStorage {
11
11
  /**
12
- * Constructor
13
- * @param globalFields Global fields
14
- * @param callback Field and data callback
12
+ * Copy keys to session from persisted source
13
+ * @param keys Keys
14
+ * @param removeSource Remove from the source
15
15
  */
16
- constructor(globalFields, callback) {
17
- this.globalFields = globalFields;
18
- /**
19
- * Instance count field name
20
- */
21
- this.instanceCountField = 'EtsooSmartERPInstanceCount';
22
- // Init instance index
23
- this._instanceIndex = this.getInstanceCount();
24
- if (globalFields.length == 0)
25
- return;
26
- // Copy global fields to session storage where first item does not exist
27
- // Duplicate browser tab would copy the session storage
28
- const firsItem = sessionStorage.getItem(globalFields[0]);
29
- if (firsItem)
30
- return;
31
- globalFields.forEach((field) => {
32
- const data = callback(field, localStorage.getItem(field), this._instanceIndex);
33
- if (data == null)
34
- sessionStorage.removeItem(field);
35
- else
36
- sessionStorage.setItem(field, data);
16
+ copy(keys, removeSource) {
17
+ keys.forEach((key) => {
18
+ this.setData(key, this.getPersistedData(key));
19
+ if (removeSource)
20
+ this.setPersistedData(key, undefined);
37
21
  });
38
22
  }
39
23
  /**
40
- * Current instance index
24
+ * Get data
25
+ * @param key Key name
26
+ * @param defaultValue Default value
41
27
  */
42
- get instanceIndex() {
43
- return this._instanceIndex;
28
+ getData(key, defaultValue) {
29
+ // Get storage
30
+ const data = sessionStorage.getItem(key);
31
+ // No default value
32
+ if (defaultValue == null)
33
+ return Utils_1.Utils.parseString(data);
34
+ // Return
35
+ return Utils_1.Utils.parseString(data, defaultValue);
44
36
  }
45
37
  /**
46
- * Get data
38
+ * Get persisted data
47
39
  * @param key Key name
48
40
  * @param defaultValue Default value
49
- * @param persistance From the persisted data
50
41
  */
51
- getData(key, defaultValue, persistance) {
42
+ getPersistedData(key, defaultValue) {
52
43
  // Get storage
53
- const data = persistance
54
- ? localStorage.getItem(key)
55
- : sessionStorage.getItem(key);
44
+ const data = localStorage.getItem(key);
56
45
  // No default value
57
46
  if (defaultValue == null)
58
47
  return Utils_1.Utils.parseString(data);
@@ -62,53 +51,40 @@ class WindowStorage {
62
51
  /**
63
52
  * Get object data
64
53
  * @param key Key name
65
- * @param persistance From the persisted data
66
54
  */
67
- getObject(key, persistance) {
55
+ getObject(key) {
68
56
  // Get storage
69
- const data = persistance
70
- ? localStorage.getItem(key)
71
- : sessionStorage.getItem(key);
57
+ const data = sessionStorage.getItem(key);
72
58
  if (data == null)
73
59
  return undefined;
74
60
  return JSON.parse(data);
75
61
  }
76
62
  /**
77
- * Set data
63
+ * Get persisted object data
78
64
  * @param key Key name
79
- * @param data Data, null for removal
80
- * @param persistance To the persisted data, false will stop persistance
81
65
  */
82
- setData(key, data, persistance) {
83
- if (persistance) {
84
- StorageUtils_1.StorageUtils.setLocalData(key, data);
85
- return;
86
- }
87
- StorageUtils_1.StorageUtils.setSessionData(key, data);
88
- if (persistance !== false && this.globalFields.includes(key)) {
89
- StorageUtils_1.StorageUtils.setLocalData(key, data);
90
- }
66
+ getPersistedObject(key) {
67
+ // Get storage
68
+ const data = localStorage.getItem(key);
69
+ if (data == null)
70
+ return undefined;
71
+ return JSON.parse(data);
91
72
  }
92
73
  /**
93
- * Get current instance count
94
- * @returns Current instance count
74
+ * Set data
75
+ * @param key Key name
76
+ * @param data Data, null for removal
95
77
  */
96
- getInstanceCount() {
97
- const count = this.getData(this.instanceCountField, 0, true);
98
- // Make sure starting from 0
99
- if (count < 0)
100
- return 0;
101
- return count;
78
+ setData(key, data) {
79
+ StorageUtils_1.StorageUtils.setSessionData(key, data);
102
80
  }
103
81
  /**
104
- * Update instance count
105
- * @param removed Is removed?
106
- * @returns Current instance count
82
+ * Set persisted data
83
+ * @param key Key name
84
+ * @param data Data, null for removal
107
85
  */
108
- updateInstanceCount(removed) {
109
- const count = this.getInstanceCount() + (removed ? -1 : 1);
110
- this.setData(this.instanceCountField, count, true);
111
- return count;
86
+ setPersistedData(key, data) {
87
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
112
88
  }
113
89
  }
114
90
  exports.WindowStorage = WindowStorage;
@@ -3,56 +3,53 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Copy keys to session from persisted source
7
+ * @param keys Keys
8
+ * @param removeSource Remove from the source
7
9
  */
8
- readonly instanceIndex: number;
10
+ copy(keys: string[], removeSource?: boolean): void;
9
11
  /**
10
12
  * Get data
11
13
  * @param key Key name
12
- * @param persistance From the persisted data
13
14
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
15
+ getData<T>(key: string): T | undefined;
15
16
  /**
16
17
  * Get data with default value
17
18
  * @param key Key name
18
19
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
20
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
21
+ getData<T>(key: string, defaultValue: T): T;
22
+ /**
23
+ * Get persisted data
24
+ * @param key Key name
25
+ */
26
+ getPersistedData<T>(key: string): T | undefined;
27
+ /**
28
+ * Get persisted data with default value
29
+ * @param key Key name
30
+ * @param defaultValue Default value
31
+ */
32
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
33
  /**
23
34
  * Get object data
24
35
  * @param key Key name
25
- * @param persistance From the persisted data
26
36
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
37
+ getObject<T extends {}>(key: string): T | undefined;
28
38
  /**
29
- * Set data
39
+ * Get persisted object data
30
40
  * @param key Key name
31
- * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
41
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
42
+ getPersistedObject<T extends {}>(key: string): T | undefined;
35
43
  /**
36
- * Get current instance count
37
- * @returns Current instance count
44
+ * Set data
45
+ * @param key Key name
46
+ * @param data Data, null for removal
38
47
  */
39
- getInstanceCount(): number;
48
+ setData(key: string, data: unknown): void;
40
49
  /**
41
- * Update instance count
42
- * @param removed Is removed?
43
- * @returns Current instance count
50
+ * Set persisted data
51
+ * @param key Key name
52
+ * @param data Data, null for removal
44
53
  */
45
- updateInstanceCount(removed: boolean): number;
46
- }
47
- /**
48
- * Storage init callback
49
- */
50
- export interface IStorageInitCallback {
51
- (field: string, data: string | null, instanceIndex: number): string | null;
52
- }
53
- /**
54
- * Storage constructor interface
55
- */
56
- export interface IStorageConstructor {
57
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
54
+ setPersistedData(key: string, data: unknown): void;
58
55
  }
@@ -1,60 +1,57 @@
1
- import { IStorage, IStorageInitCallback } from './IStorage';
1
+ import { IStorage } from './IStorage';
2
2
  /**
3
3
  * Window storage
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;
8
7
  /**
9
- * Instance count field name
8
+ * Copy keys to session from persisted source
9
+ * @param keys Keys
10
+ * @param removeSource Remove from the source
10
11
  */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
12
+ copy(keys: string[], removeSource?: boolean): void;
13
13
  /**
14
- * Current instance index
14
+ * Get data
15
+ * @param key Key name
15
16
  */
16
- get instanceIndex(): number;
17
+ getData<T>(key: string): T | undefined;
17
18
  /**
18
- * Constructor
19
- * @param globalFields Global fields
20
- * @param callback Field and data callback
19
+ * Get data with default value
20
+ * @param key Key name
21
+ * @param defaultValue Default value
21
22
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
23
+ getData<T>(key: string, defaultValue: T): T;
23
24
  /**
24
- * Get data
25
+ * Get persisted data
25
26
  * @param key Key name
26
- * @param persistance From the persisted data
27
27
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
28
+ getPersistedData<T>(key: string): T | undefined;
29
29
  /**
30
- * Get data with default value
30
+ * Get persisted data with default value
31
31
  * @param key Key name
32
32
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
33
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
34
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
35
  /**
37
36
  * Get object data
38
37
  * @param key Key name
39
- * @param persistance From the persisted data
40
38
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
39
+ getObject<T extends {}>(key: string): T | undefined;
42
40
  /**
43
- * Set data
41
+ * Get persisted object data
44
42
  * @param key Key name
45
- * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
43
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
44
+ getPersistedObject<T extends {}>(key: string): T | undefined;
49
45
  /**
50
- * Get current instance count
51
- * @returns Current instance count
46
+ * Set data
47
+ * @param key Key name
48
+ * @param data Data, null for removal
52
49
  */
53
- getInstanceCount(): number;
50
+ setData(key: string, data: unknown): void;
54
51
  /**
55
- * Update instance count
56
- * @param removed Is removed?
57
- * @returns Current instance count
52
+ * Set persisted data
53
+ * @param key Key name
54
+ * @param data Data, null for removal
58
55
  */
59
- updateInstanceCount(removed: boolean): number;
56
+ setPersistedData(key: string, data: unknown): void;
60
57
  }
@@ -6,50 +6,39 @@ import { Utils } from '../Utils';
6
6
  */
7
7
  export class WindowStorage {
8
8
  /**
9
- * Constructor
10
- * @param globalFields Global fields
11
- * @param callback Field and data callback
9
+ * Copy keys to session from persisted source
10
+ * @param keys Keys
11
+ * @param removeSource Remove from the source
12
12
  */
13
- constructor(globalFields, callback) {
14
- this.globalFields = globalFields;
15
- /**
16
- * Instance count field name
17
- */
18
- this.instanceCountField = 'EtsooSmartERPInstanceCount';
19
- // Init instance index
20
- this._instanceIndex = this.getInstanceCount();
21
- if (globalFields.length == 0)
22
- return;
23
- // Copy global fields to session storage where first item does not exist
24
- // Duplicate browser tab would copy the session storage
25
- const firsItem = sessionStorage.getItem(globalFields[0]);
26
- if (firsItem)
27
- return;
28
- globalFields.forEach((field) => {
29
- const data = callback(field, localStorage.getItem(field), this._instanceIndex);
30
- if (data == null)
31
- sessionStorage.removeItem(field);
32
- else
33
- sessionStorage.setItem(field, data);
13
+ copy(keys, removeSource) {
14
+ keys.forEach((key) => {
15
+ this.setData(key, this.getPersistedData(key));
16
+ if (removeSource)
17
+ this.setPersistedData(key, undefined);
34
18
  });
35
19
  }
36
20
  /**
37
- * Current instance index
21
+ * Get data
22
+ * @param key Key name
23
+ * @param defaultValue Default value
38
24
  */
39
- get instanceIndex() {
40
- return this._instanceIndex;
25
+ getData(key, defaultValue) {
26
+ // Get storage
27
+ const data = sessionStorage.getItem(key);
28
+ // No default value
29
+ if (defaultValue == null)
30
+ return Utils.parseString(data);
31
+ // Return
32
+ return Utils.parseString(data, defaultValue);
41
33
  }
42
34
  /**
43
- * Get data
35
+ * Get persisted data
44
36
  * @param key Key name
45
37
  * @param defaultValue Default value
46
- * @param persistance From the persisted data
47
38
  */
48
- getData(key, defaultValue, persistance) {
39
+ getPersistedData(key, defaultValue) {
49
40
  // Get storage
50
- const data = persistance
51
- ? localStorage.getItem(key)
52
- : sessionStorage.getItem(key);
41
+ const data = localStorage.getItem(key);
53
42
  // No default value
54
43
  if (defaultValue == null)
55
44
  return Utils.parseString(data);
@@ -59,52 +48,39 @@ export class WindowStorage {
59
48
  /**
60
49
  * Get object data
61
50
  * @param key Key name
62
- * @param persistance From the persisted data
63
51
  */
64
- getObject(key, persistance) {
52
+ getObject(key) {
65
53
  // Get storage
66
- const data = persistance
67
- ? localStorage.getItem(key)
68
- : sessionStorage.getItem(key);
54
+ const data = sessionStorage.getItem(key);
69
55
  if (data == null)
70
56
  return undefined;
71
57
  return JSON.parse(data);
72
58
  }
73
59
  /**
74
- * Set data
60
+ * Get persisted object data
75
61
  * @param key Key name
76
- * @param data Data, null for removal
77
- * @param persistance To the persisted data, false will stop persistance
78
62
  */
79
- setData(key, data, persistance) {
80
- if (persistance) {
81
- StorageUtils.setLocalData(key, data);
82
- return;
83
- }
84
- StorageUtils.setSessionData(key, data);
85
- if (persistance !== false && this.globalFields.includes(key)) {
86
- StorageUtils.setLocalData(key, data);
87
- }
63
+ getPersistedObject(key) {
64
+ // Get storage
65
+ const data = localStorage.getItem(key);
66
+ if (data == null)
67
+ return undefined;
68
+ return JSON.parse(data);
88
69
  }
89
70
  /**
90
- * Get current instance count
91
- * @returns Current instance count
71
+ * Set data
72
+ * @param key Key name
73
+ * @param data Data, null for removal
92
74
  */
93
- getInstanceCount() {
94
- const count = this.getData(this.instanceCountField, 0, true);
95
- // Make sure starting from 0
96
- if (count < 0)
97
- return 0;
98
- return count;
75
+ setData(key, data) {
76
+ StorageUtils.setSessionData(key, data);
99
77
  }
100
78
  /**
101
- * Update instance count
102
- * @param removed Is removed?
103
- * @returns Current instance count
79
+ * Set persisted data
80
+ * @param key Key name
81
+ * @param data Data, null for removal
104
82
  */
105
- updateInstanceCount(removed) {
106
- const count = this.getInstanceCount() + (removed ? -1 : 1);
107
- this.setData(this.instanceCountField, count, true);
108
- return count;
83
+ setPersistedData(key, data) {
84
+ StorageUtils.setLocalData(key, data);
109
85
  }
110
86
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.89",
3
+ "version": "1.0.93",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -3,64 +3,61 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Copy keys to session from persisted source
7
+ * @param keys Keys
8
+ * @param removeSource Remove from the source
7
9
  */
8
- readonly instanceIndex: number;
10
+ copy(keys: string[], removeSource?: boolean): void;
9
11
 
10
12
  /**
11
13
  * Get data
12
14
  * @param key Key name
13
- * @param persistance From the persisted data
14
15
  */
15
- getData<T>(key: string, persistance?: boolean): T | undefined;
16
+ getData<T>(key: string): T | undefined;
16
17
 
17
18
  /**
18
19
  * Get data with default value
19
20
  * @param key Key name
20
21
  * @param defaultValue Default value
21
- * @param persistance From the persisted data
22
22
  */
23
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
23
+ getData<T>(key: string, defaultValue: T): T;
24
24
 
25
25
  /**
26
- * Get object data
26
+ * Get persisted data
27
27
  * @param key Key name
28
- * @param persistance From the persisted data
29
28
  */
30
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
29
+ getPersistedData<T>(key: string): T | undefined;
31
30
 
32
31
  /**
33
- * Set data
32
+ * Get persisted data with default value
34
33
  * @param key Key name
35
- * @param data Data, null for removal
36
- * @param persistance Persist the data, false will stop persistance
34
+ * @param defaultValue Default value
37
35
  */
38
- setData(key: string, data: unknown, persistance?: boolean): void;
36
+ getPersistedData<T>(key: string, defaultValue: T): T;
39
37
 
40
38
  /**
41
- * Get current instance count
42
- * @returns Current instance count
39
+ * Get object data
40
+ * @param key Key name
43
41
  */
44
- getInstanceCount(): number;
42
+ getObject<T extends {}>(key: string): T | undefined;
45
43
 
46
44
  /**
47
- * Update instance count
48
- * @param removed Is removed?
49
- * @returns Current instance count
45
+ * Get persisted object data
46
+ * @param key Key name
50
47
  */
51
- updateInstanceCount(removed: boolean): number;
52
- }
48
+ getPersistedObject<T extends {}>(key: string): T | undefined;
53
49
 
54
- /**
55
- * Storage init callback
56
- */
57
- export interface IStorageInitCallback {
58
- (field: string, data: string | null, instanceIndex: number): string | null;
59
- }
50
+ /**
51
+ * Set data
52
+ * @param key Key name
53
+ * @param data Data, null for removal
54
+ */
55
+ setData(key: string, data: unknown): void;
60
56
 
61
- /**
62
- * Storage constructor interface
63
- */
64
- export interface IStorageConstructor {
65
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
57
+ /**
58
+ * Set persisted data
59
+ * @param key Key name
60
+ * @param data Data, null for removal
61
+ */
62
+ setPersistedData(key: string, data: unknown): void;
66
63
  }
@@ -1,6 +1,6 @@
1
1
  import { StorageUtils } from '../StorageUtils';
2
2
  import { Utils } from '../Utils';
3
- import { IStorage, IStorageInitCallback } from './IStorage';
3
+ import { IStorage } from './IStorage';
4
4
 
5
5
  /**
6
6
  * Window storage
@@ -8,78 +8,67 @@ import { IStorage, IStorageInitCallback } from './IStorage';
8
8
  */
9
9
  export class WindowStorage implements IStorage {
10
10
  /**
11
- * Instance count field name
11
+ * Copy keys to session from persisted source
12
+ * @param keys Keys
13
+ * @param removeSource Remove from the source
12
14
  */
13
- private readonly instanceCountField = 'EtsooSmartERPInstanceCount';
15
+ copy(keys: string[], removeSource?: boolean) {
16
+ keys.forEach((key) => {
17
+ this.setData(key, this.getPersistedData(key));
18
+ if (removeSource) this.setPersistedData(key, undefined);
19
+ });
20
+ }
14
21
 
15
- private _instanceIndex: number;
16
22
  /**
17
- * Current instance index
23
+ * Get data
24
+ * @param key Key name
18
25
  */
19
- get instanceIndex() {
20
- return this._instanceIndex;
21
- }
26
+ getData<T>(key: string): T | undefined;
22
27
 
23
28
  /**
24
- * Constructor
25
- * @param globalFields Global fields
26
- * @param callback Field and data callback
29
+ * Get data with default value
30
+ * @param key Key name
31
+ * @param defaultValue Default value
27
32
  */
28
- constructor(
29
- private globalFields: string[],
30
- callback: IStorageInitCallback
31
- ) {
32
- // Init instance index
33
- this._instanceIndex = this.getInstanceCount();
34
-
35
- if (globalFields.length == 0) return;
36
-
37
- // Copy global fields to session storage where first item does not exist
38
- // Duplicate browser tab would copy the session storage
39
- const firsItem = sessionStorage.getItem(globalFields[0]);
40
- if (firsItem) return;
41
-
42
- globalFields.forEach((field) => {
43
- const data = callback(
44
- field,
45
- localStorage.getItem(field),
46
- this._instanceIndex
47
- );
48
- if (data == null) sessionStorage.removeItem(field);
49
- else sessionStorage.setItem(field, data);
50
- });
51
- }
33
+ getData<T>(key: string, defaultValue: T): T;
52
34
 
53
35
  /**
54
36
  * Get data
55
37
  * @param key Key name
56
- * @param persistance From the persisted data
38
+ * @param defaultValue Default value
39
+ */
40
+ getData<T>(key: string, defaultValue?: T): T | undefined {
41
+ // Get storage
42
+ const data = sessionStorage.getItem(key);
43
+
44
+ // No default value
45
+ if (defaultValue == null) return Utils.parseString<T>(data);
46
+
47
+ // Return
48
+ return Utils.parseString<T>(data, defaultValue);
49
+ }
50
+
51
+ /**
52
+ * Get persisted data
53
+ * @param key Key name
57
54
  */
58
- getData<T>(key: string, persistance?: boolean): T | undefined;
55
+ getPersistedData<T>(key: string): T | undefined;
59
56
 
60
57
  /**
61
- * Get data with default value
58
+ * Get persisted data with default value
62
59
  * @param key Key name
63
60
  * @param defaultValue Default value
64
- * @param persistance From the persisted data
65
61
  */
66
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
62
+ getPersistedData<T>(key: string, defaultValue: T): T;
67
63
 
68
64
  /**
69
- * Get data
65
+ * Get persisted data
70
66
  * @param key Key name
71
67
  * @param defaultValue Default value
72
- * @param persistance From the persisted data
73
68
  */
74
- getData<T>(
75
- key: string,
76
- defaultValue?: T,
77
- persistance?: boolean
78
- ): T | undefined {
69
+ getPersistedData<T>(key: string, defaultValue?: T): T | undefined {
79
70
  // Get storage
80
- const data = persistance
81
- ? localStorage.getItem(key)
82
- : sessionStorage.getItem(key);
71
+ const data = localStorage.getItem(key);
83
72
 
84
73
  // No default value
85
74
  if (defaultValue == null) return Utils.parseString<T>(data);
@@ -91,13 +80,10 @@ export class WindowStorage implements IStorage {
91
80
  /**
92
81
  * Get object data
93
82
  * @param key Key name
94
- * @param persistance From the persisted data
95
83
  */
96
- getObject<T extends {}>(key: string, persistance?: boolean) {
84
+ getObject<T extends {}>(key: string) {
97
85
  // Get storage
98
- const data = persistance
99
- ? localStorage.getItem(key)
100
- : sessionStorage.getItem(key);
86
+ const data = sessionStorage.getItem(key);
101
87
 
102
88
  if (data == null) return undefined;
103
89
 
@@ -105,42 +91,33 @@ export class WindowStorage implements IStorage {
105
91
  }
106
92
 
107
93
  /**
108
- * Set data
94
+ * Get persisted object data
109
95
  * @param key Key name
110
- * @param data Data, null for removal
111
- * @param persistance To the persisted data, false will stop persistance
112
96
  */
113
- setData(key: string, data: unknown, persistance?: boolean) {
114
- if (persistance) {
115
- StorageUtils.setLocalData(key, data);
116
- return;
117
- }
97
+ getPersistedObject<T extends {}>(key: string) {
98
+ // Get storage
99
+ const data = localStorage.getItem(key);
118
100
 
119
- StorageUtils.setSessionData(key, data);
120
- if (persistance !== false && this.globalFields.includes(key)) {
121
- StorageUtils.setLocalData(key, data);
122
- }
101
+ if (data == null) return undefined;
102
+
103
+ return <T>JSON.parse(data);
123
104
  }
124
105
 
125
106
  /**
126
- * Get current instance count
127
- * @returns Current instance count
107
+ * Set data
108
+ * @param key Key name
109
+ * @param data Data, null for removal
128
110
  */
129
- getInstanceCount() {
130
- const count = this.getData(this.instanceCountField, 0, true);
131
- // Make sure starting from 0
132
- if (count < 0) return 0;
133
- return count;
111
+ setData(key: string, data: unknown) {
112
+ StorageUtils.setSessionData(key, data);
134
113
  }
135
114
 
136
115
  /**
137
- * Update instance count
138
- * @param removed Is removed?
139
- * @returns Current instance count
116
+ * Set persisted data
117
+ * @param key Key name
118
+ * @param data Data, null for removal
140
119
  */
141
- updateInstanceCount(removed: boolean) {
142
- const count = this.getInstanceCount() + (removed ? -1 : 1);
143
- this.setData(this.instanceCountField, count, true);
144
- return count;
120
+ setPersistedData(key: string, data: unknown) {
121
+ StorageUtils.setLocalData(key, data);
145
122
  }
146
123
  }
@@ -1,16 +0,0 @@
1
- import { WindowStorage } from '../src/storage/WindowStorage';
2
-
3
- // Arrange
4
- const storage = new WindowStorage([], (_field, data, _index) => data);
5
-
6
- test('Tests for getInstanceCount', () => {
7
- expect(storage.getInstanceCount()).toBe(0);
8
- });
9
-
10
- test('Tests for updateInstanceCount / getInstanceCount', () => {
11
- // Current index -1
12
- storage.updateInstanceCount(true);
13
-
14
- // Always make sure it starts with 0
15
- expect(storage.getInstanceCount()).toBe(0);
16
- });