@etsoo/shared 1.0.88 → 1.0.92

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