@carbonorm/carbonreact 6.0.2 → 6.0.4

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.
@@ -125,21 +125,11 @@ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbon
125
125
 
126
126
  const REQUEST_PRIMARY_KEY: {
127
127
  [key: string]: string
128
- } = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
129
-
130
- if (null === REQUEST_PRIMARY_KEY) {
131
-
132
- if (verbose) {
133
- console.log('WebSocket updates without a primary key are not yet supported.');
134
- }
135
-
136
- return;
137
-
138
- }
139
-
140
- if (verbose) {
141
- console.log('todo - going to impl REST', TABLE_NAME, METHOD, REQUEST_PRIMARY_KEY, parsedData?.REST);
142
- }
128
+ } | null = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
129
+ const RESPONSE_PRIMARY_KEY: {
130
+ [key: string]: string
131
+ } | null = parsedData?.REST?.RESPONSE_PRIMARY_KEY ?? null;
132
+ const RESPONSE = parsedData?.REST?.RESPONSE ?? null;
143
133
 
144
134
  const TABLE_NAME_SHORT = TABLE_NAME.startsWith(TABLE_PREFIX)
145
135
  ? TABLE_NAME.substring(TABLE_PREFIX.length)
@@ -175,19 +165,28 @@ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbon
175
165
  return normalized;
176
166
  };
177
167
 
178
- const normalizedPrimaryKey = normalizeRecord(REQUEST_PRIMARY_KEY);
179
- const primaryKeyKeys = Object.keys(normalizedPrimaryKey);
168
+ const normalizedPrimaryKey = normalizeRecord(REQUEST_PRIMARY_KEY ?? {});
169
+ const normalizedResponsePrimaryKey = normalizeRecord(RESPONSE_PRIMARY_KEY ?? {});
170
+ const primaryKeyKeys = Object.keys(
171
+ Object.keys(normalizedPrimaryKey).length
172
+ ? normalizedPrimaryKey
173
+ : normalizedResponsePrimaryKey
174
+ );
180
175
 
181
176
  if (primaryKeyKeys.length === 0) {
182
177
  if (verbose) {
183
- console.error('WebSocket update could not map primary keys for', TABLE_NAME_SHORT, REQUEST_PRIMARY_KEY);
178
+ console.error('WebSocket update could not map primary keys for', TABLE_NAME_SHORT, REQUEST_PRIMARY_KEY, RESPONSE_PRIMARY_KEY);
184
179
  }
185
180
  return;
186
181
  }
187
182
 
188
183
  // todo - which direction should we filter
189
184
  const elementsToUpdate = currentCache?.filter((row: any) =>
190
- primaryKeyKeys.every((key) => normalizedPrimaryKey[key] === row[key])
185
+ primaryKeyKeys.every((key) => {
186
+ const expected =
187
+ normalizedPrimaryKey[key] ?? normalizedResponsePrimaryKey[key];
188
+ return expected === row[key];
189
+ })
191
190
  ) ?? []
192
191
 
193
192
  if (verbose) {
@@ -195,6 +194,17 @@ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbon
195
194
  }
196
195
 
197
196
  if (elementsToUpdate.length === 0) {
197
+ if (RESPONSE) {
198
+ const responseRows = Array.isArray(RESPONSE) ? RESPONSE : [RESPONSE];
199
+ const normalizedResponseRows = responseRows.map((row) => normalizeRecord(row as any));
200
+ instance.updateRestfulObjectArrays({
201
+ dataOrCallback: normalizedResponseRows,
202
+ stateKey: TABLE_NAME_SHORT as any,
203
+ uniqueObjectId: c6Table.PRIMARY_SHORT as any,
204
+ });
205
+ return;
206
+ }
207
+
198
208
  if (verbose) {
199
209
  console.error('Could not find any elements to update in the cache.', elementsToUpdate, primaryKeyKeys, REQUEST_PRIMARY_KEY, currentCache);
200
210
  }
@@ -258,61 +268,34 @@ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbon
258
268
 
259
269
  }
260
270
 
261
- // See https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1
271
+ if (event.code === 1000) {
272
+ console.log("WebSocket: closed cleanly");
273
+ return;
274
+ }
275
+
262
276
  switch (event.code) {
263
- case 1000:
264
- reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.";
265
- break;
266
- case 1001:
267
- retry(); //call check function after timeout
268
- reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.";
269
- break;
270
- case 1002:
271
- reason = "An endpoint is terminating the connection due to a protocol error";
272
- break;
273
- case 1003:
274
- reason = "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).";
275
- break;
276
- case 1004:
277
- reason = "Reserved. The specific meaning might be defined in the future.";
278
- break;
279
- case 1005:
280
- reason = "No status code was actually present.";
281
- break;
282
277
  case 1006:
283
- retry();
284
- reason = "The connection was closed abnormally, e.g., without sending or receiving a close control frame";
278
+ reason = "Abnormal closure";
285
279
  break;
286
- case 1007:
287
- reason = "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [https://www.rfc-editor.org/rfc/rfc3629] data within a text message).";
288
- break;
289
- case 1008:
290
- reason = "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other suitable reason, or if there is a need to hide specific details about the policy.";
291
- break;
292
- case 1009:
293
- reason = "An endpoint is terminating the connection because it has received a message that is too big for it to process.";
294
- break;
295
- case 1010:
296
- reason = "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason;
280
+ case 1001:
281
+ reason = "Going away";
297
282
  break;
298
283
  case 1011:
299
- reason = "A server is terminating the connection because it encountered an un expected condition that prevented it from fulfilling the request.";
300
- break;
301
- case 1015:
302
- reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).";
284
+ reason = "Internal server error";
303
285
  break;
304
286
  default:
305
- reason = "Unknown reason";
287
+ reason = "Unknown";
306
288
  }
307
289
 
308
- console.log("The connection was closed for reason: " + reason);
290
+ console.log(`WebSocket: closed with code ${event.code} (${reason})`)
291
+
292
+ retry();
309
293
 
310
294
  });
311
295
 
312
296
  // websocket onerror event listener
313
297
  connection.addEventListener('websocket error', (e: Event) => {
314
- console.error("Socket encountered error: ", e, JSON.stringify(e));
315
- connection.close();
298
+ console.error("WebSocket error observed:", e);
316
299
  });
317
300
 
318
301
  });
@@ -322,11 +305,8 @@ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbon
322
305
  export default function <P, S extends iCarbonReactState>(props: iCarbonWebSocketProps<P, S>) {
323
306
 
324
307
  useEffectOnce(() => {
325
-
326
- initiateWebsocket(props)
327
-
308
+ initiateWebsocket(props);
328
309
  })
329
310
 
330
- return null
331
-
311
+ return null;
332
312
  }
@@ -132,7 +132,6 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
132
132
 
133
133
  this.target = new.target;
134
134
  this.stateAdapter = props.stateAdapter;
135
- console.log('CarbonORM TSX CONSTRUCTOR');
136
135
 
137
136
  Object.assign(this.target, {
138
137
  _instance: this