@carbonorm/carbonreact 3.4.8 → 3.5.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/dist/index.esm.js CHANGED
@@ -4043,7 +4043,7 @@ const useEffectOnce = (effect) => {
4043
4043
  * @function connect
4044
4044
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
4045
4045
  **/
4046
- function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
4046
+ function initiateWebsocket({ TABLES = undefined, WsLiveUpdates = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
4047
4047
  const { websocket } = CarbonReact.instance.state;
4048
4048
  if (!("WebSocket" in window)) {
4049
4049
  // todo - store that this has been shown in the state
@@ -4078,17 +4078,59 @@ function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws',
4078
4078
  };
4079
4079
  connection.onmessage = (message) => {
4080
4080
  const parsedData = isJsonString(message?.data) ? JSON.parse(message?.data) : message?.data;
4081
+ if (message.data === 'pong') {
4082
+ return;
4083
+ }
4081
4084
  CarbonReact.instance.setState((prevState) => ({
4082
4085
  websocketEvents: prevState.websocketEvents.concat(message),
4083
4086
  websocketData: prevState.websocketData.concat(parsedData), // JSON.parse no good - base64?
4084
- }));
4085
- console.info('todo - going to impl TABLES', TABLES);
4086
- /*if (undefined !== TABLES) {
4087
-
4088
- TABLES.
4089
-
4090
-
4091
- }*/
4087
+ }), () => {
4088
+ if (undefined === TABLES) {
4089
+ console.log('WebSocket updates without the TABLES property passed will not automatically update the state.');
4090
+ return;
4091
+ }
4092
+ if (undefined === WsLiveUpdates) {
4093
+ console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.');
4094
+ return;
4095
+ }
4096
+ if (parsedData?.REST) {
4097
+ const TABLE_NAME = parsedData?.REST?.TABLE_NAME;
4098
+ const TABLE_PREFIX = parsedData?.REST?.TABLE_PREFIX;
4099
+ const METHOD = parsedData?.REST?.METHOD;
4100
+ const REQUEST = parsedData?.REST?.REQUEST;
4101
+ const REQUEST_PRIMARY_KEY = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
4102
+ if (null === REQUEST_PRIMARY_KEY) {
4103
+ console.log('WebSocket updates without a primary key are not yet supported.');
4104
+ return;
4105
+ }
4106
+ console.log('todo - going to impl REST', TABLE_NAME, METHOD, REQUEST_PRIMARY_KEY, parsedData?.REST);
4107
+ const TABLE_NAME_SHORT = TABLE_NAME.substring(TABLE_PREFIX.length);
4108
+ const currentCache = CarbonReact.instance.state[TABLE_NAME_SHORT];
4109
+ // just because we have a websocket update, doesn't mean we need the update
4110
+ // check to see if the primary key is in the current cache
4111
+ const c6Table = TABLES[TABLE_NAME_SHORT] ?? null;
4112
+ if (null === c6Table) {
4113
+ console.error('WebSocket update could not find (' + TABLE_NAME_SHORT + ') in the TABLES property passed.', TABLES);
4114
+ return;
4115
+ }
4116
+ const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY);
4117
+ const elementsToUpdate = currentCache.filter((row) => {
4118
+ for (const element of primaryKeyKeys) {
4119
+ if (REQUEST_PRIMARY_KEY[element] !== row[element]) {
4120
+ return false;
4121
+ }
4122
+ }
4123
+ return true;
4124
+ });
4125
+ const updatedElements = elementsToUpdate.map((row) => {
4126
+ return {
4127
+ ...row,
4128
+ ...REQUEST
4129
+ };
4130
+ });
4131
+ WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, updatedElements);
4132
+ }
4133
+ });
4092
4134
  };
4093
4135
  window.addEventListener("focus", () => initiateWebsocket());
4094
4136
  // websocket onclose event listener