@adrianbutt/evutils 1.0.0
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/index/index.d.ts +7 -0
- package/dist/cjs/src/BasicEventManager.d.ts +95 -0
- package/dist/cjs/src/index.d.ts +1 -0
- package/dist/cjs/src/mapped/EvForwarder.d.ts +265 -0
- package/dist/cjs/src/mapped/index.d.ts +1 -0
- package/dist/cjs/src/mapped/utils.d.ts +36 -0
- package/dist/mjs/index/index.d.ts +7 -0
- package/dist/mjs/index/index.js +8 -0
- package/dist/mjs/index/index.js.map +1 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/BasicEventManager.d.ts +95 -0
- package/dist/mjs/src/BasicEventManager.js +225 -0
- package/dist/mjs/src/BasicEventManager.js.map +1 -0
- package/dist/mjs/src/index.d.ts +1 -0
- package/dist/mjs/src/index.js +2 -0
- package/dist/mjs/src/index.js.map +1 -0
- package/dist/mjs/src/mapped/EvForwarder.d.ts +265 -0
- package/dist/mjs/src/mapped/EvForwarder.js +330 -0
- package/dist/mjs/src/mapped/EvForwarder.js.map +1 -0
- package/dist/mjs/src/mapped/index.d.ts +1 -0
- package/dist/mjs/src/mapped/index.js +2 -0
- package/dist/mjs/src/mapped/index.js.map +1 -0
- package/dist/mjs/src/mapped/utils.d.ts +36 -0
- package/dist/mjs/src/mapped/utils.js +242 -0
- package/dist/mjs/src/mapped/utils.js.map +1 -0
- package/dist/web/evutils.js +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Adrian Butt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# HtmlAssetReplacer
|
|
2
|
+
|
|
3
|
+
Utilities to help replace asset paths in HTML text (css and js links/assets).
|
|
4
|
+
|
|
5
|
+
### Getting Started
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install htmlassetreplacer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
export async function exampleUsage() {
|
|
13
|
+
const processor = new HtmlAssetReplacer();
|
|
14
|
+
|
|
15
|
+
const processURLCB = function (originalURI: string) {
|
|
16
|
+
return "https://my.cdn.com/assets/" + originalURI + "?token=abc";
|
|
17
|
+
};
|
|
18
|
+
const result = await processor.processHTML({
|
|
19
|
+
html: EXAMPLE_HTML_STR,
|
|
20
|
+
urlProcessor: processURLCB
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
console.log(result);
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback function.
|
|
3
|
+
*/
|
|
4
|
+
export type EventHandlerCallback = (...args: any) => void;
|
|
5
|
+
/**
|
|
6
|
+
* Context used when calling a callback function
|
|
7
|
+
*/
|
|
8
|
+
export type EventHandlerContext = unknown;
|
|
9
|
+
/**
|
|
10
|
+
* An event firer
|
|
11
|
+
*/
|
|
12
|
+
export interface IBasicEventDispatcher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> {
|
|
13
|
+
trigger<T extends keyof TEventMap & string>(eventName: T, args: Parameters<TEventMap[T]>): boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface that allows objects to subscribe to events
|
|
17
|
+
*/
|
|
18
|
+
export interface IBasicEventPublisher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> {
|
|
19
|
+
on<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
20
|
+
off<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
21
|
+
register<T extends keyof TEventMap & string>(toState: boolean, eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Event subscriber and firer.
|
|
25
|
+
*/
|
|
26
|
+
export interface IBasicEventManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> extends IBasicEventPublisher<TEventMap>, IBasicEventDispatcher<TEventMap> {
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A handler map, used to define the events that an event manager can send.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* type ServiceEvents = {
|
|
34
|
+
* serviceLaunched: (name: string, timestamp: number) => void;
|
|
35
|
+
* serviceStopped: (name: string, timestamp: number, reason: string) => void;
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export type EventMapDef<T> = {
|
|
40
|
+
[key in keyof T]: EventHandlerCallback;
|
|
41
|
+
};
|
|
42
|
+
export type DefaultEventMap = Record<string, EventHandlerCallback>;
|
|
43
|
+
/**
|
|
44
|
+
* Argument processor result.
|
|
45
|
+
* @returns
|
|
46
|
+
* If falsey the event should be cancelled.
|
|
47
|
+
* If true the event should be sent through without any param changes
|
|
48
|
+
* If an array said array will be used as arguments in the triggered event
|
|
49
|
+
*/
|
|
50
|
+
export type BasicEventManagerArgumentProcessorCallbackResult = unknown[] | boolean | null;
|
|
51
|
+
/**
|
|
52
|
+
* Argument processor logic. Allows for custom processing when triggering events.
|
|
53
|
+
* @param eventName - the name of the event that will trigger
|
|
54
|
+
* @param args - the pending arguments of the event that will trigger
|
|
55
|
+
* @returns
|
|
56
|
+
* If falsey the event should be cancelled.
|
|
57
|
+
* If true the event should be sent through without any param changes
|
|
58
|
+
* If an array said array will be used as arguments in the triggered event
|
|
59
|
+
*/
|
|
60
|
+
export type BasicEventManagerArgumentProcessorCallback = (eventName: string, args: unknown[]) => BasicEventManagerArgumentProcessorCallbackResult;
|
|
61
|
+
/**
|
|
62
|
+
* Logger used for logging.
|
|
63
|
+
*/
|
|
64
|
+
export type Logger = Pick<Console, "log" | "error" | "warn">;
|
|
65
|
+
export declare class BasicEventManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> implements IBasicEventManager<TEventMap> {
|
|
66
|
+
private _safeMode;
|
|
67
|
+
private _listeners;
|
|
68
|
+
private _argProcessors;
|
|
69
|
+
private _logger?;
|
|
70
|
+
constructor();
|
|
71
|
+
trigger<T extends keyof TEventMap & string>(eventName: T, args: Parameters<TEventMap[T]>): boolean;
|
|
72
|
+
on<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
73
|
+
off<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
74
|
+
register<T extends keyof TEventMap & string>(toState: boolean, eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
75
|
+
hasCustomArgumentProcessor(eventName: string): boolean;
|
|
76
|
+
setCustomArgumentProcessor(eventName: string, processor: BasicEventManagerArgumentProcessorCallback | null, context?: EventHandlerContext): boolean;
|
|
77
|
+
get ignoreErrors(): boolean;
|
|
78
|
+
set ignoreErrors(v: boolean);
|
|
79
|
+
get logger(): Logger | null;
|
|
80
|
+
set logger(v: Logger | null);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Utility class that allows for simple setup of event registration,
|
|
84
|
+
* whilst keeping triggering hidden.
|
|
85
|
+
*/
|
|
86
|
+
export declare abstract class BasicEventPublisher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> implements IBasicEventPublisher<TEventMap> {
|
|
87
|
+
protected _evManager: IBasicEventManager<TEventMap>;
|
|
88
|
+
constructor(evManager?: IBasicEventManager<TEventMap>);
|
|
89
|
+
on: IBasicEventManager<TEventMap>["on"];
|
|
90
|
+
off: IBasicEventManager<TEventMap>["off"];
|
|
91
|
+
register: IBasicEventManager<TEventMap>["register"];
|
|
92
|
+
protected _trigger: IBasicEventManager<TEventMap>["trigger"];
|
|
93
|
+
}
|
|
94
|
+
export declare function mixinEventPublisherUsingManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap>(ontoObject: IBasicEventPublisher<TEventMap>, usingManager: IBasicEventManager<TEventMap>): void;
|
|
95
|
+
export default BasicEventManager;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BasicEventManager } from "./BasicEventManager";
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import type { IBasicEventPublisher, DefaultEventMap, EventHandlerContext, EventMapDef } from "@/src/BasicEventManager";
|
|
2
|
+
/**
|
|
3
|
+
* Event param conditions
|
|
4
|
+
* <br> If a string, will be parsed and converted into an EvParamCondition
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* // would check if the param at index 1 does exist (ie arguments >= 2 in length)
|
|
8
|
+
* '1'
|
|
9
|
+
* // would check if the param at index 1 does not exist (ie arguments < 2 in length)
|
|
10
|
+
* '1!'
|
|
11
|
+
* // would check if the param at index 1 IS 'Monday'
|
|
12
|
+
* '1="Monday"'
|
|
13
|
+
* '1="Monday" i' // (case insensitive)
|
|
14
|
+
* // would check if the param at index 1 is NOT 'Monday'
|
|
15
|
+
* '1!="Monday"'
|
|
16
|
+
* '1!="Monday" i' // (case insensitive)
|
|
17
|
+
* // would check if the param at index 1 starts with 'Mon'
|
|
18
|
+
* '1^="Mon"'
|
|
19
|
+
* '1^="Mon" i' // (case insensitive)
|
|
20
|
+
* // would check if the param at index 1 ends with 'day'
|
|
21
|
+
* '1$="day"'
|
|
22
|
+
* '1$="day" i' // (case insensitive)
|
|
23
|
+
* // would check if the param at index 1 contains the string 'day'
|
|
24
|
+
* '1*="day"'
|
|
25
|
+
* '1*="day" i' // (case insensitive)
|
|
26
|
+
* // would check if the param at index 1 was equal to 'Monday' or 'Tuesday'
|
|
27
|
+
* '1~="Monday Tuesday"'
|
|
28
|
+
* '1~="Monday Tuesday" i' // (case insensitive)
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type EvParamConditionLike = EvParamCondition | string;
|
|
32
|
+
/**
|
|
33
|
+
* Event parameter forwarding instructions
|
|
34
|
+
* <br> If a string, will be parsed and converted into an EvParamCondition
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* // No parameters should be forwarded
|
|
38
|
+
* '!'
|
|
39
|
+
*
|
|
40
|
+
* '1' // -> { from: 1, to: 1 }
|
|
41
|
+
* '2+' // -> { from: 2, to: Number.POSITIVE_INFINITY }
|
|
42
|
+
* '2-' // -> { from: 2, to: Number.NEGATIVE_INFINITY }
|
|
43
|
+
* '6-7' // -> { from: 6, to: 7 }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export type EvParamForwardInstructionLike = EvParamForwardInstruction | string;
|
|
47
|
+
/**
|
|
48
|
+
* Essentially an {@link EvForwarderMap} with the option to specify strings as param properties
|
|
49
|
+
*/
|
|
50
|
+
export type EvForwarderConstructorEventMap = {
|
|
51
|
+
eventName: string;
|
|
52
|
+
forwardedEventName?: string;
|
|
53
|
+
paramConditions: EvParamConditionLike[];
|
|
54
|
+
paramForwardInstructions: EvParamForwardInstructionLike[];
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Options to create an EvForwarder
|
|
58
|
+
* @property eventReceiver - the original or 'root' event broadcaster
|
|
59
|
+
* @property eventMaps - event forwarding instructions
|
|
60
|
+
* @property autoEnable - whether the forwarder should automatically connect to the eventReceiver
|
|
61
|
+
*/
|
|
62
|
+
export type EvForwarderConstructorOptions = {
|
|
63
|
+
eventReceiver: IBasicEventPublisher;
|
|
64
|
+
eventMaps: EvForwarderConstructorEventMap[];
|
|
65
|
+
autoEnable?: boolean;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Event forwarding instructions
|
|
69
|
+
* @property eventName - original event name
|
|
70
|
+
* @property forwardedEventName - the forwarded event name
|
|
71
|
+
* (if not provided, the original event name will be used)
|
|
72
|
+
* @property paramConditions - parameter conditions that the original event must clear
|
|
73
|
+
* @property paramForwardInstructions - how the original event's parameters should be forwarded.
|
|
74
|
+
* (All will be forwarded if not present)
|
|
75
|
+
*/
|
|
76
|
+
export type EvForwarderMap = {
|
|
77
|
+
eventName: string;
|
|
78
|
+
forwardedEventName?: string;
|
|
79
|
+
paramConditions: EvParamCondition[];
|
|
80
|
+
paramForwardInstructions: EvParamForwardInstruction[];
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Used to verify event params
|
|
84
|
+
* @property index - relevant zero-based parameter index
|
|
85
|
+
* @property operator - condition operator to run
|
|
86
|
+
* @property comparison - strings to compare with
|
|
87
|
+
* @property caseSensitive - whether the comparison should be case sensitive
|
|
88
|
+
*
|
|
89
|
+
*/
|
|
90
|
+
export type EvParamCondition = {
|
|
91
|
+
index: number;
|
|
92
|
+
operator: EvParamOperator;
|
|
93
|
+
comparison: string[];
|
|
94
|
+
caseSensitive: boolean;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Used to define what params should be forwarded in an event.
|
|
98
|
+
* <br><br>If both "from" and "to" are NaN -> no parameters should be forwarded
|
|
99
|
+
* <br><br>If both "from" or "to" are less than 0 -> the index is assumed to be an offset from params length
|
|
100
|
+
* @property from - start index
|
|
101
|
+
* @property to - end index
|
|
102
|
+
*/
|
|
103
|
+
export type EvParamForwardInstruction = {
|
|
104
|
+
from: number;
|
|
105
|
+
to: number;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Operator for param verification.
|
|
109
|
+
* @sortStrategy source-order
|
|
110
|
+
*/
|
|
111
|
+
export declare enum EvParamOperator {
|
|
112
|
+
/**
|
|
113
|
+
* Whether the parameter does not exist.
|
|
114
|
+
* <br> NOTE: No suffix is expected after using EXISTS.
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* // would check if the param at index 1 does exist (ie arguments >= 2 in length)
|
|
118
|
+
* '1_'
|
|
119
|
+
* // is functionally equivalent to having NO operator
|
|
120
|
+
* '1'
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
EXISTS = "_",
|
|
124
|
+
/**
|
|
125
|
+
* Whether the parameter does not exist.
|
|
126
|
+
* <br> NOTE: No suffix is expected after using NOT_EXISTS.
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* // would check if the param at index 1 does not exist (ie arguments < 2 in length)
|
|
130
|
+
* '1!'
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
NOT_EXISTS = "!",
|
|
134
|
+
/**
|
|
135
|
+
* Whether the value is equal to a string
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // would check if the param at index 1 IS 'monday'
|
|
139
|
+
* '1="monday"'
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
EQUAL = "=",
|
|
143
|
+
/**
|
|
144
|
+
* Whether the value is not equal to a string
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* // would check if the param at index 1 is NOT 'monday'
|
|
148
|
+
* '1!="monday"'
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
NOT_EQUAL = "!=",
|
|
152
|
+
/**
|
|
153
|
+
* Whether the value ends with a string
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // would check if the param at index 1 starts with 'mon'
|
|
157
|
+
* '1^="mon"'
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
STARTS_WITH = "^=",
|
|
161
|
+
/**
|
|
162
|
+
* Whether the value ends with a string
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* // would check if the param at index 1 ends with 'day'
|
|
166
|
+
* '1$="day"'
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
ENDS_WITH = "$=",
|
|
170
|
+
/**
|
|
171
|
+
* Whether the value contains a string
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* // would check if the param at index 1 contains the string 'day'
|
|
175
|
+
* '1*="day"'
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
CONTAINS = "*=",
|
|
179
|
+
/**
|
|
180
|
+
* Exact match for space separated words
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* // would check if the param at index 1 was equal to 'monday' or 'tuesday'
|
|
184
|
+
* '1~="monday tuesday"'
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
MATCHES_WORD = "~="
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Event forwarder
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const rootEvMgr = new BasicEventManager();
|
|
195
|
+
* const evFwder = new EvForwarder({
|
|
196
|
+
* eventReceiver: rootEvMgr,
|
|
197
|
+
* eventMaps: [{
|
|
198
|
+
* eventName: "FoodEvent",
|
|
199
|
+
* paramConditions: [],
|
|
200
|
+
* paramForwardInstructions: []
|
|
201
|
+
* }, {
|
|
202
|
+
* eventName: "FoodEvent",
|
|
203
|
+
* forwardedEventName: "AteFood",
|
|
204
|
+
* paramConditions: ['0="Ate"'],
|
|
205
|
+
* paramForwardInstructions: ["2"]
|
|
206
|
+
* }, {
|
|
207
|
+
* eventName: "FoodEvent",
|
|
208
|
+
* forwardedEventName: "AteSnack",
|
|
209
|
+
* paramConditions: ['0="Ate"', '1="Snack"'],
|
|
210
|
+
* paramForwardInstructions: ["2"]
|
|
211
|
+
* }],
|
|
212
|
+
* autoEnable: true
|
|
213
|
+
* });
|
|
214
|
+
*
|
|
215
|
+
* const handleFoodEvent = function () {};
|
|
216
|
+
* const handleAteFood = function () {};
|
|
217
|
+
* const handleAteSnack = function () {};
|
|
218
|
+
* evFwder.on("FoodEvent", handleFoodEvent);
|
|
219
|
+
* evFwder.on("AteFood", handleAteFood);
|
|
220
|
+
* evFwder.on("AteSnack", handleAteSnack);
|
|
221
|
+
*
|
|
222
|
+
* rootEvMgr.trigger("FoodEvent", []);
|
|
223
|
+
* rootEvMgr.trigger("FoodEvent", ["Ate", "Snack", "Chocolate"]);
|
|
224
|
+
* rootEvMgr.trigger("FoodEvent", ["Ate", "Snack", "Biscuit"]);
|
|
225
|
+
* rootEvMgr.trigger("FoodEvent", ["Ate", "Meal", "Sandwich"]);
|
|
226
|
+
* rootEvMgr.trigger("FoodEvent", ["Drank", "Soft", "Lemonade"]);
|
|
227
|
+
* rootEvMgr.trigger("FoodEvent", ["Drank", "Hard", "Vodka"]);
|
|
228
|
+
*
|
|
229
|
+
* // handleFoodEvent invocations:
|
|
230
|
+
* // handleFoodEvent();
|
|
231
|
+
* // handleFoodEvent("Ate", "Snack", "Chocolate");
|
|
232
|
+
* // handleFoodEvent("Ate", "Snack", "Biscuit");
|
|
233
|
+
* // handleFoodEvent("Ate", "Meal", "Sandwich");
|
|
234
|
+
* // handleFoodEvent("Drank", "Soft", "Lemonade");
|
|
235
|
+
* // handleFoodEvent("Drank", "Hard", "Vodka");
|
|
236
|
+
*
|
|
237
|
+
* // handleAteFood invocations:
|
|
238
|
+
* // handleAteFood("Chocolate");
|
|
239
|
+
* // handleAteFood("Biscuit");
|
|
240
|
+
* // handleAteFood("Sandwich");
|
|
241
|
+
*
|
|
242
|
+
* // handleAteSnack invocations:
|
|
243
|
+
* // handleAteSnack("Chocolate");
|
|
244
|
+
* // handleAteSnack("Biscuit");
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export declare class EvForwarder<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> implements IBasicEventPublisher<TEventMap> {
|
|
248
|
+
private _evReceiver;
|
|
249
|
+
private _evMgr;
|
|
250
|
+
private _evMaps;
|
|
251
|
+
private _active?;
|
|
252
|
+
constructor(options: EvForwarderConstructorOptions);
|
|
253
|
+
/**
|
|
254
|
+
* Whether the forwarder should forward events or not.
|
|
255
|
+
*/
|
|
256
|
+
get active(): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Whether the forwarder should forward events or not.
|
|
259
|
+
*/
|
|
260
|
+
set active(to: boolean);
|
|
261
|
+
on<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
262
|
+
off<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
263
|
+
register<T extends keyof TEventMap & string>(toState: boolean, eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
264
|
+
private _refreshListeners;
|
|
265
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./EvForwarder";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { EvParamCondition, EvParamForwardInstruction, EvParamOperator } from "./EvForwarder";
|
|
2
|
+
export declare function parseEvParamConditionStr(str: string): EvParamCondition;
|
|
3
|
+
export declare function parseEvParamForwardInstructionStr(str: string): EvParamForwardInstruction;
|
|
4
|
+
export type ParamCheckOptions = {
|
|
5
|
+
operator: EvParamOperator;
|
|
6
|
+
rawValue: string;
|
|
7
|
+
comparison: string[];
|
|
8
|
+
caseSensitive: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare function paramCheck(options: ParamCheckOptions): boolean;
|
|
11
|
+
type InternalParamCheckCallbackOptions = Pick<ParamCheckOptions, "rawValue" | "comparison" | "caseSensitive">;
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function paramCheck_EqualTo({ rawValue, comparison, caseSensitive }: InternalParamCheckCallbackOptions): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function paramCheck_NotEqualTo({ rawValue, comparison, caseSensitive }: InternalParamCheckCallbackOptions): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export declare function paramCheck_StartsWith({ rawValue, comparison, caseSensitive }: InternalParamCheckCallbackOptions): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function paramCheck_EndsWith({ rawValue, comparison, caseSensitive }: InternalParamCheckCallbackOptions): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function paramCheck_Contains({ rawValue, comparison, caseSensitive }: InternalParamCheckCallbackOptions): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export declare function paramCheck_MatchesWord(options: InternalParamCheckCallbackOptions): boolean;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../index/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AAEvC,MAAM,KAAK,GAAG;IACZ,iBAAiB;IACjB,MAAM;CACP,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback function.
|
|
3
|
+
*/
|
|
4
|
+
export type EventHandlerCallback = (...args: any) => void;
|
|
5
|
+
/**
|
|
6
|
+
* Context used when calling a callback function
|
|
7
|
+
*/
|
|
8
|
+
export type EventHandlerContext = unknown;
|
|
9
|
+
/**
|
|
10
|
+
* An event firer
|
|
11
|
+
*/
|
|
12
|
+
export interface IBasicEventDispatcher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> {
|
|
13
|
+
trigger<T extends keyof TEventMap & string>(eventName: T, args: Parameters<TEventMap[T]>): boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface that allows objects to subscribe to events
|
|
17
|
+
*/
|
|
18
|
+
export interface IBasicEventPublisher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> {
|
|
19
|
+
on<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
20
|
+
off<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
21
|
+
register<T extends keyof TEventMap & string>(toState: boolean, eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Event subscriber and firer.
|
|
25
|
+
*/
|
|
26
|
+
export interface IBasicEventManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> extends IBasicEventPublisher<TEventMap>, IBasicEventDispatcher<TEventMap> {
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A handler map, used to define the events that an event manager can send.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* type ServiceEvents = {
|
|
34
|
+
* serviceLaunched: (name: string, timestamp: number) => void;
|
|
35
|
+
* serviceStopped: (name: string, timestamp: number, reason: string) => void;
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export type EventMapDef<T> = {
|
|
40
|
+
[key in keyof T]: EventHandlerCallback;
|
|
41
|
+
};
|
|
42
|
+
export type DefaultEventMap = Record<string, EventHandlerCallback>;
|
|
43
|
+
/**
|
|
44
|
+
* Argument processor result.
|
|
45
|
+
* @returns
|
|
46
|
+
* If falsey the event should be cancelled.
|
|
47
|
+
* If true the event should be sent through without any param changes
|
|
48
|
+
* If an array said array will be used as arguments in the triggered event
|
|
49
|
+
*/
|
|
50
|
+
export type BasicEventManagerArgumentProcessorCallbackResult = unknown[] | boolean | null;
|
|
51
|
+
/**
|
|
52
|
+
* Argument processor logic. Allows for custom processing when triggering events.
|
|
53
|
+
* @param eventName - the name of the event that will trigger
|
|
54
|
+
* @param args - the pending arguments of the event that will trigger
|
|
55
|
+
* @returns
|
|
56
|
+
* If falsey the event should be cancelled.
|
|
57
|
+
* If true the event should be sent through without any param changes
|
|
58
|
+
* If an array said array will be used as arguments in the triggered event
|
|
59
|
+
*/
|
|
60
|
+
export type BasicEventManagerArgumentProcessorCallback = (eventName: string, args: unknown[]) => BasicEventManagerArgumentProcessorCallbackResult;
|
|
61
|
+
/**
|
|
62
|
+
* Logger used for logging.
|
|
63
|
+
*/
|
|
64
|
+
export type Logger = Pick<Console, "log" | "error" | "warn">;
|
|
65
|
+
export declare class BasicEventManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> implements IBasicEventManager<TEventMap> {
|
|
66
|
+
private _safeMode;
|
|
67
|
+
private _listeners;
|
|
68
|
+
private _argProcessors;
|
|
69
|
+
private _logger?;
|
|
70
|
+
constructor();
|
|
71
|
+
trigger<T extends keyof TEventMap & string>(eventName: T, args: Parameters<TEventMap[T]>): boolean;
|
|
72
|
+
on<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
73
|
+
off<T extends keyof TEventMap & string>(eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
74
|
+
register<T extends keyof TEventMap & string>(toState: boolean, eventName: T, handler: TEventMap[T], context?: EventHandlerContext): boolean;
|
|
75
|
+
hasCustomArgumentProcessor(eventName: string): boolean;
|
|
76
|
+
setCustomArgumentProcessor(eventName: string, processor: BasicEventManagerArgumentProcessorCallback | null, context?: EventHandlerContext): boolean;
|
|
77
|
+
get ignoreErrors(): boolean;
|
|
78
|
+
set ignoreErrors(v: boolean);
|
|
79
|
+
get logger(): Logger | null;
|
|
80
|
+
set logger(v: Logger | null);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Utility class that allows for simple setup of event registration,
|
|
84
|
+
* whilst keeping triggering hidden.
|
|
85
|
+
*/
|
|
86
|
+
export declare abstract class BasicEventPublisher<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap> implements IBasicEventPublisher<TEventMap> {
|
|
87
|
+
protected _evManager: IBasicEventManager<TEventMap>;
|
|
88
|
+
constructor(evManager?: IBasicEventManager<TEventMap>);
|
|
89
|
+
on: IBasicEventManager<TEventMap>["on"];
|
|
90
|
+
off: IBasicEventManager<TEventMap>["off"];
|
|
91
|
+
register: IBasicEventManager<TEventMap>["register"];
|
|
92
|
+
protected _trigger: IBasicEventManager<TEventMap>["trigger"];
|
|
93
|
+
}
|
|
94
|
+
export declare function mixinEventPublisherUsingManager<TEventMap extends EventMapDef<TEventMap> = DefaultEventMap>(ontoObject: IBasicEventPublisher<TEventMap>, usingManager: IBasicEventManager<TEventMap>): void;
|
|
95
|
+
export default BasicEventManager;
|