@etsoo/shared 1.1.32 → 1.1.33

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.
@@ -7,11 +7,14 @@ test('Tests for history', () => {
7
7
  const history = new LHistory();
8
8
  expect(history.index).toBe(-1);
9
9
  history.pushState(1);
10
+ expect(history.getStatus()).toStrictEqual([false, false]);
10
11
  history.pushState(2);
11
12
  history.pushState(3);
13
+ expect(history.getStatus()).toStrictEqual([true, false]);
12
14
  expect(history.states).toStrictEqual([1, 2, 3]);
13
15
  expect(history.index).toBe(2);
14
16
  history.back();
17
+ expect(history.getStatus()).toStrictEqual([true, true]);
15
18
  history.pushState(4);
16
19
  expect(history.index).toBe(2);
17
20
  expect(history.states).toStrictEqual([1, 2, 4]);
@@ -34,7 +37,7 @@ test('Tests for events', () => {
34
37
  const pushFn = jest.fn();
35
38
  const replaceFn = jest.fn();
36
39
 
37
- const history = new LHistory();
40
+ const history = new LHistory(3);
38
41
 
39
42
  history.on({
40
43
  clear: clearFn,
@@ -68,4 +71,9 @@ test('Tests for events', () => {
68
71
 
69
72
  // Previous handler stopped propagation
70
73
  expect(navigatorFn).toBeCalledTimes(5);
74
+
75
+ history.pushState(3);
76
+ history.pushState(4);
77
+ history.pushState(5);
78
+ expect(history.length).toBe(3);
71
79
  });
@@ -21,6 +21,7 @@ export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistory
21
21
  * ETSOO Extended abstract history class
22
22
  */
23
23
  export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
24
+ readonly maxDepth: number;
24
25
  private _index;
25
26
  /**
26
27
  * States
@@ -38,6 +39,11 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
38
39
  * Get current state
39
40
  */
40
41
  get state(): T | undefined;
42
+ /**
43
+ * Constructor
44
+ * @param maxDepth Max depth of the history
45
+ */
46
+ constructor(maxDepth?: number);
41
47
  /**
42
48
  * Back to the previous state
43
49
  */
@@ -12,8 +12,13 @@ exports.EHistoryEvent = EHistoryEvent;
12
12
  * ETSOO Extended abstract history class
13
13
  */
14
14
  class EHistory extends EventClass_1.EventClass {
15
- constructor() {
16
- super(...arguments);
15
+ /**
16
+ * Constructor
17
+ * @param maxDepth Max depth of the history
18
+ */
19
+ constructor(maxDepth = 20) {
20
+ super();
21
+ this.maxDepth = maxDepth;
17
22
  // Index
18
23
  this._index = -1;
19
24
  /**
@@ -111,6 +116,9 @@ class EHistory extends EventClass_1.EventClass {
111
116
  }
112
117
  this.states.push(state);
113
118
  this._index++;
119
+ if (this.length > this.maxDepth) {
120
+ this.states.shift();
121
+ }
114
122
  this.trigger(this.createEvent('push', this._index));
115
123
  }
116
124
  /**
@@ -21,6 +21,7 @@ export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistory
21
21
  * ETSOO Extended abstract history class
22
22
  */
23
23
  export declare abstract class EHistory<T> extends EventClass<EHistoryEventType, EHistoryEventData> {
24
+ readonly maxDepth: number;
24
25
  private _index;
25
26
  /**
26
27
  * States
@@ -38,6 +39,11 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
38
39
  * Get current state
39
40
  */
40
41
  get state(): T | undefined;
42
+ /**
43
+ * Constructor
44
+ * @param maxDepth Max depth of the history
45
+ */
46
+ constructor(maxDepth?: number);
41
47
  /**
42
48
  * Back to the previous state
43
49
  */
@@ -8,8 +8,13 @@ export class EHistoryEvent extends EventBase {
8
8
  * ETSOO Extended abstract history class
9
9
  */
10
10
  export class EHistory extends EventClass {
11
- constructor() {
12
- super(...arguments);
11
+ /**
12
+ * Constructor
13
+ * @param maxDepth Max depth of the history
14
+ */
15
+ constructor(maxDepth = 20) {
16
+ super();
17
+ this.maxDepth = maxDepth;
13
18
  // Index
14
19
  this._index = -1;
15
20
  /**
@@ -107,6 +112,9 @@ export class EHistory extends EventClass {
107
112
  }
108
113
  this.states.push(state);
109
114
  this._index++;
115
+ if (this.length > this.maxDepth) {
116
+ this.states.shift();
117
+ }
110
118
  this.trigger(this.createEvent('push', this._index));
111
119
  }
112
120
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.32",
3
+ "version": "1.1.33",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -60,6 +60,14 @@ export abstract class EHistory<T> extends EventClass<
60
60
  return this.states[this._index];
61
61
  }
62
62
 
63
+ /**
64
+ * Constructor
65
+ * @param maxDepth Max depth of the history
66
+ */
67
+ constructor(public readonly maxDepth: number = 20) {
68
+ super();
69
+ }
70
+
63
71
  /**
64
72
  * Back to the previous state
65
73
  */
@@ -136,6 +144,11 @@ export abstract class EHistory<T> extends EventClass<
136
144
 
137
145
  this.states.push(state);
138
146
  this._index++;
147
+
148
+ if (this.length > this.maxDepth) {
149
+ this.states.shift();
150
+ }
151
+
139
152
  this.trigger(this.createEvent('push', this._index));
140
153
  }
141
154