@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/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "3.4.8",
4
+ "version": "3.5.1",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "type": "module",
10
10
  "dependencies": {
11
- "@carbonorm/carbonnode": "^1.3.0",
11
+ "@carbonorm/carbonnode": "^1.4.0",
12
12
  "@fortawesome/fontawesome-svg-core": "^6.4.0",
13
13
  "@fortawesome/free-solid-svg-icons": "^6.4.0",
14
14
  "@fortawesome/react-fontawesome": "^0.2.0",
@@ -1,25 +1,28 @@
1
1
  import CarbonReact, {isJsonString} from "CarbonReact";
2
2
  import {addAlert} from "../Alert/Alert";
3
3
  import {useEffectOnce} from "../../api/hoc/useEffectOnce";
4
- import {tC6Tables} from "@carbonorm/carbonnode";
4
+ import {tC6Tables, tWsLiveUpdate} from "@carbonorm/carbonnode";
5
5
 
6
6
 
7
7
  export interface iCarbonWebSocketProps {
8
8
  url?: string,
9
9
  timeoutSeconds?: number,
10
10
  heartbeatSeconds?: number,
11
- TABLES?: tC6Tables
11
+ TABLES?: tC6Tables,
12
+ WsLiveUpdates?: tWsLiveUpdate,
12
13
  }
13
14
 
14
15
  /**
15
16
  * @function connect
16
17
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
17
18
  **/
18
- export function initiateWebsocket({TABLES = undefined,
19
+ export function initiateWebsocket({
20
+ TABLES = undefined,
21
+ WsLiveUpdates = undefined,
19
22
  url = 'ws://localhost:8080/ws',
20
23
  timeoutSeconds = 250,
21
24
  heartbeatSeconds = 60
22
- }: iCarbonWebSocketProps = {}) {
25
+ }: iCarbonWebSocketProps = {}) {
23
26
 
24
27
  const {websocket} = CarbonReact.instance.state;
25
28
 
@@ -79,19 +82,104 @@ export function initiateWebsocket({TABLES = undefined,
79
82
 
80
83
  const parsedData = isJsonString(message?.data) ? JSON.parse(message?.data) : message?.data;
81
84
 
85
+ if (message.data === 'pong') {
86
+ return;
87
+ }
88
+
82
89
  CarbonReact.instance.setState((prevState: Readonly<any>) => ({
83
90
  websocketEvents: prevState.websocketEvents.concat(message),
84
91
  websocketData: prevState.websocketData.concat(parsedData), // JSON.parse no good - base64?
85
- }));
92
+ }), () => {
93
+
94
+ if (undefined === TABLES) {
95
+
96
+ console.log('WebSocket updates without the TABLES property passed will not automatically update the state.')
97
+
98
+ return;
99
+
100
+ }
101
+
102
+ if (undefined === WsLiveUpdates) {
103
+
104
+ console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.')
105
+
106
+ return;
107
+
108
+ }
109
+
110
+ if (parsedData?.REST) {
111
+
112
+ const TABLE_NAME: string = parsedData?.REST?.TABLE_NAME;
113
+
114
+ const TABLE_PREFIX: string = parsedData?.REST?.TABLE_PREFIX;
115
+
116
+ const METHOD: string = parsedData?.REST?.METHOD;
117
+
118
+ const REQUEST: { [key:string]: any } = parsedData?.REST?.REQUEST;
119
+
120
+ const REQUEST_PRIMARY_KEY: {
121
+ [key: string]: string
122
+ } = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
123
+
124
+ if (null === REQUEST_PRIMARY_KEY) {
125
+
126
+ console.log('WebSocket updates without a primary key are not yet supported.')
127
+
128
+ return;
129
+
130
+ }
131
+
132
+ console.log('todo - going to impl REST', TABLE_NAME, METHOD, REQUEST_PRIMARY_KEY, parsedData?.REST)
133
+
134
+ const TABLE_NAME_SHORT = TABLE_NAME.substring(TABLE_PREFIX.length);
135
+
136
+ const currentCache: [] = CarbonReact.instance.state[TABLE_NAME_SHORT]
137
+
138
+ // just because we have a websocket update, doesn't mean we need the update
139
+ // check to see if the primary key is in the current cache
140
+ const c6Table = TABLES[TABLE_NAME_SHORT] ?? null;
141
+
142
+ if (null === c6Table) {
143
+
144
+ console.error('WebSocket update could not find (' + TABLE_NAME_SHORT + ') in the TABLES property passed.', TABLES)
145
+
146
+ return;
147
+
148
+ }
149
+
150
+ const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY)
151
+
152
+ const elementsToUpdate = currentCache.filter((row: any) => {
153
+ for (const element of primaryKeyKeys) {
154
+ console.log('element', element, REQUEST_PRIMARY_KEY[element], row[element])
155
+ if (REQUEST_PRIMARY_KEY[element] !== row[element]) {
156
+ return false
157
+ }
158
+ }
159
+ return true
160
+ })
161
+
162
+ console.log('elementsToUpdate', elementsToUpdate)
163
+
164
+ if (elementsToUpdate.length === 0) {
165
+ console.error('Could not find any elements to update in the cache.', elementsToUpdate, primaryKeyKeys, REQUEST_PRIMARY_KEY, currentCache)
166
+ return;
167
+ }
86
168
 
87
- console.info('todo - going to impl TABLES', TABLES)
169
+ const updatedElements = elementsToUpdate.map((row: any) => {
170
+ return {
171
+ ...row,
172
+ ...REQUEST
173
+ }
174
+ })
88
175
 
89
- /*if (undefined !== TABLES) {
176
+ console.log('updatedElements', updatedElements)
90
177
 
91
- TABLES.
178
+ WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, updatedElements)
92
179
 
180
+ }
93
181
 
94
- }*/
182
+ });
95
183
 
96
184
  };
97
185