@etsoo/shared 1.0.86 → 1.0.90

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