@etsoo/shared 1.0.84 → 1.0.88

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