@etsoo/shared 1.0.87 → 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.
@@ -2,6 +2,10 @@
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
@@ -28,10 +32,27 @@ export interface IStorage {
28
32
  * @param persistance Persist the data, false will stop persistance
29
33
  */
30
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;
31
52
  }
32
53
  /**
33
54
  * Storage constructor interface
34
55
  */
35
56
  export interface IStorageConstructor {
36
- new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
57
+ new (globalFields: string[], callback: IStorageInitCallback): IStorage;
37
58
  }
@@ -1,16 +1,25 @@
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
@@ -37,4 +46,15 @@ export declare class WindowStorage implements IStorage {
37
46
  * @param persistance To the persisted data, false will stop persistance
38
47
  */
39
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;
40
60
  }
@@ -15,6 +15,12 @@ class WindowStorage {
15
15
  */
16
16
  constructor(globalFields, callback) {
17
17
  this.globalFields = globalFields;
18
+ /**
19
+ * Instance count field name
20
+ */
21
+ this.instanceCountField = 'EtsooSmartERPInstanceCount';
22
+ // Init instance index
23
+ this._instanceIndex = this.getInstanceCount();
18
24
  if (globalFields.length == 0)
19
25
  return;
20
26
  // Copy global fields to session storage where first item does not exist
@@ -23,13 +29,19 @@ class WindowStorage {
23
29
  if (firsItem)
24
30
  return;
25
31
  globalFields.forEach((field) => {
26
- const data = callback(field, localStorage.getItem(field));
32
+ const data = callback(field, localStorage.getItem(field), this._instanceIndex);
27
33
  if (data == null)
28
34
  sessionStorage.removeItem(field);
29
35
  else
30
36
  sessionStorage.setItem(field, data);
31
37
  });
32
38
  }
39
+ /**
40
+ * Current instance index
41
+ */
42
+ get instanceIndex() {
43
+ return this._instanceIndex;
44
+ }
33
45
  /**
34
46
  * Get data
35
47
  * @param key Key name
@@ -77,5 +89,22 @@ class WindowStorage {
77
89
  StorageUtils_1.StorageUtils.setLocalData(key, data);
78
90
  }
79
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
+ }
80
109
  }
81
110
  exports.WindowStorage = WindowStorage;
@@ -2,6 +2,10 @@
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
@@ -28,10 +32,27 @@ export interface IStorage {
28
32
  * @param persistance Persist the data, false will stop persistance
29
33
  */
30
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;
31
52
  }
32
53
  /**
33
54
  * Storage constructor interface
34
55
  */
35
56
  export interface IStorageConstructor {
36
- new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
57
+ new (globalFields: string[], callback: IStorageInitCallback): IStorage;
37
58
  }
@@ -1,16 +1,25 @@
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
@@ -37,4 +46,15 @@ export declare class WindowStorage implements IStorage {
37
46
  * @param persistance To the persisted data, false will stop persistance
38
47
  */
39
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;
40
60
  }
@@ -12,6 +12,12 @@ export class WindowStorage {
12
12
  */
13
13
  constructor(globalFields, callback) {
14
14
  this.globalFields = globalFields;
15
+ /**
16
+ * Instance count field name
17
+ */
18
+ this.instanceCountField = 'EtsooSmartERPInstanceCount';
19
+ // Init instance index
20
+ this._instanceIndex = this.getInstanceCount();
15
21
  if (globalFields.length == 0)
16
22
  return;
17
23
  // Copy global fields to session storage where first item does not exist
@@ -20,13 +26,19 @@ export class WindowStorage {
20
26
  if (firsItem)
21
27
  return;
22
28
  globalFields.forEach((field) => {
23
- const data = callback(field, localStorage.getItem(field));
29
+ const data = callback(field, localStorage.getItem(field), this._instanceIndex);
24
30
  if (data == null)
25
31
  sessionStorage.removeItem(field);
26
32
  else
27
33
  sessionStorage.setItem(field, data);
28
34
  });
29
35
  }
36
+ /**
37
+ * Current instance index
38
+ */
39
+ get instanceIndex() {
40
+ return this._instanceIndex;
41
+ }
30
42
  /**
31
43
  * Get data
32
44
  * @param key Key name
@@ -74,4 +86,21 @@ export class WindowStorage {
74
86
  StorageUtils.setLocalData(key, data);
75
87
  }
76
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
+ }
77
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.87",
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",
@@ -2,6 +2,11 @@
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
@@ -31,14 +36,31 @@ export interface IStorage {
31
36
  * @param persistance Persist the data, false will stop persistance
32
37
  */
33
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;
34
59
  }
35
60
 
36
61
  /**
37
62
  * Storage constructor interface
38
63
  */
39
64
  export interface IStorageConstructor {
40
- new (
41
- globalFields: string[],
42
- callback: (field: string, data: string | null) => string | null
43
- ): IStorage;
65
+ new (globalFields: string[], callback: IStorageInitCallback): IStorage;
44
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,8 +27,11 @@ 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
  ) {
32
+ // Init instance index
33
+ this._instanceIndex = this.getInstanceCount();
34
+
19
35
  if (globalFields.length == 0) return;
20
36
 
21
37
  // Copy global fields to session storage where first item does not exist
@@ -24,7 +40,11 @@ export class WindowStorage implements IStorage {
24
40
  if (firsItem) return;
25
41
 
26
42
  globalFields.forEach((field) => {
27
- const data = callback(field, localStorage.getItem(field));
43
+ const data = callback(
44
+ field,
45
+ localStorage.getItem(field),
46
+ this._instanceIndex
47
+ );
28
48
  if (data == null) sessionStorage.removeItem(field);
29
49
  else sessionStorage.setItem(field, data);
30
50
  });
@@ -101,4 +121,23 @@ export class WindowStorage implements IStorage {
101
121
  StorageUtils.setLocalData(key, data);
102
122
  }
103
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
+ }
104
143
  }