@adcops/autocore-react 3.0.8 → 3.0.12

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/src/hub/index.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
3
3
  * Created Date: 2024-01-16 21:07:29
4
4
  * -----
5
- * Last Modified: 2024-03-08 10:20:06
5
+ * Last Modified: 2024-04-23 14:54:08
6
6
  * Modified By: ADC
7
7
  * -----
8
8
  *
@@ -10,7 +10,7 @@
10
10
 
11
11
  import { HubBase as Hub } from './HubBase';
12
12
  import { HubTauri } from './HubTauri';
13
- import { HubSocketIo } from "./HubSocketIo";
13
+ import { HubWebSocket } from "./HubWebSocket";
14
14
  import {HubSimulate} from "./HubSimulate"
15
15
 
16
16
  /**
@@ -18,17 +18,31 @@ import {HubSimulate} from "./HubSimulate"
18
18
  * @returns Hub
19
19
  */
20
20
  export function createHub(): Hub {
21
- if (window.__TAURI__) {
21
+
22
+ console.log("HELLOW THERRE!!!");
23
+
24
+ if (window.__TAURI__ !== undefined && window.__TAURI__ !== null) {
25
+ // Standalone Tauri application
26
+ console.log("HUB: Starting link to Tauri backend.");
22
27
  return new HubTauri();
23
28
  }
24
- else if (window.location.port !== '80' && window.location.port !== '443' ) {
29
+ else if (
30
+ window.location.port !== undefined
31
+ && window.location.port.length > 0
32
+ && window.location.port !== '80'
33
+ && window.location.port !== '8080'
34
+ && window.location.port !== '443'
35
+ ) {
25
36
 
26
37
  // We're loaded in some development environment
38
+ console.log("HUB: Starting HUB SIMULATOR.");
27
39
  return new HubSimulate();
28
40
 
29
41
  }
30
42
  else {
31
- return new HubSocketIo();
43
+ // A web-app that must communicate with a backend in another process.
44
+ console.log("HUB: Starting websocket connection.");
45
+ return new HubWebSocket();
32
46
  }
33
47
  }
34
48
 
@@ -37,5 +51,5 @@ export function createHub(): Hub {
37
51
  // application.
38
52
  export {HubBase as Hub} from './HubBase';
39
53
  export {HubTauri}
40
- export {HubSocketIo}
54
+ export {HubWebSocket}
41
55
  export {HubSimulate}
@@ -1,101 +0,0 @@
1
- import { HubBase } from './HubBase';
2
- /**
3
- * Hub for integrating with a webserver/backend using Socket.IO as the pipeline.
4
- *
5
- * The socket-io connection is expected to broadcast messages on the event
6
- * name: 'autocore://broadcast_event'
7
- *
8
- * This hub will capture those messages and dispatch them globally to any
9
- * listeners in the web app.
10
- *
11
- *
12
- * Example: Listen to an event 'xarm-position':
13
- * ```
14
- * const {subscribe, unsubscribe} = useContext(EventEmitterContext);
15
- * useEffect(() => {
16
- * const unsubscripeMp = subscribe('xarm-position', (value) => {
17
- * // The rust backend sent a JSON object of 3D position values.
18
- * setX(value.x);
19
- * setY(value.y);
20
- * setZ(value.z);
21
- * setA(value.roll);
22
- * setB(value.yaw);
23
- * setC(value.pitch);
24
- * });
25
- *
26
- * return () => {
27
- * unsubscribe(unsubscripeMp);
28
- * }
29
- *
30
- * }, [] );
31
- *
32
- * ```
33
- *
34
- * The hub should also be used for invoking events in the Tauri backend.
35
- * This example will call the function "update_count" in the rust backend, passing
36
- * the expected argument "count".
37
- * ```
38
- * const {invoke} = useContext(EventEmitterContext);
39
- * const incrementCount = () => {
40
- * count += 1;
41
- * invoke('update_count', {"count": count});
42
- * };
43
- * ```
44
- *
45
- * Of course, like any class derived from HubBase, the hub can be used to publish and subscribe to
46
- * topics in the front-end, without need of interacting with the Tauri backed.
47
- *
48
- * ```
49
- * const {dispatch, subscribe, unsubscribe} = useContext(EventEmitterContext);
50
- * const [controlPower, setControlPower] = useState(false);
51
- * useEffect(() => {
52
- * const unsubscribeControlPower = subscribe('value-simulator-bBit1', (value) => {
53
- * setControlPower(value);
54
- * });
55
- *
56
- *
57
- * return () => {
58
- * unsubscribe(unsubscribeControlPower);
59
- * }
60
- * }, [] );
61
- *
62
- * const onPbPressed = () => {
63
- * let count = 1;
64
- * dispatch({
65
- * topic: "my-awesome-topic",
66
- * payload: count
67
- * });
68
- * }
69
- *
70
- * ```
71
- *
72
- */
73
- export declare class HubSocketIo extends HubBase {
74
- private socket;
75
- /**
76
- * Constructor
77
- */
78
- constructor();
79
- /**
80
- * Invoke a method in the Socket.IO backend and wait for acknowledgment.
81
- *
82
- * @param fname method name
83
- * @param payload Optional data payload
84
- * @param timeout Timeout in milliseconds after which the promise is rejected if no response is received.
85
- * @returns A Promise that resolves to the response from the backend or rejects if a timeout occurs.
86
- */
87
- invoke(fname: string, payload?: object, timeout?: number): Promise<object>;
88
- /**
89
- * Disconnects the Socket.IO client.
90
- */
91
- disconnect(): void;
92
- /**
93
- * Emit an event to the Socket.IO server.
94
- * The intention is that the invoke method is used instead.
95
- *
96
- * @param eventName Name of the event to emit
97
- * @param payload Optional data payload
98
- */
99
- protected emit(eventName: string, payload?: object): void;
100
- }
101
- export default HubSocketIo;
@@ -1 +0,0 @@
1
- import{HubBase}from"./HubBase";import io from"socket.io-client";export class HubSocketIo extends HubBase{constructor(){super(),Object.defineProperty(this,"socket",{enumerable:!0,configurable:!0,writable:!0,value:void 0});const e=window.location.origin;this.socket=io(e),this.socket.on("autocore://broadcast_event",(e=>{let o=JSON.parse(e.payload);void 0!==o.topic&&null!==o.topic&&(void 0!==o.payload&&null!==o.payload?this.publish(o.topic,o.payload):this.publish(o.topic,void 0))}))}invoke(e,o,t=5e3){return new Promise(((i,s)=>{let c=!1;this.socket.emit(e,o,(e=>{c=!0,i(e)})),setTimeout((()=>{c||s(new Error(`Timeout: No response received within ${t} ms`))}),t)}))}disconnect(){this.socket.disconnect()}emit(e,o){this.socket.emit(e,o)}}export default HubSocketIo;
@@ -1,166 +0,0 @@
1
- /*
2
- * Copyright (C) 2023 Automated Design Corp. All Rights Reserved.
3
- * Created Date: 2023-12-17 10:38:21
4
- * Author: Thomas C. Bitsky Jr.
5
- * -----
6
- * Last Modified: 2024-03-08 09:46:20
7
- * Modified By: ADC
8
- * -----
9
- *
10
- */
11
-
12
- import { HubBase } from './HubBase';
13
- import io, { Socket } from 'socket.io-client';
14
-
15
- /**
16
- * Hub for integrating with a webserver/backend using Socket.IO as the pipeline.
17
- *
18
- * The socket-io connection is expected to broadcast messages on the event
19
- * name: 'autocore://broadcast_event'
20
- *
21
- * This hub will capture those messages and dispatch them globally to any
22
- * listeners in the web app.
23
- *
24
- *
25
- * Example: Listen to an event 'xarm-position':
26
- * ```
27
- * const {subscribe, unsubscribe} = useContext(EventEmitterContext);
28
- * useEffect(() => {
29
- * const unsubscripeMp = subscribe('xarm-position', (value) => {
30
- * // The rust backend sent a JSON object of 3D position values.
31
- * setX(value.x);
32
- * setY(value.y);
33
- * setZ(value.z);
34
- * setA(value.roll);
35
- * setB(value.yaw);
36
- * setC(value.pitch);
37
- * });
38
- *
39
- * return () => {
40
- * unsubscribe(unsubscripeMp);
41
- * }
42
- *
43
- * }, [] );
44
- *
45
- * ```
46
- *
47
- * The hub should also be used for invoking events in the Tauri backend.
48
- * This example will call the function "update_count" in the rust backend, passing
49
- * the expected argument "count".
50
- * ```
51
- * const {invoke} = useContext(EventEmitterContext);
52
- * const incrementCount = () => {
53
- * count += 1;
54
- * invoke('update_count', {"count": count});
55
- * };
56
- * ```
57
- *
58
- * Of course, like any class derived from HubBase, the hub can be used to publish and subscribe to
59
- * topics in the front-end, without need of interacting with the Tauri backed.
60
- *
61
- * ```
62
- * const {dispatch, subscribe, unsubscribe} = useContext(EventEmitterContext);
63
- * const [controlPower, setControlPower] = useState(false);
64
- * useEffect(() => {
65
- * const unsubscribeControlPower = subscribe('value-simulator-bBit1', (value) => {
66
- * setControlPower(value);
67
- * });
68
- *
69
- *
70
- * return () => {
71
- * unsubscribe(unsubscribeControlPower);
72
- * }
73
- * }, [] );
74
- *
75
- * const onPbPressed = () => {
76
- * let count = 1;
77
- * dispatch({
78
- * topic: "my-awesome-topic",
79
- * payload: count
80
- * });
81
- * }
82
- *
83
- * ```
84
- *
85
- */
86
- export class HubSocketIo extends HubBase {
87
- private socket: Socket;
88
-
89
- /**
90
- * Constructor
91
- */
92
- constructor() {
93
- super();
94
-
95
- // Initialize the Socket.IO client to connect to the same domain and protocol
96
- const socketUrl = window.location.origin;
97
-
98
- this.socket = io(socketUrl);
99
-
100
- // Listen for a custom event from the backend
101
- this.socket.on('autocore://broadcast_event', (ev: any) => {
102
- let objPayload = JSON.parse(ev.payload);
103
-
104
- if (objPayload.topic !== undefined && objPayload.topic !== null ) {
105
-
106
- if ( objPayload.payload !== undefined && objPayload.payload !== null ) {
107
- this.publish(objPayload.topic, objPayload.payload);
108
- }
109
- else {
110
- this.publish(objPayload.topic, undefined);
111
- }
112
-
113
- }
114
- });
115
-
116
- }
117
-
118
- /**
119
- * Invoke a method in the Socket.IO backend and wait for acknowledgment.
120
- *
121
- * @param fname method name
122
- * @param payload Optional data payload
123
- * @param timeout Timeout in milliseconds after which the promise is rejected if no response is received.
124
- * @returns A Promise that resolves to the response from the backend or rejects if a timeout occurs.
125
- */
126
- invoke(fname: string, payload?: object, timeout: number = 5000): Promise<object> {
127
- return new Promise((resolve, reject) => {
128
- // Flag to track if the response was received
129
- let responseReceived = false;
130
-
131
- this.socket.emit(fname, payload, (response: object) => {
132
- responseReceived = true;
133
- resolve(response);
134
- });
135
-
136
- // Set a timeout to reject the promise if no response is received
137
- setTimeout(() => {
138
- if (!responseReceived) {
139
- reject(new Error(`Timeout: No response received within ${timeout} ms`));
140
- }
141
- }, timeout);
142
- });
143
- }
144
-
145
- /**
146
- * Disconnects the Socket.IO client.
147
- */
148
- disconnect(): void {
149
- this.socket.disconnect();
150
- }
151
-
152
-
153
- /**
154
- * Emit an event to the Socket.IO server.
155
- * The intention is that the invoke method is used instead.
156
- *
157
- * @param eventName Name of the event to emit
158
- * @param payload Optional data payload
159
- */
160
- protected emit(eventName: string, payload?: object): void {
161
- this.socket.emit(eventName, payload);
162
- }
163
-
164
- }
165
-
166
- export default HubSocketIo;