@carbonorm/carbonreact 3.4.8 → 3.5.1
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 +2 -2
- package/dist/components/WebSocket/CarbonWebSocket.d.ts +3 -2
- package/dist/index.cjs.js +58 -9
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +58 -9
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/components/WebSocket/CarbonWebSocket.tsx +97 -9
package/README.md
CHANGED
|
@@ -204,7 +204,7 @@ stateful operations must be wrapped in a function and thus must not be run durin
|
|
|
204
204
|
Updating state is as simple as calling `CarbonORM.instance.setState({})`. The class name `CarbonORM` can be replaced with
|
|
205
205
|
any name of your liking. Typically, you will want to use the name of your project.
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
[ui.tsx](https://github.com/CarbonORM/CarbonORM.dev/blob/www/src/state/ui.tsx)
|
|
208
208
|
```typescript jsx
|
|
209
209
|
import CarbonORM from "CarbonORM";
|
|
210
210
|
|
|
@@ -229,4 +229,4 @@ export const switchDarkAndLightTheme = () => {
|
|
|
229
229
|
darkMode: !CarbonORM.instance.state.darkMode
|
|
230
230
|
});
|
|
231
231
|
};
|
|
232
|
-
```
|
|
232
|
+
```
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { tC6Tables } from "@carbonorm/carbonnode";
|
|
1
|
+
import { tC6Tables, tWsLiveUpdate } from "@carbonorm/carbonnode";
|
|
2
2
|
export interface iCarbonWebSocketProps {
|
|
3
3
|
url?: string;
|
|
4
4
|
timeoutSeconds?: number;
|
|
5
5
|
heartbeatSeconds?: number;
|
|
6
6
|
TABLES?: tC6Tables;
|
|
7
|
+
WsLiveUpdates?: tWsLiveUpdate;
|
|
7
8
|
}
|
|
8
9
|
/**
|
|
9
10
|
* @function connect
|
|
10
11
|
* This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
|
|
11
12
|
**/
|
|
12
|
-
export declare function initiateWebsocket({ TABLES, url, timeoutSeconds, heartbeatSeconds }?: iCarbonWebSocketProps): void;
|
|
13
|
+
export declare function initiateWebsocket({ TABLES, WsLiveUpdates, url, timeoutSeconds, heartbeatSeconds }?: iCarbonWebSocketProps): void;
|
|
13
14
|
export default function (props: iCarbonWebSocketProps): null;
|
package/dist/index.cjs.js
CHANGED
|
@@ -4045,7 +4045,7 @@ const useEffectOnce = (effect) => {
|
|
|
4045
4045
|
* @function connect
|
|
4046
4046
|
* This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
|
|
4047
4047
|
**/
|
|
4048
|
-
function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
|
|
4048
|
+
function initiateWebsocket({ TABLES = undefined, WsLiveUpdates = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
|
|
4049
4049
|
const { websocket } = CarbonReact.instance.state;
|
|
4050
4050
|
if (!("WebSocket" in window)) {
|
|
4051
4051
|
// todo - store that this has been shown in the state
|
|
@@ -4080,17 +4080,66 @@ function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws',
|
|
|
4080
4080
|
};
|
|
4081
4081
|
connection.onmessage = (message) => {
|
|
4082
4082
|
const parsedData = isJsonString(message?.data) ? JSON.parse(message?.data) : message?.data;
|
|
4083
|
+
if (message.data === 'pong') {
|
|
4084
|
+
return;
|
|
4085
|
+
}
|
|
4083
4086
|
CarbonReact.instance.setState((prevState) => ({
|
|
4084
4087
|
websocketEvents: prevState.websocketEvents.concat(message),
|
|
4085
4088
|
websocketData: prevState.websocketData.concat(parsedData), // JSON.parse no good - base64?
|
|
4086
|
-
}))
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4089
|
+
}), () => {
|
|
4090
|
+
if (undefined === TABLES) {
|
|
4091
|
+
console.log('WebSocket updates without the TABLES property passed will not automatically update the state.');
|
|
4092
|
+
return;
|
|
4093
|
+
}
|
|
4094
|
+
if (undefined === WsLiveUpdates) {
|
|
4095
|
+
console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.');
|
|
4096
|
+
return;
|
|
4097
|
+
}
|
|
4098
|
+
if (parsedData?.REST) {
|
|
4099
|
+
const TABLE_NAME = parsedData?.REST?.TABLE_NAME;
|
|
4100
|
+
const TABLE_PREFIX = parsedData?.REST?.TABLE_PREFIX;
|
|
4101
|
+
const METHOD = parsedData?.REST?.METHOD;
|
|
4102
|
+
const REQUEST = parsedData?.REST?.REQUEST;
|
|
4103
|
+
const REQUEST_PRIMARY_KEY = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
|
|
4104
|
+
if (null === REQUEST_PRIMARY_KEY) {
|
|
4105
|
+
console.log('WebSocket updates without a primary key are not yet supported.');
|
|
4106
|
+
return;
|
|
4107
|
+
}
|
|
4108
|
+
console.log('todo - going to impl REST', TABLE_NAME, METHOD, REQUEST_PRIMARY_KEY, parsedData?.REST);
|
|
4109
|
+
const TABLE_NAME_SHORT = TABLE_NAME.substring(TABLE_PREFIX.length);
|
|
4110
|
+
const currentCache = CarbonReact.instance.state[TABLE_NAME_SHORT];
|
|
4111
|
+
// just because we have a websocket update, doesn't mean we need the update
|
|
4112
|
+
// check to see if the primary key is in the current cache
|
|
4113
|
+
const c6Table = TABLES[TABLE_NAME_SHORT] ?? null;
|
|
4114
|
+
if (null === c6Table) {
|
|
4115
|
+
console.error('WebSocket update could not find (' + TABLE_NAME_SHORT + ') in the TABLES property passed.', TABLES);
|
|
4116
|
+
return;
|
|
4117
|
+
}
|
|
4118
|
+
const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY);
|
|
4119
|
+
const elementsToUpdate = currentCache.filter((row) => {
|
|
4120
|
+
for (const element of primaryKeyKeys) {
|
|
4121
|
+
console.log('element', element, REQUEST_PRIMARY_KEY[element], row[element]);
|
|
4122
|
+
if (REQUEST_PRIMARY_KEY[element] !== row[element]) {
|
|
4123
|
+
return false;
|
|
4124
|
+
}
|
|
4125
|
+
}
|
|
4126
|
+
return true;
|
|
4127
|
+
});
|
|
4128
|
+
console.log('elementsToUpdate', elementsToUpdate);
|
|
4129
|
+
if (elementsToUpdate.length === 0) {
|
|
4130
|
+
console.error('Could not find any elements to update in the cache.', elementsToUpdate, primaryKeyKeys, REQUEST_PRIMARY_KEY, currentCache);
|
|
4131
|
+
return;
|
|
4132
|
+
}
|
|
4133
|
+
const updatedElements = elementsToUpdate.map((row) => {
|
|
4134
|
+
return {
|
|
4135
|
+
...row,
|
|
4136
|
+
...REQUEST
|
|
4137
|
+
};
|
|
4138
|
+
});
|
|
4139
|
+
console.log('updatedElements', updatedElements);
|
|
4140
|
+
WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, updatedElements);
|
|
4141
|
+
}
|
|
4142
|
+
});
|
|
4094
4143
|
};
|
|
4095
4144
|
window.addEventListener("focus", () => initiateWebsocket());
|
|
4096
4145
|
// websocket onclose event listener
|