@adcops/autocore-react 3.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 +59 -0
- package/additional-docs/ButtonApiSpecs.md +48 -0
- package/additional-docs/GlobalEventEmitter.md +244 -0
- package/additional-docs/general_recommendations.md +22 -0
- package/dist/components/DPad.css +522 -0
- package/dist/components/DPad.d.ts +34 -0
- package/dist/components/DPad.js +1 -0
- package/dist/components/Indicator.d.ts +81 -0
- package/dist/components/Indicator.js +1 -0
- package/dist/components/IndicatorButton.d.ts +137 -0
- package/dist/components/IndicatorButton.js +1 -0
- package/dist/components/IndicatorColor.d.ts +26 -0
- package/dist/components/IndicatorColor.js +1 -0
- package/dist/components/Osk.d.ts +14 -0
- package/dist/components/Osk.js +1 -0
- package/dist/components/OskDialog.d.ts +33 -0
- package/dist/components/OskDialog.js +1 -0
- package/dist/components/ValueDisplay.d.ts +99 -0
- package/dist/components/ValueDisplay.js +1 -0
- package/dist/components/osk.css +123 -0
- package/dist/core/EventEmitterContext.d.ts +223 -0
- package/dist/core/EventEmitterContext.js +1 -0
- package/dist/core/MaskPatterns.d.ts +71 -0
- package/dist/core/MaskPatterns.js +1 -0
- package/dist/core/NumerableTypes.d.ts +58 -0
- package/dist/core/NumerableTypes.js +1 -0
- package/dist/core/PositionContext.d.ts +38 -0
- package/dist/core/PositionContext.js +1 -0
- package/dist/core/UniqueId.d.ts +16 -0
- package/dist/core/UniqueId.js +1 -0
- package/dist/core/ValueSimulator.d.ts +71 -0
- package/dist/core/ValueSimulator.js +1 -0
- package/dist/core/hoc.d.ts +31 -0
- package/dist/core/hoc.js +1 -0
- package/dist/hub/HubBase.d.ts +169 -0
- package/dist/hub/HubBase.js +1 -0
- package/dist/hub/HubSimulate.d.ts +20 -0
- package/dist/hub/HubSimulate.js +1 -0
- package/dist/hub/HubSocketIo.d.ts +101 -0
- package/dist/hub/HubSocketIo.js +1 -0
- package/dist/hub/HubTauri.d.ts +86 -0
- package/dist/hub/HubTauri.js +1 -0
- package/dist/hub/index.d.ts +13 -0
- package/dist/hub/index.js +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +106 -0
- package/docs/assets/icons.js +15 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1414 -0
- package/docs/classes/components_Indicator.Indicator.html +105 -0
- package/docs/classes/components_IndicatorButton.IndicatorButton.html +119 -0
- package/docs/classes/components_OskDialog.OskDialog.html +117 -0
- package/docs/classes/components_ValueDisplay.ValueDisplay.html +104 -0
- package/docs/classes/core_ValueSimulator.ValueSimulator.html +41 -0
- package/docs/classes/hub_HubBase.HubBase.html +67 -0
- package/docs/classes/hub_HubSimulate.HubSimulate.html +72 -0
- package/docs/classes/hub_HubSocketIo.HubSocketIo.html +80 -0
- package/docs/classes/hub_HubTauri.HubTauri.html +80 -0
- package/docs/enums/components_DPad.VcJoyPadAction.html +10 -0
- package/docs/enums/components_DPad.VcJoyPadButtonId.html +7 -0
- package/docs/enums/components_IndicatorButton.ActionMode.html +6 -0
- package/docs/enums/components_IndicatorColor.IndicatorColor.html +13 -0
- package/docs/functions/components_DPad.VcDPad.html +5 -0
- package/docs/functions/components_Osk.Osk.html +5 -0
- package/docs/functions/core_EventEmitterContext.EventEmitterProvider.html +8 -0
- package/docs/functions/core_UniqueId.UniqueId.html +4 -0
- package/docs/functions/core_hoc.hocAddSubscription.html +6 -0
- package/docs/functions/hub.createHub.html +3 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +69 -0
- package/docs/interfaces/components_IndicatorButton.IndicatorButtonProps.html +635 -0
- package/docs/interfaces/components_IndicatorButton.IndicatorButtonState.html +10 -0
- package/docs/interfaces/core_DimensionsContext.IDimensionsContext.html +4 -0
- package/docs/interfaces/core_EventEmitterContext.Action.html +5 -0
- package/docs/interfaces/core_EventEmitterContext.EventEmitterContextType.html +18 -0
- package/docs/interfaces/core_EventEmitterContext.State.html +8 -0
- package/docs/interfaces/core_EventEmitterContext.Subscription.html +6 -0
- package/docs/modules/components_DPad.html +5 -0
- package/docs/modules/components_Indicator.html +4 -0
- package/docs/modules/components_IndicatorButton.html +7 -0
- package/docs/modules/components_IndicatorColor.html +2 -0
- package/docs/modules/components_Osk.html +3 -0
- package/docs/modules/components_OskDialog.html +3 -0
- package/docs/modules/components_ValueDisplay.html +3 -0
- package/docs/modules/core_DimensionsContext.html +4 -0
- package/docs/modules/core_EventEmitterContext.html +11 -0
- package/docs/modules/core_InputPatterns.html +2 -0
- package/docs/modules/core_NumerableTypes.html +2 -0
- package/docs/modules/core_UniqueId.html +2 -0
- package/docs/modules/core_ValueSimulator.html +3 -0
- package/docs/modules/core_hoc.html +3 -0
- package/docs/modules/hub.html +6 -0
- package/docs/modules/hub_HubBase.html +3 -0
- package/docs/modules/hub_HubSimulate.html +3 -0
- package/docs/modules/hub_HubSocketIo.html +3 -0
- package/docs/modules/hub_HubTauri.html +2 -0
- package/docs/types/core_EventEmitterContext.EmitterDispatchFunction.html +1 -0
- package/docs/types/core_EventEmitterContext.EmitterSubscribeFunction.html +1 -0
- package/docs/types/core_EventEmitterContext.EmitterUnsubscribeFunction.html +1 -0
- package/docs/types/core_NumerableTypes.NumerableFormatOptions.html +1 -0
- package/docs/types/core_hoc.HocAddSubscriptionProps.html +4 -0
- package/docs/variables/core_DimensionsContext.DimensionsContext.html +1 -0
- package/docs/variables/core_EventEmitterContext.EventEmitterContext.html +5 -0
- package/docs/variables/core_InputPatterns.InputPatterns.html +2 -0
- package/package.json +79 -0
- package/readme.md +140 -0
- package/src/components/DPad.css +522 -0
- package/src/components/DPad.tsx +94 -0
- package/src/components/Indicator.tsx +243 -0
- package/src/components/IndicatorButton.tsx +306 -0
- package/src/components/IndicatorColor.ts +36 -0
- package/src/components/Osk.tsx +193 -0
- package/src/components/OskDialog.tsx +165 -0
- package/src/components/ValueDisplay.tsx +181 -0
- package/src/components/osk.css +123 -0
- package/src/core/EventEmitterContext.tsx +394 -0
- package/src/core/MaskPatterns.ts +82 -0
- package/src/core/NumerableTypes.ts +81 -0
- package/src/core/PositionContext.ts +60 -0
- package/src/core/UniqueId.ts +41 -0
- package/src/core/ValueSimulator.ts +167 -0
- package/src/core/hoc.tsx +65 -0
- package/src/hub/HubBase.ts +293 -0
- package/src/hub/HubSimulate.ts +47 -0
- package/src/hub/HubSocketIo.ts +166 -0
- package/src/hub/HubTauri.ts +145 -0
- package/src/hub/index.ts +41 -0
- package/terser.config.cjs +25 -0
- package/todo.md +18 -0
- package/tools/copy-distribution-files.cjs +73 -0
- package/tools/minify.cjs +56 -0
- package/tsconfig.json +34 -0
- package/typedoc.json +13 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2024-01-17 11:45:10
|
|
4
|
+
* -----
|
|
5
|
+
* Last Modified: 2024-03-08 10:20:46
|
|
6
|
+
* Modified By: ADC
|
|
7
|
+
* -----
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import React, { createContext, ReactNode, useState, useMemo } from 'react';
|
|
13
|
+
import {createHub, Hub} from "../hub";
|
|
14
|
+
|
|
15
|
+
export {Hub};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Represents an event subsription.
|
|
19
|
+
*/
|
|
20
|
+
export interface Subscription {
|
|
21
|
+
/** ID of the subscription used for unsubscription. */
|
|
22
|
+
id: number;
|
|
23
|
+
/** Callback function. */
|
|
24
|
+
callback: React.Dispatch<any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Represents the payload data associated with an event.
|
|
30
|
+
*/
|
|
31
|
+
export interface State {
|
|
32
|
+
/**
|
|
33
|
+
* The optional data payload of the event.
|
|
34
|
+
*/
|
|
35
|
+
eventData?: any;
|
|
36
|
+
/** Callback subscription. A list of callback subscriptions matched to a topic. */
|
|
37
|
+
subscriptions: Record<string, Subscription[]>;
|
|
38
|
+
|
|
39
|
+
/** Tracks the next subscription ID that will be assigned. */
|
|
40
|
+
nextSubscriptionId: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* An action event published by the EventEmitterContext. Contains
|
|
45
|
+
* the topic identifying the event and the optional payload, which is
|
|
46
|
+
* a value of any type.
|
|
47
|
+
*/
|
|
48
|
+
export interface Action {
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The topic or identifier of the event.
|
|
52
|
+
*/
|
|
53
|
+
topic: string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The optional data payload associated with the event.
|
|
57
|
+
*/
|
|
58
|
+
payload?: any;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Type declaration for the EventEmitter dispatch function, which
|
|
63
|
+
* publishes an Action globally throughout the EventEmitterContext.
|
|
64
|
+
*/
|
|
65
|
+
export type EmitterDispatchFunction = (action: Action) => void;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Type declaration for the EventEmitter dispatch function, which
|
|
69
|
+
* receives an Action on a specific topic broadcast through the EventEmitterContext.
|
|
70
|
+
*/
|
|
71
|
+
export type EmitterSubscribeFunction = (action: Action) => void;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Type declaration for the EventEmitter unsubscribe function, which
|
|
75
|
+
* receives an Action on a specific topic broadcast through the EventEmitterContext.
|
|
76
|
+
*/
|
|
77
|
+
export type EmitterUnsubscribeFunction = (action: Action) => void;
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Defines the context for an event emitter used throughout a front-end application to manage and dispatch events.
|
|
82
|
+
* This interface includes methods for managing the application's state, handling global actions, communicating with the back end,
|
|
83
|
+
* subscribing to and unsubscribing from events, and accessing a global hub for event publishing and subscription management.
|
|
84
|
+
* It serves as a central point for event-driven interactions within the application, facilitating communication between
|
|
85
|
+
* components and the back end, as well as among components themselves.
|
|
86
|
+
*/
|
|
87
|
+
export interface EventEmitterContextType {
|
|
88
|
+
/**
|
|
89
|
+
* The current state of the event emitter, containing the latest event data.
|
|
90
|
+
*/
|
|
91
|
+
state: State;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A function to dispatch actions globally throughout the front-end,
|
|
95
|
+
* triggering state updates and events.
|
|
96
|
+
*
|
|
97
|
+
* @param action The action to dispatch, containing topic and optional payload.
|
|
98
|
+
*/
|
|
99
|
+
dispatch: (action: Action) => void;
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Invoke/send a message to the back end.
|
|
104
|
+
* This does NOT get published to the front end.
|
|
105
|
+
*/
|
|
106
|
+
invoke(fname: string, payload? : object) : Promise<object>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Subscribe to events identified by the topic.
|
|
110
|
+
* @param topic The subscription topic.
|
|
111
|
+
* @param callback The callback to signal.
|
|
112
|
+
* @returns number Subscription ID used to unsubscribe later.
|
|
113
|
+
*/
|
|
114
|
+
subscribe: (topic: string, callback: React.Dispatch<any>) => number;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Unsubscribe to events.
|
|
118
|
+
* @param subscriptionId The id of the subscription returned by the subscribe method.
|
|
119
|
+
* @returns
|
|
120
|
+
*/
|
|
121
|
+
unsubscribe: (subscriptionId : number) => void;
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Global hub for publishing and receiving events throughout the interface, and for exchanging
|
|
126
|
+
* data with the backend.
|
|
127
|
+
*/
|
|
128
|
+
hub : Hub | null;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Retrieves the current subscriptions. Used for debugging purposes.
|
|
132
|
+
* @param topic Optional. The topic to retrieve subscriptions for. If omitted, returns all subscriptions.
|
|
133
|
+
* @returns An object containing the current subscriptions, optionally filtered by topic.
|
|
134
|
+
*/
|
|
135
|
+
getSubscriptions: (topic?: string) => Record<string, Subscription[]> | Subscription[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A global context for managing event emission and subscription.
|
|
140
|
+
*
|
|
141
|
+
* Creates a React context for the EventEmitter system, providing a structured way to manage events and data flow
|
|
142
|
+
* in a React application. It serves as a global event bus that components can subscribe to or emit events, allowing for
|
|
143
|
+
* a loosely coupled architecture. Additionally, it provides a mechanism for invoking backend functions and managing
|
|
144
|
+
* subscriptions, making it easier to integrate React components with backend services.
|
|
145
|
+
*
|
|
146
|
+
* The context includes several key functionalities:
|
|
147
|
+
* - `state`: Maintains the current state of subscriptions and their identifiers.
|
|
148
|
+
* - `dispatch`: Allows components to emit events with specific topics and payloads, which can be listened to by other components.
|
|
149
|
+
* - `subscribe`: Enables components to listen to specific topics and react to those events by providing a callback function.
|
|
150
|
+
* - `unsubscribe`: Provides a way for components to stop listening to events, helping prevent memory leaks and unnecessary updates.
|
|
151
|
+
* - `invoke`: Facilitates calling backend functions with specific arguments and handling their responses asynchronously.
|
|
152
|
+
* - `getSubscriptions`: Offers insight into current active subscriptions, useful for debugging purposes.
|
|
153
|
+
*
|
|
154
|
+
* This context is essential for applications that require a high degree of inter-component communication or need to
|
|
155
|
+
* interact with a backend efficiently.
|
|
156
|
+
*
|
|
157
|
+
* For more information, see [Additional Documentation](../additional-docs/GlobalEventEmitter.md).
|
|
158
|
+
*
|
|
159
|
+
* ## Usage
|
|
160
|
+
*
|
|
161
|
+
* The entire application should be wrapped in the EventEmitterProvider.
|
|
162
|
+
*
|
|
163
|
+
* App.tsx
|
|
164
|
+
* ```
|
|
165
|
+
* import { EventEmitterProvider } from "@adcops/autocore-react/core/EventEmitterContext.js";
|
|
166
|
+
* function App() {
|
|
167
|
+
*
|
|
168
|
+
* return(
|
|
169
|
+
* <EventEmitterProvider>
|
|
170
|
+
* <PrimeReactProvider>
|
|
171
|
+
* <main>
|
|
172
|
+
* <section>
|
|
173
|
+
* <ContentView />
|
|
174
|
+
* </section>
|
|
175
|
+
* </main>
|
|
176
|
+
* </PrimeReactProvider>
|
|
177
|
+
* </EventEmitterProvider>
|
|
178
|
+
* );
|
|
179
|
+
*
|
|
180
|
+
* }
|
|
181
|
+
*
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* ### Catching and receiving events
|
|
185
|
+
* The EventEmitterContext creates an appropriate instance of the hub, which is derived from HubBase.
|
|
186
|
+
* That hub can be used to publish and subscribe to
|
|
187
|
+
* topics globally in the front-end, regardless of being connected to any backend.
|
|
188
|
+
* Usage within a component is simple.
|
|
189
|
+
*
|
|
190
|
+
* ```
|
|
191
|
+
* const {dispatch, subscribe, unsubscribe} = useContext(EventEmitterContext);
|
|
192
|
+
* const [controlPower, setControlPower] = useState(false);
|
|
193
|
+
* useEffect(() => {
|
|
194
|
+
* const unsubscribeControlPower = subscribe('value-simulator-bBit1', (value) => {
|
|
195
|
+
* setControlPower(value);
|
|
196
|
+
* });
|
|
197
|
+
*
|
|
198
|
+
*
|
|
199
|
+
* return () => {
|
|
200
|
+
* unsubscribe(unsubscribeControlPower);
|
|
201
|
+
* }
|
|
202
|
+
* }, [] );
|
|
203
|
+
*
|
|
204
|
+
* const onPbPressed = () => {
|
|
205
|
+
* let count = 1;
|
|
206
|
+
* dispatch({
|
|
207
|
+
* topic: "my-awesome-topic",
|
|
208
|
+
* payload: count
|
|
209
|
+
* });
|
|
210
|
+
* }
|
|
211
|
+
*
|
|
212
|
+
* ```
|
|
213
|
+
* The hub should also be used for invoking events in the backend.
|
|
214
|
+
* This example will call the function "update_count" in the backend, passing
|
|
215
|
+
* the expected argument "count". Details of the interaction between the Hub and
|
|
216
|
+
* the backend will be handled by the appropriate HubBase sub-class, and should
|
|
217
|
+
* be transparent to the front end.
|
|
218
|
+
*
|
|
219
|
+
* ```
|
|
220
|
+
* const {invoke} = useContext(EventEmitterContext);
|
|
221
|
+
* const incrementCount = () => {
|
|
222
|
+
* count += 1;
|
|
223
|
+
* invoke('update_count', {"count": count});
|
|
224
|
+
* };
|
|
225
|
+
*
|
|
226
|
+
* Subscribing to a topic is simple. The type of value received is specific
|
|
227
|
+
* to the topic.
|
|
228
|
+
*
|
|
229
|
+
* Example: Listen to an event 'xarm-position':
|
|
230
|
+
* ```
|
|
231
|
+
* const {subscribe, unsubscribe} = useContext(EventEmitterContext);
|
|
232
|
+
* useEffect(() => {
|
|
233
|
+
* const unsubscripeMp = subscribe('xarm-position', (value) => {
|
|
234
|
+
* // The publisher sent a JSON object of 3D position values.
|
|
235
|
+
* setX(value.x);
|
|
236
|
+
* setY(value.y);
|
|
237
|
+
* setZ(value.z);
|
|
238
|
+
* setA(value.roll);
|
|
239
|
+
* setB(value.yaw);
|
|
240
|
+
* setC(value.pitch);
|
|
241
|
+
* });
|
|
242
|
+
*
|
|
243
|
+
* return () => {
|
|
244
|
+
* unsubscribe(unsubscripeMp);
|
|
245
|
+
* }
|
|
246
|
+
*
|
|
247
|
+
* }, [] );
|
|
248
|
+
*
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* For applications that need to access the instance of the hub, get the current instance
|
|
252
|
+
* from the EventEmitterContext:
|
|
253
|
+
*
|
|
254
|
+
* ```
|
|
255
|
+
* const {hub} = useContext(EventEmitterContext);
|
|
256
|
+
* * ```
|
|
257
|
+
*
|
|
258
|
+
*
|
|
259
|
+
*/
|
|
260
|
+
export const EventEmitterContext = createContext<EventEmitterContextType>({
|
|
261
|
+
state: { subscriptions: {}, nextSubscriptionId : 1 },
|
|
262
|
+
dispatch: () => { },
|
|
263
|
+
subscribe: () => { return 0; }, // Placeholder for subscription logic
|
|
264
|
+
invoke: async (fname: string, payload?: object) => {
|
|
265
|
+
fname;
|
|
266
|
+
payload;
|
|
267
|
+
// Placeholder for invoke logic
|
|
268
|
+
// Implement the logic to send a message to the backend and return a promise
|
|
269
|
+
return Promise.resolve({}); // Example placeholder, replace with actual implementation
|
|
270
|
+
},
|
|
271
|
+
unsubscribe: (subscriptionId: number) => { subscriptionId;}, // Placeholder for unsubscription logic
|
|
272
|
+
hub: null,
|
|
273
|
+
getSubscriptions: () => { return []; }
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A React component that provides the EventEmitterContext to its children.
|
|
279
|
+
*
|
|
280
|
+
* Wraps child components with the context, enabling them to access and interact
|
|
281
|
+
* with the event emitter.
|
|
282
|
+
*
|
|
283
|
+
* @param children The child components to be wrapped in the context.
|
|
284
|
+
*
|
|
285
|
+
* ## Usage
|
|
286
|
+
*
|
|
287
|
+
* The entire application should be wrapped in the EventEmitterProvider.
|
|
288
|
+
*
|
|
289
|
+
* App.tsx
|
|
290
|
+
* ```
|
|
291
|
+
* import { EventEmitterProvider } from "@adcops/autocore-react/core/EventEmitterContext.js";
|
|
292
|
+
* function App() {
|
|
293
|
+
*
|
|
294
|
+
* return(
|
|
295
|
+
* <EventEmitterProvider>
|
|
296
|
+
* <PrimeReactProvider>
|
|
297
|
+
* <main>
|
|
298
|
+
* <section>
|
|
299
|
+
* <ContentView />
|
|
300
|
+
* </section>
|
|
301
|
+
* </main>
|
|
302
|
+
* </PrimeReactProvider>
|
|
303
|
+
* </EventEmitterProvider>
|
|
304
|
+
* );
|
|
305
|
+
*
|
|
306
|
+
* }
|
|
307
|
+
*
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
export const EventEmitterProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
|
311
|
+
const [state, setState] = useState<State>({ subscriptions: {}, nextSubscriptionId : 1 });
|
|
312
|
+
|
|
313
|
+
// Memoize the hub instance so it's only created once
|
|
314
|
+
const hub = useMemo(() => createHub(), []);
|
|
315
|
+
|
|
316
|
+
const dispatch = (action: Action) => {
|
|
317
|
+
const { topic, payload } = action;
|
|
318
|
+
|
|
319
|
+
setState(prevState => {
|
|
320
|
+
prevState.subscriptions[topic]?.forEach(sub => sub.callback(payload));
|
|
321
|
+
return { ...prevState, eventData: payload };
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const subscribe = (topic: string, callback: React.Dispatch<any>) : number => {
|
|
326
|
+
|
|
327
|
+
const subscriptionId = state.nextSubscriptionId;
|
|
328
|
+
|
|
329
|
+
setState(prevState => ({
|
|
330
|
+
...prevState,
|
|
331
|
+
subscriptions: {
|
|
332
|
+
...prevState.subscriptions,
|
|
333
|
+
[topic]: [...(prevState.subscriptions[topic] || []), { id: subscriptionId, callback }]
|
|
334
|
+
},
|
|
335
|
+
nextSubscriptionId: prevState.nextSubscriptionId + 1
|
|
336
|
+
}));
|
|
337
|
+
|
|
338
|
+
return subscriptionId;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
const unsubscribe = (subscriptionId: number) => {
|
|
343
|
+
setState(prevState => {
|
|
344
|
+
const newSubscriptions = { ...prevState.subscriptions };
|
|
345
|
+
|
|
346
|
+
// Iterate through all topics
|
|
347
|
+
for (const topic in newSubscriptions) {
|
|
348
|
+
if (newSubscriptions.hasOwnProperty(topic)) {
|
|
349
|
+
// Filter out the subscription with the given ID
|
|
350
|
+
newSubscriptions[topic] = newSubscriptions[topic].filter(sub => sub.id !== subscriptionId);
|
|
351
|
+
|
|
352
|
+
// If the topic now has no subscriptions, you can optionally delete the topic key
|
|
353
|
+
if (newSubscriptions[topic].length === 0) {
|
|
354
|
+
delete newSubscriptions[topic];
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return { ...prevState, subscriptions: newSubscriptions };
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
const getSubscriptions = (topic?: string): Record<string, Subscription[]> | Subscription[] => {
|
|
365
|
+
if (topic) {
|
|
366
|
+
// Return subscriptions for the provided topic, or an empty array if the topic doesn't exist
|
|
367
|
+
return state.subscriptions[topic] || [];
|
|
368
|
+
} else {
|
|
369
|
+
// Return all subscriptions
|
|
370
|
+
return state.subscriptions;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// Provide the memoized hub instance in the context value
|
|
375
|
+
const contextValue = useMemo(() => ({
|
|
376
|
+
state,
|
|
377
|
+
dispatch,
|
|
378
|
+
subscribe,
|
|
379
|
+
unsubscribe,
|
|
380
|
+
invoke: hub.invoke,
|
|
381
|
+
hub,
|
|
382
|
+
getSubscriptions
|
|
383
|
+
}), [state, hub]);
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
hub.setContext(contextValue);
|
|
387
|
+
|
|
388
|
+
return (
|
|
389
|
+
<EventEmitterContext.Provider value={contextValue}>
|
|
390
|
+
{children}
|
|
391
|
+
</EventEmitterContext.Provider>
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* (C) Copyright 2021 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* File Created: Thursday, 1st April 2021 12:30:41 pm
|
|
4
|
+
* Author: Thomas C. Bitsky Jr. Automated Design Corp.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @fileoverview
|
|
9
|
+
* Provides a collection of pre-defined patterns for masking/validating different types of user input or data string.
|
|
10
|
+
* These patterns come up so regularly in our work that it seemed best to standardize and centralize their definition.
|
|
11
|
+
*
|
|
12
|
+
* These patterns can be used to ensure that user inputs conform to expected formats, such as real numbers (with or without
|
|
13
|
+
* decimal points), positive real numbers, integers, and positive integers. Utilizing these patterns simplifies the process
|
|
14
|
+
* of validating and filtering input in forms or any other user input scenarios.
|
|
15
|
+
*
|
|
16
|
+
* The patterns are defined as regular expressions and are encapsulated within the `InputPatterns` constant for easy access.
|
|
17
|
+
* They can be used directly with input validation functions or in conjunction with form validation libraries to enforce
|
|
18
|
+
* input constraints.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Regular expressions Common input patterns for matching different input or expected data types.
|
|
23
|
+
*/
|
|
24
|
+
export const RegExMaskPatterns = {
|
|
25
|
+
/**
|
|
26
|
+
* Regular expression pattern for matching real numbers.
|
|
27
|
+
* This pattern matches any real number, including negative and positive numbers with or without a decimal point.
|
|
28
|
+
* Examples of valid inputs: "123", "-123", "123.456", "-123.456", ".456", "-.456"
|
|
29
|
+
*/
|
|
30
|
+
RealNumber: /^-?\d*(\.)?(\d+)?$/,
|
|
31
|
+
/**
|
|
32
|
+
* Regular expression pattern for matching positive real numbers.
|
|
33
|
+
* This pattern matches any positive real number with or without a decimal point, excluding negative numbers.
|
|
34
|
+
* Examples of valid inputs: "123", "123.456", ".456"
|
|
35
|
+
*/
|
|
36
|
+
RealNumberPositive: /^\d*(\.)?(\d+)?$/,
|
|
37
|
+
/**
|
|
38
|
+
* Regular expression pattern for matching integers.
|
|
39
|
+
* This pattern matches any integer, including negative and positive integers without a decimal point.
|
|
40
|
+
* Examples of valid inputs: "123", "-123"
|
|
41
|
+
*/
|
|
42
|
+
Integer: /^-?\d*$/,
|
|
43
|
+
/**
|
|
44
|
+
* Regular expression pattern for matching positive integers.
|
|
45
|
+
* This pattern matches any positive integer without a decimal point, excluding negative numbers.
|
|
46
|
+
* Examples of valid inputs: "123"
|
|
47
|
+
*/
|
|
48
|
+
IntegerPositive: /^\d+$/
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Patterns for use with PrimeReact.InputMask components
|
|
55
|
+
*/
|
|
56
|
+
export const PrimeReactMaskPatterns = {
|
|
57
|
+
/** USA Social Security Number */
|
|
58
|
+
SocialSecurityNumber: "999-99-9999",
|
|
59
|
+
/** USA phone number with optional extension */
|
|
60
|
+
PhoneNumberUS: "(999) 999-9999? x99999",
|
|
61
|
+
/** International phone number with optional extension */
|
|
62
|
+
PhoneNumberInternational: "+999 999 999 9999? x99999",
|
|
63
|
+
/** International phone number with optional extension */
|
|
64
|
+
PhoneNumberJapan: "+81 9999 999 9999? x99999",
|
|
65
|
+
/** International phone number with optional extension */
|
|
66
|
+
PhoneNumberChina: "+86 999 999 9999? x99999",
|
|
67
|
+
/** International phone number with optional extension */
|
|
68
|
+
PhoneNumberVietnam: "+84 999 999 9999? x99999",
|
|
69
|
+
/** International phone number with optional extension */
|
|
70
|
+
PhoneNumberSouthKorea: "+82 99 9999 9999 x99999",
|
|
71
|
+
/** International phone number with optional extension */
|
|
72
|
+
PhoneNumberCanada: "+1 (999) 999-9999 x99999",
|
|
73
|
+
/** International phone number with optional extension */
|
|
74
|
+
PhoneNumberMexico: "+52 999 999 9999 x99999",
|
|
75
|
+
/** International phone number with optional extension */
|
|
76
|
+
PhoneNumberScotland: "+44 9999 999999 x99999",
|
|
77
|
+
/** International phone number with optional extension */
|
|
78
|
+
PhoneNumberEngland: "+44 9999 999999 x99999",
|
|
79
|
+
/** International phone number with optional extension */
|
|
80
|
+
PhoneNumberIreland: "+353 99 999 9999 x99999",
|
|
81
|
+
|
|
82
|
+
} as const;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2024-01-16 14:19:17
|
|
4
|
+
* -----
|
|
5
|
+
* Last Modified: 2024-03-08 07:55:25
|
|
6
|
+
* Modified By: ADC
|
|
7
|
+
* -----
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @fileoverview
|
|
13
|
+
*
|
|
14
|
+
* These types enhance TypeScript support for the `numerable` library, which does not directly
|
|
15
|
+
* export the `NumerableFormatNumberOptions` type. By extracting and refining the type definitions
|
|
16
|
+
* for the `format` function's parameters, including handling its overloaded signatures, this file
|
|
17
|
+
* makes it easier to use `numerable` in TypeScript and React projects.
|
|
18
|
+
*
|
|
19
|
+
* The process involves creating a type that captures the overloads of the `format` function and
|
|
20
|
+
* then mapping over these overloads to extract their parameters. This technique enables the
|
|
21
|
+
* extraction of the specific options type `NumerableFormatNumberOptions` used by the `format`
|
|
22
|
+
* function, thereby providing type safety and IntelliSense support in TypeScript environments.
|
|
23
|
+
*
|
|
24
|
+
* The approach used here is based on a solution for handling function overloads in TypeScript,
|
|
25
|
+
* as discussed in a StackOverflow answer: https://stackoverflow.com/a/59538756/10049787.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { format } from 'numerable';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Type helper to capture the overload signatures of a function.
|
|
32
|
+
* This utility type takes a function with multiple overloads and maps them to a tuple of
|
|
33
|
+
* functions, each representing one of the overloads.
|
|
34
|
+
*/
|
|
35
|
+
type NumerableFormatOverloads<T> = T extends {
|
|
36
|
+
(...args: infer A1): infer R1;
|
|
37
|
+
(...args: infer A2): infer R2;
|
|
38
|
+
(...args: infer A3): infer R3;
|
|
39
|
+
}
|
|
40
|
+
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
|
|
41
|
+
: T extends {
|
|
42
|
+
(...args: infer A1): infer R1;
|
|
43
|
+
(...args: infer A2): infer R2;
|
|
44
|
+
}
|
|
45
|
+
? [(...args: A1) => R1, (...args: A2) => R2]
|
|
46
|
+
: T extends {
|
|
47
|
+
(...args: infer A1): infer R1;
|
|
48
|
+
}
|
|
49
|
+
? [(...args: A1) => R1]
|
|
50
|
+
: any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Extracts parameters from a tuple of function types.
|
|
54
|
+
* This utility type maps over a tuple of functions (representing overloads) to produce a tuple
|
|
55
|
+
* of their respective parameter types.
|
|
56
|
+
*/
|
|
57
|
+
type OverloadedParameters<T> = NumerableFormatOverloads<T> extends infer O
|
|
58
|
+
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
{ [K in keyof O]: Parameters<Extract<O[K], (...args: any) => any>> }
|
|
60
|
+
: never;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Type representing the parameters of the `numerable.format` function, taking into account its
|
|
64
|
+
* overloaded signatures.
|
|
65
|
+
*/
|
|
66
|
+
type NumerableFormatParams = OverloadedParameters<typeof format>;
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Type specifically for the options parameter of the `numerable.format` function.
|
|
71
|
+
* This extracts the type of the third parameter (options) from the first overload of `format`,
|
|
72
|
+
* as this is where the formatting options are specified.
|
|
73
|
+
*/
|
|
74
|
+
type NumerableFormatNumberOptions = NumerableFormatParams[0][2];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Exported type for external use, providing a non-nullable version of the formatting options.
|
|
78
|
+
* This type is intended for use wherever `numerable.format` options are needed, enhancing
|
|
79
|
+
* code readability and type safety.
|
|
80
|
+
*/
|
|
81
|
+
export type NumerableFormatOptions = NonNullable<NumerableFormatNumberOptions>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2024-01-16 14:19:52
|
|
4
|
+
* -----
|
|
5
|
+
* Last Modified: 2024-03-08 07:48:18
|
|
6
|
+
* Modified By: ADC
|
|
7
|
+
* -----
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
import { createContext } from 'react';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Interface representing the position and scale context of a component within its parent container.
|
|
18
|
+
*
|
|
19
|
+
* This context is designed to provide components with necessary scaling and positioning
|
|
20
|
+
* information, such as for rendering elements correctly in absolute positioning or for
|
|
21
|
+
* adjusting layout based on dynamic offsets.
|
|
22
|
+
*/
|
|
23
|
+
export interface IPositionContext {
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Represents the scale factor applied to the dimensions of elements.
|
|
27
|
+
* A scale of 1 indicates no scaling, while values greater or less than 1
|
|
28
|
+
* indicate zoomed-in or zoomed-out states respectively.
|
|
29
|
+
*/
|
|
30
|
+
scale: number;
|
|
31
|
+
/**
|
|
32
|
+
* Represents the horizontal offset that should be applied to the position
|
|
33
|
+
* of elements. This can be used to adjust element positioning in response to
|
|
34
|
+
* dynamic layout changes or to align elements within a scaled environment.
|
|
35
|
+
*/
|
|
36
|
+
xOffset: number;
|
|
37
|
+
/**
|
|
38
|
+
* Represents the vertical offset that should be applied to the position
|
|
39
|
+
* of elements. Similar to `xOffset`, this is used for vertical alignment and
|
|
40
|
+
* positioning adjustments in a dynamic or scaled layout.
|
|
41
|
+
*/
|
|
42
|
+
yOffset: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Creates a React context for position and scaling information of a component using absolute positioning.
|
|
48
|
+
* This context provides a standardized way to access and react to changes in scale and positioning offsets,
|
|
49
|
+
* enabling components to adjust their layout and rendering behavior dynamically.
|
|
50
|
+
*
|
|
51
|
+
* The default context values are set to assume no scaling (`scale: 1`) and no offsets (`xOffset: 0, yOffset: 0`),
|
|
52
|
+
* which corresponds to the initial state in many applications before any dynamic adjustments are made.
|
|
53
|
+
*/
|
|
54
|
+
export const DimensionsContext = createContext<IPositionContext>({
|
|
55
|
+
scale: 1, /** Default scaling factor indicating no scaling. */
|
|
56
|
+
xOffset: 0, /** Default horizontal offset indicating no shift. */
|
|
57
|
+
yOffset: 0, /** Default vertical offset indicating no shift. */
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export default DimensionsContext;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate a unique UUID in the form of RF4122.
|
|
6
|
+
* @returns string
|
|
7
|
+
*/
|
|
8
|
+
const PrivateUniqueId = (function()
|
|
9
|
+
{
|
|
10
|
+
function chr4()
|
|
11
|
+
{
|
|
12
|
+
return Math.random().toString(16).slice(-4);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return chr4() + chr4() +
|
|
16
|
+
'-' + chr4() +
|
|
17
|
+
'-' + chr4() +
|
|
18
|
+
'-' + chr4() +
|
|
19
|
+
'-' + chr4() + chr4() + chr4();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generates a unique UUID per RFC4122.
|
|
25
|
+
* 8 chars, followed by 3 groups of 4 chars, followed by 12 chars.
|
|
26
|
+
*
|
|
27
|
+
* @returns string UUID
|
|
28
|
+
*
|
|
29
|
+
* ## Remarks
|
|
30
|
+
* NOTE: This format of 8 chars, followed by 3 groups of 4 chars, followed by 12 chars
|
|
31
|
+
* is known as a UUID and is defined in RFC4122 and is a standard for generating unique IDs.
|
|
32
|
+
* This function DOES NOT implement this standard. It simply outputs a string
|
|
33
|
+
* that looks similar. The standard is found here: https://www.ietf.org/rfc/rfc4122.txt
|
|
34
|
+
*
|
|
35
|
+
* Should avoid duplicates over 10-millions subsequent calls.
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
export function UniqueId() : string {
|
|
39
|
+
return PrivateUniqueId();
|
|
40
|
+
}
|
|
41
|
+
|