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