@carbonorm/carbonreact 4.0.13 → 4.0.15

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
@@ -1,8 +1,8 @@
1
1
  import react, { useRef, useState, useEffect, Component, createContext, lazy } from 'react';
2
- import { toastOptions, clearCache, timeout, axiosInstance, isVerbose, checkAllRequestsComplete } from '@carbonorm/carbonnode';
3
2
  import { useNavigate } from 'react-router-dom';
4
3
  import { toast, ToastContainer } from 'react-toastify';
5
4
  import OutsideClickHandler from 'react-outside-click-handler';
5
+ import { toastOptions, timeout, axiosInstance, isVerbose, checkAllRequestsComplete } from '@carbonorm/carbonnode';
6
6
  import classNames from 'classnames';
7
7
  import { faClose } from '@fortawesome/free-solid-svg-icons';
8
8
  import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
@@ -4064,7 +4064,8 @@ const useEffectOnce = (effect) => {
4064
4064
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
4065
4065
  **/
4066
4066
  function initiateWebsocket(props) {
4067
- let { instance, TABLES = undefined, WsLiveUpdates = undefined, url = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + window.location.host + '/carbonorm/websocket', timeoutSeconds = 250, heartbeatSeconds = 60 } = props;
4067
+ let { instance, url = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + window.location.host + '/carbonorm/websocket', timeoutSeconds = 250, heartbeatSeconds = 60, C6 } = props;
4068
+ const { TABLES = undefined, IMPORT = undefined, } = C6 ?? {};
4068
4069
  const { websocket } = instance.state;
4069
4070
  if (!("WebSocket" in window)) {
4070
4071
  // todo - store that this has been shown in the state
@@ -4111,10 +4112,6 @@ function initiateWebsocket(props) {
4111
4112
  console.log('WebSocket updates without the TABLES property passed will not automatically update the state.');
4112
4113
  return;
4113
4114
  }
4114
- if (undefined === WsLiveUpdates) {
4115
- console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.');
4116
- return;
4117
- }
4118
4115
  if (parsedData?.REST) {
4119
4116
  const TABLE_NAME = parsedData?.REST?.TABLE_NAME;
4120
4117
  const TABLE_PREFIX = parsedData?.REST?.TABLE_PREFIX;
@@ -4136,7 +4133,8 @@ function initiateWebsocket(props) {
4136
4133
  return;
4137
4134
  }
4138
4135
  const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY);
4139
- const elementsToUpdate = currentCache.filter((row) => {
4136
+ // todo - which direction should we filter
4137
+ const elementsToUpdate = currentCache?.filter((row) => {
4140
4138
  for (const element of primaryKeyKeys) {
4141
4139
  // remove the table name from the column name
4142
4140
  const column = element.split('.')[1];
@@ -4146,7 +4144,7 @@ function initiateWebsocket(props) {
4146
4144
  }
4147
4145
  }
4148
4146
  return true;
4149
- });
4147
+ }) ?? [];
4150
4148
  console.log('elementsToUpdate', elementsToUpdate);
4151
4149
  if (elementsToUpdate.length === 0) {
4152
4150
  console.error('Could not find any elements to update in the cache.', elementsToUpdate, primaryKeyKeys, REQUEST_PRIMARY_KEY, currentCache);
@@ -4158,8 +4156,22 @@ function initiateWebsocket(props) {
4158
4156
  ...REQUEST
4159
4157
  };
4160
4158
  });
4161
- updatedElements.forEach((row) => {
4162
- WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, row);
4159
+ updatedElements.forEach(async (row) => {
4160
+ const RestRequests = await IMPORT?.(TABLE_NAME_SHORT);
4161
+ const { postState, deleteState, putState, } = RestRequests;
4162
+ switch (METHOD) {
4163
+ case 'POST':
4164
+ postState({}, row);
4165
+ break;
4166
+ case 'DELETE':
4167
+ deleteState({}, row);
4168
+ break;
4169
+ case 'PUT':
4170
+ putState({}, row);
4171
+ break;
4172
+ default:
4173
+ console.error('Method not supported', METHOD);
4174
+ }
4163
4175
  });
4164
4176
  }
4165
4177
  });
@@ -4358,12 +4370,22 @@ function isJsonString(str) {
4358
4370
  return true;
4359
4371
  }
4360
4372
  class CarbonReact extends Component {
4361
- static persistentStateMap = new Map();
4373
+ static allInstances = new Map();
4362
4374
  static activeInstances = new Map();
4363
4375
  context = createContext(this.state);
4364
4376
  target;
4365
4377
  static _instance;
4366
- static getInstance() {
4378
+ static getInstance(instanceId) {
4379
+ const identifier = this.generateIdentifier(instanceId);
4380
+ if (undefined !== instanceId) {
4381
+ if (CarbonReact.activeInstances.has(identifier)) {
4382
+ return CarbonReact.activeInstances.get(identifier);
4383
+ }
4384
+ throw new Error(`No instance has been instantiated yet for class (${this.name}) with instanceId (${instanceId})`);
4385
+ }
4386
+ if (!this._instance) {
4387
+ throw new Error(`No instance has been instantiated yet for class (${this.name})`);
4388
+ }
4367
4389
  return this._instance;
4368
4390
  }
4369
4391
  static get instance() {
@@ -4384,32 +4406,39 @@ class CarbonReact extends Component {
4384
4406
  static whyDidYouRender = true;
4385
4407
  constructor(props) {
4386
4408
  super(props);
4387
- const target = new.target;
4388
- const identifier = props.instanceId || target.name;
4409
+ const identifier = this.generateIdentifier();
4389
4410
  if (CarbonReact.activeInstances.has(identifier)) {
4390
- throw new Error(`Instance with ID ${identifier} already exists! CarbonReact extended classes can only be referenced once in DOM with the same identifier.`);
4411
+ throw new Error(`${identifier} instance already exists in the DOM! Each instance should have a unique instanceId.`);
4391
4412
  }
4413
+ // Register the new instance
4392
4414
  CarbonReact.activeInstances.set(identifier, this);
4393
- this.target = target;
4394
- console.log('CarbonORM TSX CONSTRUCTOR');
4395
- Object.assign(target, {
4396
- _instance: this
4397
- });
4398
- if (CarbonReact.persistentStateMap.has(identifier)) {
4399
- this.state = CarbonReact.persistentStateMap.get(identifier);
4415
+ if (props.persistentState && CarbonReact.allInstances.has(identifier)) {
4416
+ // Reuse the state from the existing instance
4417
+ this.state = CarbonReact.allInstances.get(identifier).state;
4400
4418
  }
4401
4419
  else {
4402
- clearCache({
4403
- ignoreWarning: true
4404
- });
4405
4420
  this.state = initialCarbonReactState;
4421
+ CarbonReact.allInstances.set(identifier, this);
4406
4422
  }
4407
- // Save the initial state to the persistent state map with the identifier
4408
- CarbonReact.persistentStateMap.set(identifier, this.state);
4423
+ this.target = new.target;
4424
+ console.log('CarbonORM TSX CONSTRUCTOR');
4425
+ Object.assign(this.target, {
4426
+ _instance: this
4427
+ });
4428
+ }
4429
+ static generateIdentifier(instanceId) {
4430
+ const className = this.name;
4431
+ return instanceId ? `${className}-${instanceId}` : className;
4432
+ }
4433
+ generateIdentifier() {
4434
+ const className = this.constructor.name;
4435
+ return this.props.instanceId ? `${className}-${this.props.instanceId}` : className;
4436
+ }
4437
+ componentWillUnmount() {
4438
+ const identifier = this.generateIdentifier();
4439
+ CarbonReact.activeInstances.delete(identifier);
4409
4440
  }
4410
4441
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4411
- const identifier = this.props.instanceId || this.constructor.name;
4412
- CarbonReact.persistentStateMap.set(identifier, nextState);
4413
4442
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4414
4443
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
4415
4444
  return true;
@@ -4436,10 +4465,6 @@ class CarbonReact extends Component {
4436
4465
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4437
4466
  jsxRuntime_1(CarbonWebSocket, { ...(false !== this.props.websocket ? this.props.websocket : {}), instance: this }), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(ToastContainer, {})] });
4438
4467
  }
4439
- componentWillUnmount() {
4440
- const identifier = this.props.instanceId || this.constructor.name;
4441
- CarbonReact.activeInstances.delete(identifier);
4442
- }
4443
4468
  }
4444
4469
 
4445
4470
  var getStatefulObjectWithWhere = ({ request }) => {