dreaction-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/README.md +255 -0
- package/lib/components/ConfigPanel.d.ts +12 -0
- package/lib/components/ConfigPanel.d.ts.map +1 -0
- package/lib/components/ConfigPanel.js +151 -0
- package/lib/dreaction.d.ts +32 -0
- package/lib/dreaction.d.ts.map +1 -0
- package/lib/dreaction.js +110 -0
- package/lib/hooks.d.ts +17 -0
- package/lib/hooks.d.ts.map +1 -0
- package/lib/hooks.js +111 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +23 -0
- package/lib/plugins/localStorage.d.ts +14 -0
- package/lib/plugins/localStorage.d.ts.map +1 -0
- package/lib/plugins/localStorage.js +85 -0
- package/lib/plugins/networking.d.ts +11 -0
- package/lib/plugins/networking.d.ts.map +1 -0
- package/lib/plugins/networking.js +263 -0
- package/lib/plugins/trackGlobalErrors.d.ts +25 -0
- package/lib/plugins/trackGlobalErrors.d.ts.map +1 -0
- package/lib/plugins/trackGlobalErrors.js +149 -0
- package/lib/plugins/trackGlobalLogs.d.ts +10 -0
- package/lib/plugins/trackGlobalLogs.d.ts.map +1 -0
- package/lib/plugins/trackGlobalLogs.js +42 -0
- package/package.json +35 -0
- package/src/components/ConfigPanel.tsx +247 -0
- package/src/dreaction.ts +181 -0
- package/src/hooks.ts +129 -0
- package/src/index.ts +17 -0
- package/src/plugins/localStorage.ts +100 -0
- package/src/plugins/networking.ts +333 -0
- package/src/plugins/trackGlobalErrors.ts +203 -0
- package/src/plugins/trackGlobalLogs.ts +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# DReaction React
|
|
2
|
+
|
|
3
|
+
DReaction client library for React web applications with plugin support for debugging and monitoring.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install dreaction-react
|
|
9
|
+
# or
|
|
10
|
+
yarn add dreaction-react
|
|
11
|
+
# or
|
|
12
|
+
pnpm add dreaction-react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { dreaction } from 'dreaction-react';
|
|
19
|
+
|
|
20
|
+
// Configure and enable plugins
|
|
21
|
+
dreaction
|
|
22
|
+
.configure({
|
|
23
|
+
host: 'localhost',
|
|
24
|
+
port: 9600,
|
|
25
|
+
name: 'My React App',
|
|
26
|
+
})
|
|
27
|
+
.useReact() // Enable all plugins
|
|
28
|
+
.connect(); // Connect to DReaction server
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage with ConfigPanel
|
|
32
|
+
|
|
33
|
+
For a better user experience, you can use the built-in `ConfigPanel` component:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { ConfigPanel } from 'dreaction-react';
|
|
37
|
+
|
|
38
|
+
function App() {
|
|
39
|
+
return (
|
|
40
|
+
<div>
|
|
41
|
+
<YourApp />
|
|
42
|
+
<ConfigPanel />
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Auto-Reconnect Feature
|
|
49
|
+
|
|
50
|
+
The `ConfigPanel` automatically remembers your connection settings and status. When you refresh the page:
|
|
51
|
+
- **Host and Port** settings are saved to localStorage
|
|
52
|
+
- **Connection state** is remembered
|
|
53
|
+
- If you were connected before refresh, it will **automatically reconnect**
|
|
54
|
+
|
|
55
|
+
This means you only need to configure and connect once, and all future page loads will automatically restore your connection!
|
|
56
|
+
|
|
57
|
+
**Important**: Make sure to call `.useReact()` to enable plugins before connecting:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// In your initialization file (e.g., utils/dreaction.ts)
|
|
61
|
+
import { dreaction } from 'dreaction-react';
|
|
62
|
+
|
|
63
|
+
dreaction
|
|
64
|
+
.configure({
|
|
65
|
+
info: {
|
|
66
|
+
appName: 'My App',
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
.useReact(); // This is required to enable plugins!
|
|
70
|
+
|
|
71
|
+
export { dreaction };
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Plugins
|
|
75
|
+
|
|
76
|
+
DReaction React comes with built-in plugins for debugging:
|
|
77
|
+
|
|
78
|
+
### 1. **Networking Plugin**
|
|
79
|
+
Intercepts and monitors network requests (fetch and XMLHttpRequest).
|
|
80
|
+
|
|
81
|
+
### 2. **localStorage Plugin**
|
|
82
|
+
Tracks all localStorage operations (setItem, removeItem, clear).
|
|
83
|
+
|
|
84
|
+
### 3. **Console Logs Plugin**
|
|
85
|
+
Captures console.log, console.warn, console.debug, and console.info calls.
|
|
86
|
+
|
|
87
|
+
### 4. **Global Errors Plugin**
|
|
88
|
+
Catches unhandled errors and promise rejections.
|
|
89
|
+
|
|
90
|
+
## Plugin Configuration
|
|
91
|
+
|
|
92
|
+
You can customize plugin behavior:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
dreaction.useReact({
|
|
96
|
+
// Enable/disable error tracking
|
|
97
|
+
errors: true, // or provide options: { veto: (frame) => boolean }
|
|
98
|
+
|
|
99
|
+
// Enable/disable console logging
|
|
100
|
+
log: true,
|
|
101
|
+
|
|
102
|
+
// Configure localStorage monitoring
|
|
103
|
+
localStorage: {
|
|
104
|
+
ignore: ['SOME_KEY_TO_IGNORE'] // Keys to ignore
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
// Configure network monitoring
|
|
108
|
+
networking: {
|
|
109
|
+
ignoreUrls: /analytics|tracking/, // URLs to ignore
|
|
110
|
+
ignoreContentTypes: /image/, // Content types to ignore
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Disable Specific Plugins
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
dreaction.useReact({
|
|
119
|
+
errors: false, // Disable error tracking
|
|
120
|
+
log: false, // Disable console logging
|
|
121
|
+
localStorage: false, // Disable localStorage monitoring
|
|
122
|
+
networking: false, // Disable network monitoring
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Data Watchers
|
|
127
|
+
|
|
128
|
+
Monitor React state in real-time:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { dreaction } from 'dreaction-react';
|
|
132
|
+
|
|
133
|
+
// Register a data watcher
|
|
134
|
+
export const { useDebugDataWatch: useDebugCounter } =
|
|
135
|
+
dreaction.registerDataWatcher('counter', 'text');
|
|
136
|
+
|
|
137
|
+
// Use in your component
|
|
138
|
+
function Counter() {
|
|
139
|
+
const [count, setCount] = useState(0);
|
|
140
|
+
|
|
141
|
+
// This will send updates to DReaction server
|
|
142
|
+
useDebugCounter(count);
|
|
143
|
+
|
|
144
|
+
return <div>Count: {count}</div>;
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Data Watch Types
|
|
149
|
+
|
|
150
|
+
- `'text'` - Display as text
|
|
151
|
+
- `'json'` - Display as formatted JSON
|
|
152
|
+
- `'list'` - Display as a list
|
|
153
|
+
|
|
154
|
+
## Custom Commands
|
|
155
|
+
|
|
156
|
+
Register custom commands that can be triggered from the DReaction app:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
dreaction.registerCustomCommand({
|
|
160
|
+
title: 'Clear Cache',
|
|
161
|
+
command: 'clearCache',
|
|
162
|
+
description: 'Clear application cache',
|
|
163
|
+
handler: async () => {
|
|
164
|
+
localStorage.clear();
|
|
165
|
+
return 'Cache cleared successfully';
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Commands with arguments
|
|
170
|
+
dreaction.registerCustomCommand({
|
|
171
|
+
title: 'Set User',
|
|
172
|
+
command: 'setUser',
|
|
173
|
+
description: 'Set current user',
|
|
174
|
+
args: [
|
|
175
|
+
{
|
|
176
|
+
name: 'userId',
|
|
177
|
+
type: 'string',
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
handler: async (args) => {
|
|
181
|
+
console.log('Setting user:', args.userId);
|
|
182
|
+
return `User set to: ${args.userId}`;
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## API Reference
|
|
188
|
+
|
|
189
|
+
### `dreaction.useReact(options?)`
|
|
190
|
+
|
|
191
|
+
Enable React plugins with optional configuration.
|
|
192
|
+
|
|
193
|
+
**Parameters:**
|
|
194
|
+
- `options.errors` - Error tracking options or boolean
|
|
195
|
+
- `options.log` - Enable console logging (boolean)
|
|
196
|
+
- `options.localStorage` - localStorage monitoring options or boolean
|
|
197
|
+
- `options.networking` - Network monitoring options or boolean
|
|
198
|
+
|
|
199
|
+
**Returns:** `dreaction` instance (chainable)
|
|
200
|
+
|
|
201
|
+
### `dreaction.configure(options)`
|
|
202
|
+
|
|
203
|
+
Configure DReaction client settings.
|
|
204
|
+
|
|
205
|
+
**Parameters:**
|
|
206
|
+
- `options.host` - Server host (default: 'localhost')
|
|
207
|
+
- `options.port` - Server port (default: 9600)
|
|
208
|
+
- `options.name` - Application name
|
|
209
|
+
- `options.info` - Additional info object
|
|
210
|
+
|
|
211
|
+
**Returns:** `dreaction` instance (chainable)
|
|
212
|
+
|
|
213
|
+
### `dreaction.connect()`
|
|
214
|
+
|
|
215
|
+
Connect to DReaction server and activate all plugins.
|
|
216
|
+
|
|
217
|
+
**Returns:** `dreaction` instance (chainable)
|
|
218
|
+
|
|
219
|
+
### `dreaction.close()`
|
|
220
|
+
|
|
221
|
+
Disconnect from server and deactivate plugins.
|
|
222
|
+
|
|
223
|
+
**Returns:** `dreaction` instance (chainable)
|
|
224
|
+
|
|
225
|
+
### `dreaction.registerDataWatcher(name, type, options?)`
|
|
226
|
+
|
|
227
|
+
Register a data watcher for monitoring React state.
|
|
228
|
+
|
|
229
|
+
**Parameters:**
|
|
230
|
+
- `name` - Watcher identifier
|
|
231
|
+
- `type` - Display type ('text' | 'json' | 'list')
|
|
232
|
+
- `options.enabled` - Enable/disable watcher (default: true in development)
|
|
233
|
+
|
|
234
|
+
**Returns:** Object with `useDebugDataWatch` hook
|
|
235
|
+
|
|
236
|
+
### `dreaction.registerCustomCommand(config)`
|
|
237
|
+
|
|
238
|
+
Register a custom command.
|
|
239
|
+
|
|
240
|
+
**Parameters:**
|
|
241
|
+
- `config.title` - Command title
|
|
242
|
+
- `config.command` - Command identifier
|
|
243
|
+
- `config.description` - Command description
|
|
244
|
+
- `config.args` - Array of argument definitions
|
|
245
|
+
- `config.handler` - Command handler function
|
|
246
|
+
|
|
247
|
+
**Returns:** Unregister function
|
|
248
|
+
|
|
249
|
+
## Example
|
|
250
|
+
|
|
251
|
+
Check out the complete example in `example/nextjs/` directory.
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface ConfigPanelProps {
|
|
2
|
+
/**
|
|
3
|
+
* Initial collapsed state
|
|
4
|
+
*/
|
|
5
|
+
defaultCollapsed?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Position of the panel
|
|
8
|
+
*/
|
|
9
|
+
position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
|
|
10
|
+
}
|
|
11
|
+
export declare function ConfigPanel({ defaultCollapsed, position, }: ConfigPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
//# sourceMappingURL=ConfigPanel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConfigPanel.d.ts","sourceRoot":"","sources":["../../src/components/ConfigPanel.tsx"],"names":[],"mappings":"AAGA,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;OAEG;IACH,QAAQ,CAAC,EAAE,WAAW,GAAG,UAAU,GAAG,cAAc,GAAG,aAAa,CAAC;CACtE;AAED,wBAAgB,WAAW,CAAC,EAC1B,gBAAuB,EACvB,QAAsB,GACvB,EAAE,gBAAgB,2CAqOlB"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConfigPanel = ConfigPanel;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const hooks_1 = require("../hooks");
|
|
7
|
+
function ConfigPanel({ defaultCollapsed = true, position = 'top-right', }) {
|
|
8
|
+
const [isCollapsed, setIsCollapsed] = (0, react_1.useState)(defaultCollapsed);
|
|
9
|
+
const { host, port, isConnected, updateHost, updatePort, connect, disconnect, } = (0, hooks_1.useDReactionConfig)();
|
|
10
|
+
const [localHost, setLocalHost] = (0, react_1.useState)(host);
|
|
11
|
+
const [localPort, setLocalPort] = (0, react_1.useState)(port.toString());
|
|
12
|
+
const handleConnect = () => {
|
|
13
|
+
updateHost(localHost);
|
|
14
|
+
updatePort(parseInt(localPort, 10));
|
|
15
|
+
connect();
|
|
16
|
+
};
|
|
17
|
+
const handleDisconnect = () => {
|
|
18
|
+
disconnect();
|
|
19
|
+
};
|
|
20
|
+
const positionStyles = {
|
|
21
|
+
'top-right': { top: '12px', right: '12px' },
|
|
22
|
+
'top-left': { top: '12px', left: '12px' },
|
|
23
|
+
'bottom-right': { bottom: '12px', right: '12px' },
|
|
24
|
+
'bottom-left': { bottom: '12px', left: '12px' },
|
|
25
|
+
};
|
|
26
|
+
const panelStyle = {
|
|
27
|
+
position: 'fixed',
|
|
28
|
+
...positionStyles[position],
|
|
29
|
+
backgroundColor: '#1a1a1a',
|
|
30
|
+
color: '#ffffff',
|
|
31
|
+
borderRadius: isCollapsed ? '6px' : '8px',
|
|
32
|
+
boxShadow: isCollapsed
|
|
33
|
+
? '0 2px 8px rgba(0, 0, 0, 0.2)'
|
|
34
|
+
: '0 4px 12px rgba(0, 0, 0, 0.3)',
|
|
35
|
+
zIndex: 9999,
|
|
36
|
+
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
37
|
+
fontSize: '14px',
|
|
38
|
+
width: isCollapsed ? '120px' : '280px',
|
|
39
|
+
transition: 'width 0.2s ease-in-out, border-radius 0.2s ease-in-out, box-shadow 0.2s ease-in-out',
|
|
40
|
+
};
|
|
41
|
+
const headerStyle = {
|
|
42
|
+
padding: '2px 6px',
|
|
43
|
+
borderBottom: isCollapsed ? '1px solid transparent' : '1px solid #333',
|
|
44
|
+
display: 'flex',
|
|
45
|
+
justifyContent: 'space-between',
|
|
46
|
+
alignItems: 'center',
|
|
47
|
+
cursor: 'pointer',
|
|
48
|
+
userSelect: 'none',
|
|
49
|
+
overflow: 'hidden',
|
|
50
|
+
whiteSpace: 'nowrap',
|
|
51
|
+
transition: 'border-bottom 0.2s ease-in-out',
|
|
52
|
+
};
|
|
53
|
+
const bodyStyle = {
|
|
54
|
+
padding: isCollapsed ? '0 16px' : '16px',
|
|
55
|
+
maxHeight: isCollapsed ? '0' : '400px',
|
|
56
|
+
overflow: 'hidden',
|
|
57
|
+
opacity: isCollapsed ? 0 : 1,
|
|
58
|
+
transition: 'max-height 0.2s ease-in-out, opacity 0.2s ease-in-out, padding 0.2s ease-in-out',
|
|
59
|
+
};
|
|
60
|
+
const inputStyle = {
|
|
61
|
+
width: '100%',
|
|
62
|
+
padding: '8px 12px',
|
|
63
|
+
marginTop: '4px',
|
|
64
|
+
backgroundColor: '#2a2a2a',
|
|
65
|
+
border: '1px solid #444',
|
|
66
|
+
borderRadius: '4px',
|
|
67
|
+
color: '#ffffff',
|
|
68
|
+
fontSize: '14px',
|
|
69
|
+
outline: 'none',
|
|
70
|
+
};
|
|
71
|
+
const buttonStyle = {
|
|
72
|
+
width: '100%',
|
|
73
|
+
padding: '10px',
|
|
74
|
+
marginTop: '12px',
|
|
75
|
+
backgroundColor: isConnected ? '#dc2626' : '#10b981',
|
|
76
|
+
border: 'none',
|
|
77
|
+
borderRadius: '4px',
|
|
78
|
+
color: '#ffffff',
|
|
79
|
+
fontSize: '14px',
|
|
80
|
+
fontWeight: '600',
|
|
81
|
+
cursor: 'pointer',
|
|
82
|
+
transition: 'background-color 0.2s',
|
|
83
|
+
};
|
|
84
|
+
const statusStyle = {
|
|
85
|
+
display: 'flex',
|
|
86
|
+
alignItems: 'center',
|
|
87
|
+
padding: '8px 12px',
|
|
88
|
+
marginTop: '8px',
|
|
89
|
+
backgroundColor: isConnected ? '#065f46' : '#7c2d12',
|
|
90
|
+
borderRadius: '4px',
|
|
91
|
+
fontSize: '13px',
|
|
92
|
+
};
|
|
93
|
+
const dotStyle = {
|
|
94
|
+
width: '8px',
|
|
95
|
+
height: '8px',
|
|
96
|
+
borderRadius: '50%',
|
|
97
|
+
backgroundColor: isConnected ? '#10b981' : '#ef4444',
|
|
98
|
+
marginRight: '8px',
|
|
99
|
+
};
|
|
100
|
+
const labelStyle = {
|
|
101
|
+
marginTop: '12px',
|
|
102
|
+
marginBottom: '4px',
|
|
103
|
+
fontSize: '13px',
|
|
104
|
+
fontWeight: '500',
|
|
105
|
+
color: '#a0a0a0',
|
|
106
|
+
};
|
|
107
|
+
const statusIndicatorStyle = {
|
|
108
|
+
width: '6px',
|
|
109
|
+
height: '6px',
|
|
110
|
+
borderRadius: '50%',
|
|
111
|
+
backgroundColor: isConnected ? '#10b981' : '#ef4444',
|
|
112
|
+
marginLeft: '8px',
|
|
113
|
+
flexShrink: 0,
|
|
114
|
+
transition: 'background-color 0.2s ease-in-out',
|
|
115
|
+
boxShadow: isConnected
|
|
116
|
+
? '0 0 8px rgba(16, 185, 129, 0.6)'
|
|
117
|
+
: '0 0 12px rgba(239, 68, 68, 0.8)',
|
|
118
|
+
animation: isConnected
|
|
119
|
+
? 'none'
|
|
120
|
+
: 'pulse 1.5s cubic-bezier(0.4, 0, 0.6, 1) infinite',
|
|
121
|
+
};
|
|
122
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { children: `
|
|
123
|
+
@keyframes pulse {
|
|
124
|
+
0%, 100% {
|
|
125
|
+
opacity: 1;
|
|
126
|
+
transform: scale(1);
|
|
127
|
+
}
|
|
128
|
+
50% {
|
|
129
|
+
opacity: 0.3;
|
|
130
|
+
transform: scale(1.3);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
` }), (0, jsx_runtime_1.jsxs)("div", { style: panelStyle, children: [(0, jsx_runtime_1.jsxs)("div", { style: headerStyle, onClick: () => setIsCollapsed(!isCollapsed), children: [(0, jsx_runtime_1.jsxs)("div", { style: {
|
|
134
|
+
display: 'flex',
|
|
135
|
+
alignItems: 'center',
|
|
136
|
+
flex: 1,
|
|
137
|
+
overflow: 'hidden',
|
|
138
|
+
}, children: [(0, jsx_runtime_1.jsx)("div", { style: {
|
|
139
|
+
fontWeight: '600',
|
|
140
|
+
fontSize: '10px',
|
|
141
|
+
}, children: "\u269B\uFE0F\u00A0DReaction" }), isCollapsed && (0, jsx_runtime_1.jsx)("div", { style: statusIndicatorStyle })] }), (0, jsx_runtime_1.jsx)("div", { style: {
|
|
142
|
+
fontSize: '14px',
|
|
143
|
+
marginLeft: '4px',
|
|
144
|
+
transform: isCollapsed ? 'rotate(0deg)' : 'rotate(180deg)',
|
|
145
|
+
transition: 'transform 0.2s ease-in-out',
|
|
146
|
+
}, children: "\u25BC" })] }), (0, jsx_runtime_1.jsxs)("div", { style: bodyStyle, children: [(0, jsx_runtime_1.jsxs)("div", { style: statusStyle, children: [(0, jsx_runtime_1.jsx)("div", { style: dotStyle }), (0, jsx_runtime_1.jsx)("span", { children: isConnected ? 'Connected' : 'Disconnected' })] }), (0, jsx_runtime_1.jsx)("div", { style: labelStyle, children: "Host" }), (0, jsx_runtime_1.jsx)("input", { type: "text", value: localHost, onChange: (e) => setLocalHost(e.target.value), placeholder: "localhost", style: inputStyle, disabled: isConnected }), (0, jsx_runtime_1.jsx)("div", { style: labelStyle, children: "Port" }), (0, jsx_runtime_1.jsx)("input", { type: "number", value: localPort, onChange: (e) => setLocalPort(e.target.value), placeholder: "9600", style: inputStyle, disabled: isConnected }), (0, jsx_runtime_1.jsx)("button", { style: buttonStyle, onClick: isConnected ? handleDisconnect : handleConnect, onMouseOver: (e) => {
|
|
147
|
+
e.currentTarget.style.opacity = '0.9';
|
|
148
|
+
}, onMouseOut: (e) => {
|
|
149
|
+
e.currentTarget.style.opacity = '1';
|
|
150
|
+
}, children: isConnected ? 'Disconnect' : 'Connect' })] })] })] }));
|
|
151
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ClientOptions, DReaction, DReactionCore, InferFeaturesFromPlugins } from 'dreaction-client-core';
|
|
2
|
+
import type { DataWatchPayload } from 'dreaction-protocol';
|
|
3
|
+
import { NetworkingOptions } from './plugins/networking';
|
|
4
|
+
import { LocalStorageOptions } from './plugins/localStorage';
|
|
5
|
+
import { TrackGlobalErrorsOptions } from './plugins/trackGlobalErrors';
|
|
6
|
+
export type { ClientOptions };
|
|
7
|
+
export declare const reactCorePlugins: ((dreaction: DReactionCore) => {
|
|
8
|
+
onConnect: () => void;
|
|
9
|
+
onDisconnect: () => void;
|
|
10
|
+
})[];
|
|
11
|
+
export interface UseReactOptions {
|
|
12
|
+
errors?: TrackGlobalErrorsOptions | boolean;
|
|
13
|
+
log?: boolean;
|
|
14
|
+
localStorage?: LocalStorageOptions | boolean;
|
|
15
|
+
networking?: NetworkingOptions | boolean;
|
|
16
|
+
}
|
|
17
|
+
type ReactPluginFeatures = InferFeaturesFromPlugins<DReactionCore, typeof reactCorePlugins>;
|
|
18
|
+
export interface DReactionReact extends DReaction, ReactPluginFeatures {
|
|
19
|
+
useReact: (options?: UseReactOptions) => this;
|
|
20
|
+
registerDataWatcher: <T = unknown>(name: string, type: DataWatchPayload['type'], options?: {
|
|
21
|
+
/**
|
|
22
|
+
* Is data watcher enabled?
|
|
23
|
+
*/
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
}) => {
|
|
26
|
+
currentDebugValue: T | undefined;
|
|
27
|
+
updateDebugValue: (data: unknown) => void;
|
|
28
|
+
useDebugDataWatch: (target: unknown) => void;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export declare const dreaction: DReactionReact;
|
|
32
|
+
//# sourceMappingURL=dreaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dreaction.d.ts","sourceRoot":"","sources":["../src/dreaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,SAAS,EACT,aAAa,EACb,wBAAwB,EAEzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,OAAmB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAqB,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAE3E,OAA0B,EACxB,wBAAwB,EACzB,MAAM,6BAA6B,CAAC;AAErC,YAAY,EAAE,aAAa,EAAE,CAAC;AAM9B,eAAO,MAAM,gBAAgB;;;IAKa,CAAC;AAE3C,MAAM,WAAW,eAAe;IAC9B,MAAM,CAAC,EAAE,wBAAwB,GAAG,OAAO,CAAC;IAC5C,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,YAAY,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC;IAC7C,UAAU,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC;CAC1C;AAED,KAAK,mBAAmB,GAAG,wBAAwB,CACjD,aAAa,EACb,OAAO,gBAAgB,CACxB,CAAC;AAEF,MAAM,WAAW,cACf,SAAQ,SAAS,EAEf,mBAAmB;IACrB,QAAQ,EAAE,CAAC,OAAO,CAAC,EAAE,eAAe,KAAK,IAAI,CAAC;IAC9C,mBAAmB,EAAE,CAAC,CAAC,GAAG,OAAO,EAC/B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAC9B,OAAO,CAAC,EAAE;QACR;;WAEG;QACH,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,KACE;QACH,iBAAiB,EAAE,CAAC,GAAG,SAAS,CAAC;QACjC,gBAAgB,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;QAC1C,iBAAiB,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;KAC9C,CAAC;CACH;AA8CD,eAAO,MAAM,SAAS,gBAAyC,CAAC"}
|
package/lib/dreaction.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.dreaction = exports.reactCorePlugins = void 0;
|
|
7
|
+
const dreaction_client_core_1 = require("dreaction-client-core");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const networking_1 = __importDefault(require("./plugins/networking"));
|
|
10
|
+
const localStorage_1 = __importDefault(require("./plugins/localStorage"));
|
|
11
|
+
const trackGlobalLogs_1 = __importDefault(require("./plugins/trackGlobalLogs"));
|
|
12
|
+
const trackGlobalErrors_1 = __importDefault(require("./plugins/trackGlobalErrors"));
|
|
13
|
+
const DREACTION_LOCAL_STORAGE_CLIENT_ID = 'DREACTION_clientId';
|
|
14
|
+
let tempClientId = null;
|
|
15
|
+
exports.reactCorePlugins = [
|
|
16
|
+
(0, trackGlobalErrors_1.default)(),
|
|
17
|
+
(0, trackGlobalLogs_1.default)(),
|
|
18
|
+
(0, localStorage_1.default)(),
|
|
19
|
+
(0, networking_1.default)(),
|
|
20
|
+
];
|
|
21
|
+
const DEFAULTS = {
|
|
22
|
+
createSocket: (path) => new WebSocket(path),
|
|
23
|
+
host: 'localhost',
|
|
24
|
+
port: 9600,
|
|
25
|
+
name: 'React Web App',
|
|
26
|
+
environment: process.env.NODE_ENV || 'development',
|
|
27
|
+
client: {
|
|
28
|
+
dreactionLibraryName: 'dreaction-react',
|
|
29
|
+
dreactionLibraryVersion: '1.0.0',
|
|
30
|
+
platform: 'web',
|
|
31
|
+
userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '',
|
|
32
|
+
},
|
|
33
|
+
getClientId: async (name = '') => {
|
|
34
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
35
|
+
const stored = window.localStorage.getItem(DREACTION_LOCAL_STORAGE_CLIENT_ID);
|
|
36
|
+
if (stored) {
|
|
37
|
+
return stored;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Generate clientId based on browser info
|
|
41
|
+
tempClientId = [
|
|
42
|
+
name,
|
|
43
|
+
'web',
|
|
44
|
+
typeof navigator !== 'undefined' ? navigator.userAgent : '',
|
|
45
|
+
Date.now(),
|
|
46
|
+
]
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
.join('-');
|
|
49
|
+
return tempClientId;
|
|
50
|
+
},
|
|
51
|
+
setClientId: async (clientId) => {
|
|
52
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
53
|
+
window.localStorage.setItem(DREACTION_LOCAL_STORAGE_CLIENT_ID, clientId);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
tempClientId = clientId;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
proxyHack: true,
|
|
60
|
+
};
|
|
61
|
+
exports.dreaction = (0, dreaction_client_core_1.createClient)(DEFAULTS);
|
|
62
|
+
function getPluginOptions(options) {
|
|
63
|
+
return typeof options === 'object' ? options : null;
|
|
64
|
+
}
|
|
65
|
+
exports.dreaction.useReact = (options = {}) => {
|
|
66
|
+
if (options.errors !== false) {
|
|
67
|
+
exports.dreaction.use((0, trackGlobalErrors_1.default)(getPluginOptions(options.errors)));
|
|
68
|
+
}
|
|
69
|
+
if (options.log !== false) {
|
|
70
|
+
exports.dreaction.use((0, trackGlobalLogs_1.default)());
|
|
71
|
+
}
|
|
72
|
+
if (options.localStorage !== false) {
|
|
73
|
+
exports.dreaction.use((0, localStorage_1.default)(getPluginOptions(options.localStorage)));
|
|
74
|
+
}
|
|
75
|
+
if (options.networking !== false) {
|
|
76
|
+
exports.dreaction.use((0, networking_1.default)(getPluginOptions(options.networking)));
|
|
77
|
+
}
|
|
78
|
+
return exports.dreaction;
|
|
79
|
+
};
|
|
80
|
+
exports.dreaction.registerDataWatcher = (name, type, options) => {
|
|
81
|
+
const { enabled = process.env.NODE_ENV === 'development' } = options ?? {};
|
|
82
|
+
if (!enabled) {
|
|
83
|
+
return {
|
|
84
|
+
currentDebugValue: undefined,
|
|
85
|
+
updateDebugValue: () => { },
|
|
86
|
+
useDebugDataWatch: () => { },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
let prev = undefined;
|
|
90
|
+
const updateDebugValue = (data) => {
|
|
91
|
+
let newData = prev;
|
|
92
|
+
if (typeof data === 'function') {
|
|
93
|
+
newData = data(prev);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
newData = data;
|
|
97
|
+
}
|
|
98
|
+
prev = newData;
|
|
99
|
+
exports.dreaction.send('dataWatch', { name, type, data: newData });
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
currentDebugValue: prev,
|
|
103
|
+
updateDebugValue,
|
|
104
|
+
useDebugDataWatch: (target) => {
|
|
105
|
+
(0, react_1.useEffect)(() => {
|
|
106
|
+
updateDebugValue(target);
|
|
107
|
+
}, [target]);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
};
|
package/lib/hooks.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to manage DReaction connection status
|
|
3
|
+
*/
|
|
4
|
+
export declare function useConnectionStatus(): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Hook to manage DReaction configuration
|
|
7
|
+
*/
|
|
8
|
+
export declare function useDReactionConfig(): {
|
|
9
|
+
host: string;
|
|
10
|
+
port: number;
|
|
11
|
+
isConnected: boolean;
|
|
12
|
+
updateHost: (newHost: string) => void;
|
|
13
|
+
updatePort: (newPort: number) => void;
|
|
14
|
+
connect: () => void;
|
|
15
|
+
disconnect: () => void;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,wBAAgB,mBAAmB,YAelC;AAED;;GAEG;AACH,wBAAgB,kBAAkB;;;;0BAmDH,MAAM;0BAON,MAAM;;;EAwCpC"}
|