@connexis/react 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Connexis Realtime Team
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.
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import { RealtimeClient, SubscribeOptions, ConnectionState, Metrics } from '@connexis/core';
3
+
4
+ interface RealtimeProviderProps {
5
+ client: RealtimeClient;
6
+ children: React.ReactNode;
7
+ }
8
+ /**
9
+ * Context Provider enabling access to the Realtime Client across the React component tree.
10
+ */
11
+ declare const RealtimeProvider: React.FC<RealtimeProviderProps>;
12
+ /**
13
+ * Accesses the raw RealtimeClient instance from Context.
14
+ */
15
+ declare const useRealtime: () => RealtimeClient;
16
+ /**
17
+ * Hook to subscribe to connection state and performance metrics.
18
+ */
19
+ declare const useConnection: () => {
20
+ state: ConnectionState;
21
+ metrics: Metrics;
22
+ };
23
+ /**
24
+ * Hook to subscribe to a realtime topic with automatic subscription lifecycle management.
25
+ */
26
+ declare const useSubscription: <T = any>(topic: string, optionsOrHandler: SubscribeOptions | ((data: T) => void), handlerOrUndefined?: (data: T) => void) => void;
27
+ /**
28
+ * Semantic alias for `useSubscription` conforming to the useChannel requirement.
29
+ */
30
+ declare const useChannel: <T = any>(topic: string, optionsOrHandler: SubscribeOptions | ((data: T) => void), handlerOrUndefined?: (data: T) => void) => void;
31
+ /**
32
+ * Hook returning a callback to publish events to a topic.
33
+ */
34
+ declare const usePublish: () => (<K extends string, V = any>(topic: K, payload: V) => Promise<void>);
35
+
36
+ export { RealtimeProvider, type RealtimeProviderProps, useChannel, useConnection, usePublish, useRealtime, useSubscription };
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import { RealtimeClient, SubscribeOptions, ConnectionState, Metrics } from '@connexis/core';
3
+
4
+ interface RealtimeProviderProps {
5
+ client: RealtimeClient;
6
+ children: React.ReactNode;
7
+ }
8
+ /**
9
+ * Context Provider enabling access to the Realtime Client across the React component tree.
10
+ */
11
+ declare const RealtimeProvider: React.FC<RealtimeProviderProps>;
12
+ /**
13
+ * Accesses the raw RealtimeClient instance from Context.
14
+ */
15
+ declare const useRealtime: () => RealtimeClient;
16
+ /**
17
+ * Hook to subscribe to connection state and performance metrics.
18
+ */
19
+ declare const useConnection: () => {
20
+ state: ConnectionState;
21
+ metrics: Metrics;
22
+ };
23
+ /**
24
+ * Hook to subscribe to a realtime topic with automatic subscription lifecycle management.
25
+ */
26
+ declare const useSubscription: <T = any>(topic: string, optionsOrHandler: SubscribeOptions | ((data: T) => void), handlerOrUndefined?: (data: T) => void) => void;
27
+ /**
28
+ * Semantic alias for `useSubscription` conforming to the useChannel requirement.
29
+ */
30
+ declare const useChannel: <T = any>(topic: string, optionsOrHandler: SubscribeOptions | ((data: T) => void), handlerOrUndefined?: (data: T) => void) => void;
31
+ /**
32
+ * Hook returning a callback to publish events to a topic.
33
+ */
34
+ declare const usePublish: () => (<K extends string, V = any>(topic: K, payload: V) => Promise<void>);
35
+
36
+ export { RealtimeProvider, type RealtimeProviderProps, useChannel, useConnection, usePublish, useRealtime, useSubscription };
package/dist/index.js ADDED
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ RealtimeProvider: () => RealtimeProvider,
34
+ useChannel: () => useChannel,
35
+ useConnection: () => useConnection,
36
+ usePublish: () => usePublish,
37
+ useRealtime: () => useRealtime,
38
+ useSubscription: () => useSubscription
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+ var import_react = __toESM(require("react"));
42
+ var RealtimeContext = (0, import_react.createContext)(null);
43
+ var RealtimeProvider = ({ client, children }) => {
44
+ return import_react.default.createElement(RealtimeContext.Provider, { value: client }, children);
45
+ };
46
+ var useRealtime = () => {
47
+ const client = (0, import_react.useContext)(RealtimeContext);
48
+ if (!client) {
49
+ throw new Error("useRealtime must be used within a RealtimeProvider");
50
+ }
51
+ return client;
52
+ };
53
+ var useConnection = () => {
54
+ const client = useRealtime();
55
+ const [state, setState] = (0, import_react.useState)(client.state);
56
+ const [metrics, setMetrics] = (0, import_react.useState)(client.metrics);
57
+ (0, import_react.useEffect)(() => {
58
+ setState(client.state);
59
+ setMetrics(client.metrics);
60
+ const unsubState = client.on("stateChange", ({ state: state2 }) => {
61
+ setState(state2);
62
+ });
63
+ const unsubMetrics = client.on("metricsChange", (metrics2) => {
64
+ setMetrics(metrics2);
65
+ });
66
+ return () => {
67
+ unsubState();
68
+ unsubMetrics();
69
+ };
70
+ }, [client]);
71
+ return { state, metrics };
72
+ };
73
+ var useSubscription = (topic, optionsOrHandler, handlerOrUndefined) => {
74
+ const client = useRealtime();
75
+ const options = typeof optionsOrHandler === "object" ? optionsOrHandler : {};
76
+ const handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handlerOrUndefined;
77
+ if (!handler) {
78
+ throw new Error("Subscription handler function is required");
79
+ }
80
+ const handlerRef = (0, import_react.useRef)(handler);
81
+ handlerRef.current = handler;
82
+ const filterString = options.filter ? JSON.stringify(options.filter) : "";
83
+ (0, import_react.useEffect)(() => {
84
+ let active = true;
85
+ let unsubPromise = null;
86
+ let unsubscribe = null;
87
+ const subOpts = options.filter ? { filter: options.filter } : {};
88
+ unsubPromise = client.subscribe(topic, subOpts, (data) => {
89
+ if (active) {
90
+ handlerRef.current(data);
91
+ }
92
+ });
93
+ unsubPromise.then((unsub) => {
94
+ if (!active) {
95
+ unsub();
96
+ } else {
97
+ unsubscribe = unsub;
98
+ }
99
+ });
100
+ return () => {
101
+ active = false;
102
+ if (unsubscribe) {
103
+ unsubscribe();
104
+ } else if (unsubPromise) {
105
+ unsubPromise.then((unsub) => unsub());
106
+ }
107
+ };
108
+ }, [client, topic, filterString]);
109
+ };
110
+ var useChannel = useSubscription;
111
+ var usePublish = () => {
112
+ const client = useRealtime();
113
+ return (0, import_react.useCallback)(
114
+ async (topic, payload) => {
115
+ await client.publish(topic, payload);
116
+ },
117
+ [client]
118
+ );
119
+ };
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ RealtimeProvider,
123
+ useChannel,
124
+ useConnection,
125
+ usePublish,
126
+ useRealtime,
127
+ useSubscription
128
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,88 @@
1
+ // src/index.ts
2
+ import React, { createContext, useContext, useEffect, useState, useRef, useCallback } from "react";
3
+ var RealtimeContext = createContext(null);
4
+ var RealtimeProvider = ({ client, children }) => {
5
+ return React.createElement(RealtimeContext.Provider, { value: client }, children);
6
+ };
7
+ var useRealtime = () => {
8
+ const client = useContext(RealtimeContext);
9
+ if (!client) {
10
+ throw new Error("useRealtime must be used within a RealtimeProvider");
11
+ }
12
+ return client;
13
+ };
14
+ var useConnection = () => {
15
+ const client = useRealtime();
16
+ const [state, setState] = useState(client.state);
17
+ const [metrics, setMetrics] = useState(client.metrics);
18
+ useEffect(() => {
19
+ setState(client.state);
20
+ setMetrics(client.metrics);
21
+ const unsubState = client.on("stateChange", ({ state: state2 }) => {
22
+ setState(state2);
23
+ });
24
+ const unsubMetrics = client.on("metricsChange", (metrics2) => {
25
+ setMetrics(metrics2);
26
+ });
27
+ return () => {
28
+ unsubState();
29
+ unsubMetrics();
30
+ };
31
+ }, [client]);
32
+ return { state, metrics };
33
+ };
34
+ var useSubscription = (topic, optionsOrHandler, handlerOrUndefined) => {
35
+ const client = useRealtime();
36
+ const options = typeof optionsOrHandler === "object" ? optionsOrHandler : {};
37
+ const handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handlerOrUndefined;
38
+ if (!handler) {
39
+ throw new Error("Subscription handler function is required");
40
+ }
41
+ const handlerRef = useRef(handler);
42
+ handlerRef.current = handler;
43
+ const filterString = options.filter ? JSON.stringify(options.filter) : "";
44
+ useEffect(() => {
45
+ let active = true;
46
+ let unsubPromise = null;
47
+ let unsubscribe = null;
48
+ const subOpts = options.filter ? { filter: options.filter } : {};
49
+ unsubPromise = client.subscribe(topic, subOpts, (data) => {
50
+ if (active) {
51
+ handlerRef.current(data);
52
+ }
53
+ });
54
+ unsubPromise.then((unsub) => {
55
+ if (!active) {
56
+ unsub();
57
+ } else {
58
+ unsubscribe = unsub;
59
+ }
60
+ });
61
+ return () => {
62
+ active = false;
63
+ if (unsubscribe) {
64
+ unsubscribe();
65
+ } else if (unsubPromise) {
66
+ unsubPromise.then((unsub) => unsub());
67
+ }
68
+ };
69
+ }, [client, topic, filterString]);
70
+ };
71
+ var useChannel = useSubscription;
72
+ var usePublish = () => {
73
+ const client = useRealtime();
74
+ return useCallback(
75
+ async (topic, payload) => {
76
+ await client.publish(topic, payload);
77
+ },
78
+ [client]
79
+ );
80
+ };
81
+ export {
82
+ RealtimeProvider,
83
+ useChannel,
84
+ useConnection,
85
+ usePublish,
86
+ useRealtime,
87
+ useSubscription
88
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@connexis/react",
3
+ "version": "1.0.0",
4
+ "description": "React hooks and provider for @connexis",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "peerDependencies": {
16
+ "react": "^18.0.0 || ^19.0.0"
17
+ },
18
+ "dependencies": {
19
+ "@connexis/core": "1.0.0"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "^18.3.3",
23
+ "react": "^18.3.1",
24
+ "typescript": "^5.5.2"
25
+ },
26
+ "scripts": {
27
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,133 @@
1
+ import React, { createContext, useContext, useEffect, useState, useRef, useCallback } from 'react';
2
+ import { RealtimeClient, ConnectionState, Metrics, SubscribeOptions } from '@connexis/core';
3
+
4
+ const RealtimeContext = createContext<RealtimeClient | null>(null);
5
+
6
+ export interface RealtimeProviderProps {
7
+ client: RealtimeClient;
8
+ children: React.ReactNode;
9
+ }
10
+
11
+ /**
12
+ * Context Provider enabling access to the Realtime Client across the React component tree.
13
+ */
14
+ export const RealtimeProvider: React.FC<RealtimeProviderProps> = ({ client, children }) => {
15
+ return React.createElement(RealtimeContext.Provider, { value: client }, children);
16
+ };
17
+
18
+ /**
19
+ * Accesses the raw RealtimeClient instance from Context.
20
+ */
21
+ export const useRealtime = (): RealtimeClient => {
22
+ const client = useContext(RealtimeContext);
23
+ if (!client) {
24
+ throw new Error('useRealtime must be used within a RealtimeProvider');
25
+ }
26
+ return client;
27
+ };
28
+
29
+ /**
30
+ * Hook to subscribe to connection state and performance metrics.
31
+ */
32
+ export const useConnection = (): { state: ConnectionState; metrics: Metrics } => {
33
+ const client = useRealtime();
34
+ const [state, setState] = useState<ConnectionState>(client.state);
35
+ const [metrics, setMetrics] = useState<Metrics>(client.metrics);
36
+
37
+ useEffect(() => {
38
+ // Initial sync
39
+ setState(client.state);
40
+ setMetrics(client.metrics);
41
+
42
+ const unsubState = client.on('stateChange', ({ state }) => {
43
+ setState(state);
44
+ });
45
+
46
+ const unsubMetrics = client.on('metricsChange', (metrics) => {
47
+ setMetrics(metrics);
48
+ });
49
+
50
+ return () => {
51
+ unsubState();
52
+ unsubMetrics();
53
+ };
54
+ }, [client]);
55
+
56
+ return { state, metrics };
57
+ };
58
+
59
+ /**
60
+ * Hook to subscribe to a realtime topic with automatic subscription lifecycle management.
61
+ */
62
+ export const useSubscription = <T = any>(
63
+ topic: string,
64
+ optionsOrHandler: SubscribeOptions | ((data: T) => void),
65
+ handlerOrUndefined?: (data: T) => void
66
+ ): void => {
67
+ const client = useRealtime();
68
+
69
+ const options = typeof optionsOrHandler === 'object' ? optionsOrHandler : {};
70
+ const handler = typeof optionsOrHandler === 'function' ? optionsOrHandler : handlerOrUndefined;
71
+
72
+ if (!handler) {
73
+ throw new Error('Subscription handler function is required');
74
+ }
75
+
76
+ const handlerRef = useRef(handler);
77
+ handlerRef.current = handler;
78
+
79
+ const filterString = options.filter ? JSON.stringify(options.filter) : '';
80
+
81
+ useEffect(() => {
82
+ let active = true;
83
+ let unsubPromise: Promise<() => Promise<void>> | null = null;
84
+ let unsubscribe: (() => Promise<void>) | null = null;
85
+
86
+ const subOpts = options.filter ? { filter: options.filter } : {};
87
+
88
+ unsubPromise = client.subscribe(topic, subOpts, (data) => {
89
+ if (active) {
90
+ handlerRef.current(data);
91
+ }
92
+ });
93
+
94
+ unsubPromise.then((unsub) => {
95
+ if (!active) {
96
+ unsub();
97
+ } else {
98
+ unsubscribe = unsub;
99
+ }
100
+ });
101
+
102
+ return () => {
103
+ active = false;
104
+ if (unsubscribe) {
105
+ unsubscribe();
106
+ } else if (unsubPromise) {
107
+ unsubPromise.then((unsub) => unsub());
108
+ }
109
+ };
110
+ }, [client, topic, filterString]);
111
+ };
112
+
113
+ /**
114
+ * Semantic alias for `useSubscription` conforming to the useChannel requirement.
115
+ */
116
+ export const useChannel = useSubscription;
117
+
118
+ /**
119
+ * Hook returning a callback to publish events to a topic.
120
+ */
121
+ export const usePublish = (): (<K extends string, V = any>(
122
+ topic: K,
123
+ payload: V
124
+ ) => Promise<void>) => {
125
+ const client = useRealtime();
126
+
127
+ return useCallback(
128
+ async <K extends string, V = any>(topic: K, payload: V) => {
129
+ await client.publish(topic, payload);
130
+ },
131
+ [client]
132
+ );
133
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "jsx": "react-jsx"
7
+ },
8
+ "include": ["src/**/*"]
9
+ }