@etsoo/shared 1.1.33 → 1.1.34
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.
- package/__tests__/EHistory.ts +2 -2
- package/lib/cjs/types/EHistory.d.ts +24 -10
- package/lib/cjs/types/EHistory.js +14 -2
- package/lib/cjs/types/EventClass.d.ts +15 -24
- package/lib/cjs/types/EventClass.js +2 -0
- package/lib/mjs/types/EHistory.d.ts +24 -10
- package/lib/mjs/types/EHistory.js +12 -1
- package/lib/mjs/types/EventClass.d.ts +15 -24
- package/lib/mjs/types/EventClass.js +2 -0
- package/package.json +1 -1
- package/src/types/EHistory.ts +36 -15
- package/src/types/EventClass.ts +42 -37
package/__tests__/EHistory.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EHistory,
|
|
1
|
+
import { EHistory, EHistoryNavigateEvent } from '../src/types/EHistory';
|
|
2
2
|
|
|
3
3
|
// Extended for tests
|
|
4
4
|
class LHistory extends EHistory<number> {}
|
|
@@ -30,7 +30,7 @@ test('Tests for history', () => {
|
|
|
30
30
|
|
|
31
31
|
test('Tests for events', () => {
|
|
32
32
|
const navigatorFn = jest.fn();
|
|
33
|
-
const navigatorStopFn = jest.fn((event:
|
|
33
|
+
const navigatorStopFn = jest.fn((event: EHistoryNavigateEvent) => {
|
|
34
34
|
event.stopImmediatePropagation();
|
|
35
35
|
});
|
|
36
36
|
const clearFn = jest.fn();
|
|
@@ -1,26 +1,38 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
|
-
|
|
3
|
-
* ETSOO Extended history event type
|
|
4
|
-
*/
|
|
5
|
-
export declare type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
6
|
-
/**
|
|
7
|
-
* ETSOO Extended history event data
|
|
8
|
-
*/
|
|
9
|
-
export interface EHistoryEventData {
|
|
2
|
+
interface EHistoryEventData {
|
|
10
3
|
/**
|
|
11
4
|
* Current index
|
|
12
5
|
*/
|
|
13
6
|
index: number;
|
|
14
7
|
}
|
|
8
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
9
|
+
/**
|
|
10
|
+
* Delta
|
|
11
|
+
*/
|
|
12
|
+
delta: number;
|
|
13
|
+
}
|
|
14
|
+
declare type EHistoryEventDef = {
|
|
15
|
+
clear: EHistoryEventData;
|
|
16
|
+
navigate: EHistoryNavigateEventData;
|
|
17
|
+
push: EHistoryEventData;
|
|
18
|
+
replace: EHistoryEventData;
|
|
19
|
+
};
|
|
20
|
+
declare type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
15
21
|
/**
|
|
16
22
|
* ETSOO Extended history event
|
|
17
23
|
*/
|
|
18
24
|
export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* ETSOO Extended history navigate event
|
|
28
|
+
*/
|
|
29
|
+
export declare class EHistoryNavigateEvent extends EventBase<'navigate', EHistoryNavigateEventData> {
|
|
30
|
+
constructor(target: {}, data: EHistoryNavigateEventData);
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* ETSOO Extended abstract history class
|
|
22
34
|
*/
|
|
23
|
-
export declare abstract class EHistory<T> extends EventClass<
|
|
35
|
+
export declare abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
24
36
|
readonly maxDepth: number;
|
|
25
37
|
private _index;
|
|
26
38
|
/**
|
|
@@ -57,7 +69,8 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
57
69
|
* @param type Type
|
|
58
70
|
* @param index Current index
|
|
59
71
|
*/
|
|
60
|
-
protected createEvent(type:
|
|
72
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number): EHistoryEvent;
|
|
73
|
+
protected createNavigateEvent(index: number, delta: number): EHistoryNavigateEvent;
|
|
61
74
|
/**
|
|
62
75
|
* Forward to the next state
|
|
63
76
|
*/
|
|
@@ -82,3 +95,4 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
82
95
|
*/
|
|
83
96
|
replaceState(state: T): void;
|
|
84
97
|
}
|
|
98
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EHistory = exports.EHistoryEvent = void 0;
|
|
3
|
+
exports.EHistory = exports.EHistoryNavigateEvent = exports.EHistoryEvent = void 0;
|
|
4
4
|
const EventClass_1 = require("./EventClass");
|
|
5
5
|
/**
|
|
6
6
|
* ETSOO Extended history event
|
|
@@ -8,6 +8,15 @@ const EventClass_1 = require("./EventClass");
|
|
|
8
8
|
class EHistoryEvent extends EventClass_1.EventBase {
|
|
9
9
|
}
|
|
10
10
|
exports.EHistoryEvent = EHistoryEvent;
|
|
11
|
+
/**
|
|
12
|
+
* ETSOO Extended history navigate event
|
|
13
|
+
*/
|
|
14
|
+
class EHistoryNavigateEvent extends EventClass_1.EventBase {
|
|
15
|
+
constructor(target, data) {
|
|
16
|
+
super(target, 'navigate', data);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.EHistoryNavigateEvent = EHistoryNavigateEvent;
|
|
11
20
|
/**
|
|
12
21
|
* ETSOO Extended abstract history class
|
|
13
22
|
*/
|
|
@@ -69,6 +78,9 @@ class EHistory extends EventClass_1.EventClass {
|
|
|
69
78
|
createEvent(type, index) {
|
|
70
79
|
return new EHistoryEvent(this, type, { index });
|
|
71
80
|
}
|
|
81
|
+
createNavigateEvent(index, delta) {
|
|
82
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
83
|
+
}
|
|
72
84
|
/**
|
|
73
85
|
* Forward to the next state
|
|
74
86
|
*/
|
|
@@ -103,7 +115,7 @@ class EHistory extends EventClass_1.EventClass {
|
|
|
103
115
|
// Update the index
|
|
104
116
|
this._index = newIndex;
|
|
105
117
|
// Trigger event
|
|
106
|
-
this.trigger(this.
|
|
118
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
107
119
|
}
|
|
108
120
|
/**
|
|
109
121
|
* Adds an entry to the history stack
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export declare abstract class EventBase<T
|
|
7
|
-
readonly target:
|
|
6
|
+
export declare abstract class EventBase<T, D> {
|
|
7
|
+
readonly target: {};
|
|
8
8
|
readonly type: T;
|
|
9
9
|
readonly data: D;
|
|
10
10
|
private _propagationStopped;
|
|
@@ -21,7 +21,7 @@ export declare abstract class EventBase<T extends string, D> {
|
|
|
21
21
|
* Constructor
|
|
22
22
|
* @param type Type
|
|
23
23
|
*/
|
|
24
|
-
constructor(target:
|
|
24
|
+
constructor(target: {}, type: T, data: D);
|
|
25
25
|
/**
|
|
26
26
|
* Prevent all other listeners from being called
|
|
27
27
|
*/
|
|
@@ -40,65 +40,56 @@ interface EventOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
once?: boolean;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* T for type
|
|
46
|
-
* D for data
|
|
47
|
-
*/
|
|
48
|
-
export declare type EventClassCallback<T extends string, D> = (event: EventBase<T, D>) => void;
|
|
49
|
-
/**
|
|
50
|
-
* Event class collection definition
|
|
51
|
-
* T for type
|
|
52
|
-
* D for data
|
|
53
|
-
*/
|
|
54
|
-
export declare type EventClassCollection<T extends string, D> = {
|
|
55
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
43
|
+
declare type EventClassDef = {
|
|
44
|
+
[key: string]: {};
|
|
56
45
|
};
|
|
57
46
|
/**
|
|
58
47
|
* Event class
|
|
59
48
|
* T for type
|
|
60
49
|
* D for data
|
|
61
50
|
*/
|
|
62
|
-
export declare abstract class EventClass<
|
|
51
|
+
export declare abstract class EventClass<D extends EventClassDef> {
|
|
63
52
|
private readonly listeners;
|
|
64
53
|
/**
|
|
65
54
|
* Has specific type events
|
|
66
55
|
* @param type Type
|
|
67
56
|
*/
|
|
68
|
-
hasEvents(type: T): boolean;
|
|
57
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
69
58
|
/**
|
|
70
59
|
* Has specific type and callback events
|
|
71
60
|
* @param type Type
|
|
72
61
|
* @param callback Callback
|
|
73
62
|
*/
|
|
74
|
-
hasEvents(type: T, callback:
|
|
63
|
+
hasEvents<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): boolean;
|
|
75
64
|
/**
|
|
76
65
|
* Remove all specific type events
|
|
77
66
|
* @param type Type
|
|
78
67
|
*/
|
|
79
|
-
off(type: T): void;
|
|
68
|
+
off<T extends keyof D>(type: T): void;
|
|
80
69
|
/**
|
|
81
70
|
* Remove specific type and callback event
|
|
82
71
|
* @param type Type
|
|
83
72
|
* @param callback Callback
|
|
84
73
|
*/
|
|
85
|
-
off(type: T, callback:
|
|
74
|
+
off<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): void;
|
|
86
75
|
/**
|
|
87
76
|
* Add event listeners
|
|
88
77
|
* @param collection Collection of events
|
|
89
78
|
*/
|
|
90
|
-
on(collection:
|
|
79
|
+
on(collection: {
|
|
80
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
81
|
+
}): void;
|
|
91
82
|
/**
|
|
92
83
|
* Add event listener
|
|
93
84
|
* @param type Type
|
|
94
85
|
* @param callback Callback
|
|
95
86
|
* @param options Options
|
|
96
87
|
*/
|
|
97
|
-
on(type: T, callback:
|
|
88
|
+
on<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void, options?: EventOptions): void;
|
|
98
89
|
/**
|
|
99
90
|
* Trigger event
|
|
100
91
|
* @param event Event
|
|
101
92
|
*/
|
|
102
|
-
trigger(event: EventBase<T, D>): void;
|
|
93
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>): void;
|
|
103
94
|
}
|
|
104
95
|
export {};
|
|
@@ -102,6 +102,8 @@ class EventClass {
|
|
|
102
102
|
if (callback == null)
|
|
103
103
|
return;
|
|
104
104
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
105
|
+
// String to T conversion problem
|
|
106
|
+
// "keyofStringsOnly": true could solve part of it
|
|
105
107
|
(_b = this.listeners.get(type)) === null || _b === void 0 ? void 0 : _b.push([callback, options]);
|
|
106
108
|
}
|
|
107
109
|
/**
|
|
@@ -1,26 +1,38 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
|
-
|
|
3
|
-
* ETSOO Extended history event type
|
|
4
|
-
*/
|
|
5
|
-
export declare type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
6
|
-
/**
|
|
7
|
-
* ETSOO Extended history event data
|
|
8
|
-
*/
|
|
9
|
-
export interface EHistoryEventData {
|
|
2
|
+
interface EHistoryEventData {
|
|
10
3
|
/**
|
|
11
4
|
* Current index
|
|
12
5
|
*/
|
|
13
6
|
index: number;
|
|
14
7
|
}
|
|
8
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
9
|
+
/**
|
|
10
|
+
* Delta
|
|
11
|
+
*/
|
|
12
|
+
delta: number;
|
|
13
|
+
}
|
|
14
|
+
declare type EHistoryEventDef = {
|
|
15
|
+
clear: EHistoryEventData;
|
|
16
|
+
navigate: EHistoryNavigateEventData;
|
|
17
|
+
push: EHistoryEventData;
|
|
18
|
+
replace: EHistoryEventData;
|
|
19
|
+
};
|
|
20
|
+
declare type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
15
21
|
/**
|
|
16
22
|
* ETSOO Extended history event
|
|
17
23
|
*/
|
|
18
24
|
export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* ETSOO Extended history navigate event
|
|
28
|
+
*/
|
|
29
|
+
export declare class EHistoryNavigateEvent extends EventBase<'navigate', EHistoryNavigateEventData> {
|
|
30
|
+
constructor(target: {}, data: EHistoryNavigateEventData);
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* ETSOO Extended abstract history class
|
|
22
34
|
*/
|
|
23
|
-
export declare abstract class EHistory<T> extends EventClass<
|
|
35
|
+
export declare abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
24
36
|
readonly maxDepth: number;
|
|
25
37
|
private _index;
|
|
26
38
|
/**
|
|
@@ -57,7 +69,8 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
57
69
|
* @param type Type
|
|
58
70
|
* @param index Current index
|
|
59
71
|
*/
|
|
60
|
-
protected createEvent(type:
|
|
72
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number): EHistoryEvent;
|
|
73
|
+
protected createNavigateEvent(index: number, delta: number): EHistoryNavigateEvent;
|
|
61
74
|
/**
|
|
62
75
|
* Forward to the next state
|
|
63
76
|
*/
|
|
@@ -82,3 +95,4 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
82
95
|
*/
|
|
83
96
|
replaceState(state: T): void;
|
|
84
97
|
}
|
|
98
|
+
export {};
|
|
@@ -4,6 +4,14 @@ import { EventBase, EventClass } from './EventClass';
|
|
|
4
4
|
*/
|
|
5
5
|
export class EHistoryEvent extends EventBase {
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* ETSOO Extended history navigate event
|
|
9
|
+
*/
|
|
10
|
+
export class EHistoryNavigateEvent extends EventBase {
|
|
11
|
+
constructor(target, data) {
|
|
12
|
+
super(target, 'navigate', data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
7
15
|
/**
|
|
8
16
|
* ETSOO Extended abstract history class
|
|
9
17
|
*/
|
|
@@ -65,6 +73,9 @@ export class EHistory extends EventClass {
|
|
|
65
73
|
createEvent(type, index) {
|
|
66
74
|
return new EHistoryEvent(this, type, { index });
|
|
67
75
|
}
|
|
76
|
+
createNavigateEvent(index, delta) {
|
|
77
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
78
|
+
}
|
|
68
79
|
/**
|
|
69
80
|
* Forward to the next state
|
|
70
81
|
*/
|
|
@@ -99,7 +110,7 @@ export class EHistory extends EventClass {
|
|
|
99
110
|
// Update the index
|
|
100
111
|
this._index = newIndex;
|
|
101
112
|
// Trigger event
|
|
102
|
-
this.trigger(this.
|
|
113
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
103
114
|
}
|
|
104
115
|
/**
|
|
105
116
|
* Adds an entry to the history stack
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export declare abstract class EventBase<T
|
|
7
|
-
readonly target:
|
|
6
|
+
export declare abstract class EventBase<T, D> {
|
|
7
|
+
readonly target: {};
|
|
8
8
|
readonly type: T;
|
|
9
9
|
readonly data: D;
|
|
10
10
|
private _propagationStopped;
|
|
@@ -21,7 +21,7 @@ export declare abstract class EventBase<T extends string, D> {
|
|
|
21
21
|
* Constructor
|
|
22
22
|
* @param type Type
|
|
23
23
|
*/
|
|
24
|
-
constructor(target:
|
|
24
|
+
constructor(target: {}, type: T, data: D);
|
|
25
25
|
/**
|
|
26
26
|
* Prevent all other listeners from being called
|
|
27
27
|
*/
|
|
@@ -40,65 +40,56 @@ interface EventOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
once?: boolean;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* T for type
|
|
46
|
-
* D for data
|
|
47
|
-
*/
|
|
48
|
-
export declare type EventClassCallback<T extends string, D> = (event: EventBase<T, D>) => void;
|
|
49
|
-
/**
|
|
50
|
-
* Event class collection definition
|
|
51
|
-
* T for type
|
|
52
|
-
* D for data
|
|
53
|
-
*/
|
|
54
|
-
export declare type EventClassCollection<T extends string, D> = {
|
|
55
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
43
|
+
declare type EventClassDef = {
|
|
44
|
+
[key: string]: {};
|
|
56
45
|
};
|
|
57
46
|
/**
|
|
58
47
|
* Event class
|
|
59
48
|
* T for type
|
|
60
49
|
* D for data
|
|
61
50
|
*/
|
|
62
|
-
export declare abstract class EventClass<
|
|
51
|
+
export declare abstract class EventClass<D extends EventClassDef> {
|
|
63
52
|
private readonly listeners;
|
|
64
53
|
/**
|
|
65
54
|
* Has specific type events
|
|
66
55
|
* @param type Type
|
|
67
56
|
*/
|
|
68
|
-
hasEvents(type: T): boolean;
|
|
57
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
69
58
|
/**
|
|
70
59
|
* Has specific type and callback events
|
|
71
60
|
* @param type Type
|
|
72
61
|
* @param callback Callback
|
|
73
62
|
*/
|
|
74
|
-
hasEvents(type: T, callback:
|
|
63
|
+
hasEvents<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): boolean;
|
|
75
64
|
/**
|
|
76
65
|
* Remove all specific type events
|
|
77
66
|
* @param type Type
|
|
78
67
|
*/
|
|
79
|
-
off(type: T): void;
|
|
68
|
+
off<T extends keyof D>(type: T): void;
|
|
80
69
|
/**
|
|
81
70
|
* Remove specific type and callback event
|
|
82
71
|
* @param type Type
|
|
83
72
|
* @param callback Callback
|
|
84
73
|
*/
|
|
85
|
-
off(type: T, callback:
|
|
74
|
+
off<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): void;
|
|
86
75
|
/**
|
|
87
76
|
* Add event listeners
|
|
88
77
|
* @param collection Collection of events
|
|
89
78
|
*/
|
|
90
|
-
on(collection:
|
|
79
|
+
on(collection: {
|
|
80
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
81
|
+
}): void;
|
|
91
82
|
/**
|
|
92
83
|
* Add event listener
|
|
93
84
|
* @param type Type
|
|
94
85
|
* @param callback Callback
|
|
95
86
|
* @param options Options
|
|
96
87
|
*/
|
|
97
|
-
on(type: T, callback:
|
|
88
|
+
on<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void, options?: EventOptions): void;
|
|
98
89
|
/**
|
|
99
90
|
* Trigger event
|
|
100
91
|
* @param event Event
|
|
101
92
|
*/
|
|
102
|
-
trigger(event: EventBase<T, D>): void;
|
|
93
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>): void;
|
|
103
94
|
}
|
|
104
95
|
export {};
|
|
@@ -98,6 +98,8 @@ export class EventClass {
|
|
|
98
98
|
if (callback == null)
|
|
99
99
|
return;
|
|
100
100
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
101
|
+
// String to T conversion problem
|
|
102
|
+
// "keyofStringsOnly": true could solve part of it
|
|
101
103
|
(_b = this.listeners.get(type)) === null || _b === void 0 ? void 0 : _b.push([callback, options]);
|
|
102
104
|
}
|
|
103
105
|
/**
|
package/package.json
CHANGED
package/src/types/EHistory.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* ETSOO Extended history event type
|
|
5
|
-
*/
|
|
6
|
-
export type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* ETSOO Extended history event data
|
|
10
|
-
*/
|
|
11
|
-
export interface EHistoryEventData {
|
|
3
|
+
interface EHistoryEventData {
|
|
12
4
|
/**
|
|
13
5
|
* Current index
|
|
14
6
|
*/
|
|
15
7
|
index: number;
|
|
16
8
|
}
|
|
17
9
|
|
|
10
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
11
|
+
/**
|
|
12
|
+
* Delta
|
|
13
|
+
*/
|
|
14
|
+
delta: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type EHistoryEventDef = {
|
|
18
|
+
clear: EHistoryEventData;
|
|
19
|
+
navigate: EHistoryNavigateEventData;
|
|
20
|
+
push: EHistoryEventData;
|
|
21
|
+
replace: EHistoryEventData;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
25
|
+
|
|
18
26
|
/**
|
|
19
27
|
* ETSOO Extended history event
|
|
20
28
|
*/
|
|
@@ -24,12 +32,21 @@ export class EHistoryEvent extends EventBase<
|
|
|
24
32
|
> {}
|
|
25
33
|
|
|
26
34
|
/**
|
|
27
|
-
* ETSOO Extended
|
|
35
|
+
* ETSOO Extended history navigate event
|
|
28
36
|
*/
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
export class EHistoryNavigateEvent extends EventBase<
|
|
38
|
+
'navigate',
|
|
39
|
+
EHistoryNavigateEventData
|
|
32
40
|
> {
|
|
41
|
+
constructor(target: {}, data: EHistoryNavigateEventData) {
|
|
42
|
+
super(target, 'navigate', data);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* ETSOO Extended abstract history class
|
|
48
|
+
*/
|
|
49
|
+
export abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
33
50
|
// Index
|
|
34
51
|
private _index: number = -1;
|
|
35
52
|
|
|
@@ -90,10 +107,14 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
90
107
|
* @param type Type
|
|
91
108
|
* @param index Current index
|
|
92
109
|
*/
|
|
93
|
-
protected createEvent(type:
|
|
110
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number) {
|
|
94
111
|
return new EHistoryEvent(this, type, { index });
|
|
95
112
|
}
|
|
96
113
|
|
|
114
|
+
protected createNavigateEvent(index: number, delta: number) {
|
|
115
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
116
|
+
}
|
|
117
|
+
|
|
97
118
|
/**
|
|
98
119
|
* Forward to the next state
|
|
99
120
|
*/
|
|
@@ -129,7 +150,7 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
129
150
|
this._index = newIndex;
|
|
130
151
|
|
|
131
152
|
// Trigger event
|
|
132
|
-
this.trigger(this.
|
|
153
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
133
154
|
}
|
|
134
155
|
|
|
135
156
|
/**
|
package/src/types/EventClass.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export abstract class EventBase<T
|
|
6
|
+
export abstract class EventBase<T, D> {
|
|
7
7
|
private _propagationStopped: boolean = false;
|
|
8
8
|
/**
|
|
9
9
|
* stopImmediatePropagation called
|
|
@@ -25,7 +25,7 @@ export abstract class EventBase<T extends string, D> {
|
|
|
25
25
|
* @param type Type
|
|
26
26
|
*/
|
|
27
27
|
constructor(
|
|
28
|
-
public readonly target:
|
|
28
|
+
public readonly target: {},
|
|
29
29
|
public readonly type: T,
|
|
30
30
|
public readonly data: D
|
|
31
31
|
) {
|
|
@@ -55,48 +55,35 @@ interface EventOptions {
|
|
|
55
55
|
once?: boolean;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
* Event class callback
|
|
60
|
-
* T for type
|
|
61
|
-
* D for data
|
|
62
|
-
*/
|
|
63
|
-
export type EventClassCallback<T extends string, D> = (
|
|
64
|
-
event: EventBase<T, D>
|
|
65
|
-
) => void;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Event class collection definition
|
|
69
|
-
* T for type
|
|
70
|
-
* D for data
|
|
71
|
-
*/
|
|
72
|
-
export type EventClassCollection<T extends string, D> = {
|
|
73
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
74
|
-
};
|
|
58
|
+
type EventClassDef = { [key: string]: {} };
|
|
75
59
|
|
|
76
60
|
/**
|
|
77
61
|
* Event class
|
|
78
62
|
* T for type
|
|
79
63
|
* D for data
|
|
80
64
|
*/
|
|
81
|
-
export abstract class EventClass<
|
|
65
|
+
export abstract class EventClass<D extends EventClassDef> {
|
|
82
66
|
// Listeners
|
|
83
67
|
private readonly listeners = new Map<
|
|
84
|
-
|
|
85
|
-
[
|
|
68
|
+
keyof D,
|
|
69
|
+
[(event: EventBase<keyof D, D[keyof D]>) => void, EventOptions?][]
|
|
86
70
|
>();
|
|
87
71
|
|
|
88
72
|
/**
|
|
89
73
|
* Has specific type events
|
|
90
74
|
* @param type Type
|
|
91
75
|
*/
|
|
92
|
-
hasEvents(type: T): boolean;
|
|
76
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
93
77
|
|
|
94
78
|
/**
|
|
95
79
|
* Has specific type and callback events
|
|
96
80
|
* @param type Type
|
|
97
81
|
* @param callback Callback
|
|
98
82
|
*/
|
|
99
|
-
hasEvents
|
|
83
|
+
hasEvents<T extends keyof D>(
|
|
84
|
+
type: T,
|
|
85
|
+
callback: (event: EventBase<T, D[T]>) => void
|
|
86
|
+
): boolean;
|
|
100
87
|
|
|
101
88
|
/**
|
|
102
89
|
* Has specific type and callback events
|
|
@@ -104,7 +91,10 @@ export abstract class EventClass<T extends string, D> {
|
|
|
104
91
|
* @param callback Callback
|
|
105
92
|
* @returns Result
|
|
106
93
|
*/
|
|
107
|
-
hasEvents
|
|
94
|
+
hasEvents<T extends keyof D>(
|
|
95
|
+
type: T,
|
|
96
|
+
callback?: (event: EventBase<T, D[T]>) => void
|
|
97
|
+
) {
|
|
108
98
|
const items = this.listeners.get(type);
|
|
109
99
|
if (items == null || items.length === 0) return false;
|
|
110
100
|
|
|
@@ -119,21 +109,27 @@ export abstract class EventClass<T extends string, D> {
|
|
|
119
109
|
* Remove all specific type events
|
|
120
110
|
* @param type Type
|
|
121
111
|
*/
|
|
122
|
-
off(type: T): void;
|
|
112
|
+
off<T extends keyof D>(type: T): void;
|
|
123
113
|
|
|
124
114
|
/**
|
|
125
115
|
* Remove specific type and callback event
|
|
126
116
|
* @param type Type
|
|
127
117
|
* @param callback Callback
|
|
128
118
|
*/
|
|
129
|
-
off
|
|
119
|
+
off<T extends keyof D>(
|
|
120
|
+
type: T,
|
|
121
|
+
callback: (event: EventBase<T, D[T]>) => void
|
|
122
|
+
): void;
|
|
130
123
|
|
|
131
124
|
/**
|
|
132
125
|
* Remove specific type and callback event
|
|
133
126
|
* @param type Type
|
|
134
127
|
* @param callback Callback
|
|
135
128
|
*/
|
|
136
|
-
off
|
|
129
|
+
off<T extends keyof D>(
|
|
130
|
+
type: T,
|
|
131
|
+
callback?: (event: EventBase<T, D[T]>) => void
|
|
132
|
+
) {
|
|
137
133
|
if (callback == null) {
|
|
138
134
|
this.listeners.delete(type);
|
|
139
135
|
return;
|
|
@@ -153,7 +149,9 @@ export abstract class EventClass<T extends string, D> {
|
|
|
153
149
|
* Add event listeners
|
|
154
150
|
* @param collection Collection of events
|
|
155
151
|
*/
|
|
156
|
-
on(collection:
|
|
152
|
+
on(collection: {
|
|
153
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
154
|
+
}): void;
|
|
157
155
|
|
|
158
156
|
/**
|
|
159
157
|
* Add event listener
|
|
@@ -161,9 +159,9 @@ export abstract class EventClass<T extends string, D> {
|
|
|
161
159
|
* @param callback Callback
|
|
162
160
|
* @param options Options
|
|
163
161
|
*/
|
|
164
|
-
on(
|
|
162
|
+
on<T extends keyof D>(
|
|
165
163
|
type: T,
|
|
166
|
-
callback:
|
|
164
|
+
callback: (event: EventBase<T, D[T]>) => void,
|
|
167
165
|
options?: EventOptions
|
|
168
166
|
): void;
|
|
169
167
|
|
|
@@ -173,14 +171,18 @@ export abstract class EventClass<T extends string, D> {
|
|
|
173
171
|
* @param callback Callback
|
|
174
172
|
* @param options Options
|
|
175
173
|
*/
|
|
176
|
-
on(
|
|
177
|
-
type:
|
|
178
|
-
|
|
174
|
+
on<T extends keyof D>(
|
|
175
|
+
type:
|
|
176
|
+
| {
|
|
177
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
178
|
+
}
|
|
179
|
+
| T,
|
|
180
|
+
callback?: (event: EventBase<T, D[T]>) => void,
|
|
179
181
|
options?: EventOptions
|
|
180
182
|
) {
|
|
181
183
|
if (typeof type === 'object') {
|
|
182
184
|
for (const key in type) {
|
|
183
|
-
const item = key as
|
|
185
|
+
const item = key as keyof D;
|
|
184
186
|
const itemCallback = type[item] ?? callback;
|
|
185
187
|
if (itemCallback) this.on(item, itemCallback, options);
|
|
186
188
|
}
|
|
@@ -189,14 +191,17 @@ export abstract class EventClass<T extends string, D> {
|
|
|
189
191
|
|
|
190
192
|
if (callback == null) return;
|
|
191
193
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
192
|
-
|
|
194
|
+
|
|
195
|
+
// String to T conversion problem
|
|
196
|
+
// "keyofStringsOnly": true could solve part of it
|
|
197
|
+
this.listeners.get(type)?.push([callback as any, options]);
|
|
193
198
|
}
|
|
194
199
|
|
|
195
200
|
/**
|
|
196
201
|
* Trigger event
|
|
197
202
|
* @param event Event
|
|
198
203
|
*/
|
|
199
|
-
trigger(event: EventBase<T, D>) {
|
|
204
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>) {
|
|
200
205
|
const items = this.listeners.get(event.type);
|
|
201
206
|
if (items == null) return;
|
|
202
207
|
|