@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/CarbonReact.d.ts +7 -4
- package/dist/components/WebSocket/CarbonWebSocket.d.ts +2 -3
- package/dist/index.cjs.js +58 -33
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +58 -33
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/CarbonReact.tsx +47 -31
- package/src/components/WebSocket/CarbonWebSocket.tsx +44 -25
package/dist/CarbonReact.d.ts
CHANGED
|
@@ -20,14 +20,15 @@ export declare function isJsonString(str: string): boolean;
|
|
|
20
20
|
declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
|
|
21
21
|
children?: ReactNode | ReactNode[];
|
|
22
22
|
instanceId?: string;
|
|
23
|
+
persistentState?: boolean;
|
|
23
24
|
websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false;
|
|
24
25
|
} & P, S> {
|
|
25
|
-
private static
|
|
26
|
+
private static allInstances;
|
|
26
27
|
private static activeInstances;
|
|
27
28
|
context: Context<S & iCarbonReactState>;
|
|
28
29
|
protected target: typeof CarbonReact;
|
|
29
30
|
protected static _instance: ThisType<CarbonReact<any, any>>;
|
|
30
|
-
static getInstance<T extends CarbonReact<any, any>>(): T;
|
|
31
|
+
static getInstance<T extends CarbonReact<any, any>>(instanceId?: string): T;
|
|
31
32
|
static get instance(): CarbonReact<any, any>;
|
|
32
33
|
static set instance(instance: CarbonReact<any, any>);
|
|
33
34
|
updateRestfulObjectArrays: <ObjectType extends {
|
|
@@ -40,13 +41,15 @@ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbon
|
|
|
40
41
|
static whyDidYouRender: boolean;
|
|
41
42
|
protected constructor(props: {
|
|
42
43
|
children?: ReactNode | ReactNode[];
|
|
43
|
-
shouldStatePersist?: boolean | undefined;
|
|
44
44
|
websocket?: boolean | iCarbonWebSocketProps<P, S> | undefined;
|
|
45
45
|
instanceId?: string;
|
|
46
|
+
persistentState?: boolean;
|
|
46
47
|
} & P);
|
|
48
|
+
private static generateIdentifier;
|
|
49
|
+
private generateIdentifier;
|
|
50
|
+
componentWillUnmount(): void;
|
|
47
51
|
shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, _nextContext: any): boolean;
|
|
48
52
|
componentDidUpdate(_prevProps: Readonly<P>, _prevState: Readonly<S>, _snapshot?: any): void;
|
|
49
53
|
render(): ReactElement;
|
|
50
|
-
componentWillUnmount(): void;
|
|
51
54
|
}
|
|
52
55
|
export default CarbonReact;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import CarbonReact, { iCarbonReactState } from "CarbonReact";
|
|
2
|
-
import {
|
|
2
|
+
import { iC6Object } from "@carbonorm/carbonnode";
|
|
3
3
|
export interface iCarbonWebSocketProps<P, S extends iCarbonReactState> {
|
|
4
4
|
url?: string;
|
|
5
5
|
timeoutSeconds?: number;
|
|
6
6
|
heartbeatSeconds?: number;
|
|
7
7
|
instance: CarbonReact<P, S>;
|
|
8
|
-
|
|
9
|
-
WsLiveUpdates?: tC6RestApi;
|
|
8
|
+
C6?: iC6Object;
|
|
10
9
|
}
|
|
11
10
|
/**
|
|
12
11
|
* @function connect
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var react = require('react');
|
|
4
|
-
var carbonnode = require('@carbonorm/carbonnode');
|
|
5
4
|
var reactRouterDom = require('react-router-dom');
|
|
6
5
|
var reactToastify = require('react-toastify');
|
|
7
6
|
var OutsideClickHandler = require('react-outside-click-handler');
|
|
7
|
+
var carbonnode = require('@carbonorm/carbonnode');
|
|
8
8
|
var classNames = require('classnames');
|
|
9
9
|
var freeSolidSvgIcons = require('@fortawesome/free-solid-svg-icons');
|
|
10
10
|
var reactFontawesome = require('@fortawesome/react-fontawesome');
|
|
@@ -4066,7 +4066,8 @@ const useEffectOnce = (effect) => {
|
|
|
4066
4066
|
* This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
|
|
4067
4067
|
**/
|
|
4068
4068
|
function initiateWebsocket(props) {
|
|
4069
|
-
let { instance,
|
|
4069
|
+
let { instance, url = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + window.location.host + '/carbonorm/websocket', timeoutSeconds = 250, heartbeatSeconds = 60, C6 } = props;
|
|
4070
|
+
const { TABLES = undefined, IMPORT = undefined, } = C6 ?? {};
|
|
4070
4071
|
const { websocket } = instance.state;
|
|
4071
4072
|
if (!("WebSocket" in window)) {
|
|
4072
4073
|
// todo - store that this has been shown in the state
|
|
@@ -4113,10 +4114,6 @@ function initiateWebsocket(props) {
|
|
|
4113
4114
|
console.log('WebSocket updates without the TABLES property passed will not automatically update the state.');
|
|
4114
4115
|
return;
|
|
4115
4116
|
}
|
|
4116
|
-
if (undefined === WsLiveUpdates) {
|
|
4117
|
-
console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.');
|
|
4118
|
-
return;
|
|
4119
|
-
}
|
|
4120
4117
|
if (parsedData?.REST) {
|
|
4121
4118
|
const TABLE_NAME = parsedData?.REST?.TABLE_NAME;
|
|
4122
4119
|
const TABLE_PREFIX = parsedData?.REST?.TABLE_PREFIX;
|
|
@@ -4138,7 +4135,8 @@ function initiateWebsocket(props) {
|
|
|
4138
4135
|
return;
|
|
4139
4136
|
}
|
|
4140
4137
|
const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY);
|
|
4141
|
-
|
|
4138
|
+
// todo - which direction should we filter
|
|
4139
|
+
const elementsToUpdate = currentCache?.filter((row) => {
|
|
4142
4140
|
for (const element of primaryKeyKeys) {
|
|
4143
4141
|
// remove the table name from the column name
|
|
4144
4142
|
const column = element.split('.')[1];
|
|
@@ -4148,7 +4146,7 @@ function initiateWebsocket(props) {
|
|
|
4148
4146
|
}
|
|
4149
4147
|
}
|
|
4150
4148
|
return true;
|
|
4151
|
-
});
|
|
4149
|
+
}) ?? [];
|
|
4152
4150
|
console.log('elementsToUpdate', elementsToUpdate);
|
|
4153
4151
|
if (elementsToUpdate.length === 0) {
|
|
4154
4152
|
console.error('Could not find any elements to update in the cache.', elementsToUpdate, primaryKeyKeys, REQUEST_PRIMARY_KEY, currentCache);
|
|
@@ -4160,8 +4158,22 @@ function initiateWebsocket(props) {
|
|
|
4160
4158
|
...REQUEST
|
|
4161
4159
|
};
|
|
4162
4160
|
});
|
|
4163
|
-
updatedElements.forEach((row) => {
|
|
4164
|
-
|
|
4161
|
+
updatedElements.forEach(async (row) => {
|
|
4162
|
+
const RestRequests = await IMPORT?.(TABLE_NAME_SHORT);
|
|
4163
|
+
const { postState, deleteState, putState, } = RestRequests;
|
|
4164
|
+
switch (METHOD) {
|
|
4165
|
+
case 'POST':
|
|
4166
|
+
postState({}, row);
|
|
4167
|
+
break;
|
|
4168
|
+
case 'DELETE':
|
|
4169
|
+
deleteState({}, row);
|
|
4170
|
+
break;
|
|
4171
|
+
case 'PUT':
|
|
4172
|
+
putState({}, row);
|
|
4173
|
+
break;
|
|
4174
|
+
default:
|
|
4175
|
+
console.error('Method not supported', METHOD);
|
|
4176
|
+
}
|
|
4165
4177
|
});
|
|
4166
4178
|
}
|
|
4167
4179
|
});
|
|
@@ -4360,12 +4372,22 @@ function isJsonString(str) {
|
|
|
4360
4372
|
return true;
|
|
4361
4373
|
}
|
|
4362
4374
|
class CarbonReact extends react.Component {
|
|
4363
|
-
static
|
|
4375
|
+
static allInstances = new Map();
|
|
4364
4376
|
static activeInstances = new Map();
|
|
4365
4377
|
context = react.createContext(this.state);
|
|
4366
4378
|
target;
|
|
4367
4379
|
static _instance;
|
|
4368
|
-
static getInstance() {
|
|
4380
|
+
static getInstance(instanceId) {
|
|
4381
|
+
const identifier = this.generateIdentifier(instanceId);
|
|
4382
|
+
if (undefined !== instanceId) {
|
|
4383
|
+
if (CarbonReact.activeInstances.has(identifier)) {
|
|
4384
|
+
return CarbonReact.activeInstances.get(identifier);
|
|
4385
|
+
}
|
|
4386
|
+
throw new Error(`No instance has been instantiated yet for class (${this.name}) with instanceId (${instanceId})`);
|
|
4387
|
+
}
|
|
4388
|
+
if (!this._instance) {
|
|
4389
|
+
throw new Error(`No instance has been instantiated yet for class (${this.name})`);
|
|
4390
|
+
}
|
|
4369
4391
|
return this._instance;
|
|
4370
4392
|
}
|
|
4371
4393
|
static get instance() {
|
|
@@ -4386,32 +4408,39 @@ class CarbonReact extends react.Component {
|
|
|
4386
4408
|
static whyDidYouRender = true;
|
|
4387
4409
|
constructor(props) {
|
|
4388
4410
|
super(props);
|
|
4389
|
-
const
|
|
4390
|
-
const identifier = props.instanceId || target.name;
|
|
4411
|
+
const identifier = this.generateIdentifier();
|
|
4391
4412
|
if (CarbonReact.activeInstances.has(identifier)) {
|
|
4392
|
-
throw new Error(
|
|
4413
|
+
throw new Error(`${identifier} instance already exists in the DOM! Each instance should have a unique instanceId.`);
|
|
4393
4414
|
}
|
|
4415
|
+
// Register the new instance
|
|
4394
4416
|
CarbonReact.activeInstances.set(identifier, this);
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
_instance: this
|
|
4399
|
-
});
|
|
4400
|
-
if (CarbonReact.persistentStateMap.has(identifier)) {
|
|
4401
|
-
this.state = CarbonReact.persistentStateMap.get(identifier);
|
|
4417
|
+
if (props.persistentState && CarbonReact.allInstances.has(identifier)) {
|
|
4418
|
+
// Reuse the state from the existing instance
|
|
4419
|
+
this.state = CarbonReact.allInstances.get(identifier).state;
|
|
4402
4420
|
}
|
|
4403
4421
|
else {
|
|
4404
|
-
carbonnode.clearCache({
|
|
4405
|
-
ignoreWarning: true
|
|
4406
|
-
});
|
|
4407
4422
|
this.state = initialCarbonReactState;
|
|
4423
|
+
CarbonReact.allInstances.set(identifier, this);
|
|
4408
4424
|
}
|
|
4409
|
-
|
|
4410
|
-
|
|
4425
|
+
this.target = new.target;
|
|
4426
|
+
console.log('CarbonORM TSX CONSTRUCTOR');
|
|
4427
|
+
Object.assign(this.target, {
|
|
4428
|
+
_instance: this
|
|
4429
|
+
});
|
|
4430
|
+
}
|
|
4431
|
+
static generateIdentifier(instanceId) {
|
|
4432
|
+
const className = this.name;
|
|
4433
|
+
return instanceId ? `${className}-${instanceId}` : className;
|
|
4434
|
+
}
|
|
4435
|
+
generateIdentifier() {
|
|
4436
|
+
const className = this.constructor.name;
|
|
4437
|
+
return this.props.instanceId ? `${className}-${this.props.instanceId}` : className;
|
|
4438
|
+
}
|
|
4439
|
+
componentWillUnmount() {
|
|
4440
|
+
const identifier = this.generateIdentifier();
|
|
4441
|
+
CarbonReact.activeInstances.delete(identifier);
|
|
4411
4442
|
}
|
|
4412
4443
|
shouldComponentUpdate(nextProps, nextState, _nextContext) {
|
|
4413
|
-
const identifier = this.props.instanceId || this.constructor.name;
|
|
4414
|
-
CarbonReact.persistentStateMap.set(identifier, nextState);
|
|
4415
4444
|
changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
|
|
4416
4445
|
changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
|
|
4417
4446
|
return true;
|
|
@@ -4438,10 +4467,6 @@ class CarbonReact extends react.Component {
|
|
|
4438
4467
|
return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
|
|
4439
4468
|
jsxRuntime_1(CarbonWebSocket, { ...(false !== this.props.websocket ? this.props.websocket : {}), instance: this }), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(reactToastify.ToastContainer, {})] });
|
|
4440
4469
|
}
|
|
4441
|
-
componentWillUnmount() {
|
|
4442
|
-
const identifier = this.props.instanceId || this.constructor.name;
|
|
4443
|
-
CarbonReact.activeInstances.delete(identifier);
|
|
4444
|
-
}
|
|
4445
4470
|
}
|
|
4446
4471
|
|
|
4447
4472
|
var getStatefulObjectWithWhere = ({ request }) => {
|