@carbonorm/carbonreact 4.0.14 → 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.
@@ -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 persistentStateMap;
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;
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');
@@ -4372,12 +4372,22 @@ function isJsonString(str) {
4372
4372
  return true;
4373
4373
  }
4374
4374
  class CarbonReact extends react.Component {
4375
- static persistentStateMap = new Map();
4375
+ static allInstances = new Map();
4376
4376
  static activeInstances = new Map();
4377
4377
  context = react.createContext(this.state);
4378
4378
  target;
4379
4379
  static _instance;
4380
- 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
+ }
4381
4391
  return this._instance;
4382
4392
  }
4383
4393
  static get instance() {
@@ -4398,32 +4408,39 @@ class CarbonReact extends react.Component {
4398
4408
  static whyDidYouRender = true;
4399
4409
  constructor(props) {
4400
4410
  super(props);
4401
- const target = new.target;
4402
- const identifier = props.instanceId || target.name;
4411
+ const identifier = this.generateIdentifier();
4403
4412
  if (CarbonReact.activeInstances.has(identifier)) {
4404
- throw new Error(`Instance with ID ${identifier} already exists! CarbonReact extended classes can only be referenced once in DOM with the same identifier.`);
4413
+ throw new Error(`${identifier} instance already exists in the DOM! Each instance should have a unique instanceId.`);
4405
4414
  }
4415
+ // Register the new instance
4406
4416
  CarbonReact.activeInstances.set(identifier, this);
4407
- this.target = target;
4408
- console.log('CarbonORM TSX CONSTRUCTOR');
4409
- Object.assign(target, {
4410
- _instance: this
4411
- });
4412
- if (CarbonReact.persistentStateMap.has(identifier)) {
4413
- 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;
4414
4420
  }
4415
4421
  else {
4416
- carbonnode.clearCache({
4417
- ignoreWarning: true
4418
- });
4419
4422
  this.state = initialCarbonReactState;
4423
+ CarbonReact.allInstances.set(identifier, this);
4420
4424
  }
4421
- // Save the initial state to the persistent state map with the identifier
4422
- CarbonReact.persistentStateMap.set(identifier, this.state);
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);
4423
4442
  }
4424
4443
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4425
- const identifier = this.props.instanceId || this.constructor.name;
4426
- CarbonReact.persistentStateMap.set(identifier, nextState);
4427
4444
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4428
4445
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
4429
4446
  return true;
@@ -4450,10 +4467,6 @@ class CarbonReact extends react.Component {
4450
4467
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4451
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, {})] });
4452
4469
  }
4453
- componentWillUnmount() {
4454
- const identifier = this.props.instanceId || this.constructor.name;
4455
- CarbonReact.activeInstances.delete(identifier);
4456
- }
4457
4470
  }
4458
4471
 
4459
4472
  var getStatefulObjectWithWhere = ({ request }) => {