@carbonorm/carbonreact 4.0.10 → 4.0.12

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.
@@ -23,6 +23,7 @@ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbon
23
23
  websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false;
24
24
  } & P, S> {
25
25
  private static persistentStateMap;
26
+ private static activeInstances;
26
27
  context: Context<S & iCarbonReactState>;
27
28
  protected target: typeof CarbonReact;
28
29
  protected static _instance: ThisType<CarbonReact<any, any>>;
@@ -41,9 +42,11 @@ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbon
41
42
  children?: ReactNode | ReactNode[];
42
43
  shouldStatePersist?: boolean | undefined;
43
44
  websocket?: boolean | iCarbonWebSocketProps<P, S> | undefined;
45
+ instanceId?: string;
44
46
  } & P);
45
47
  shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, _nextContext: any): boolean;
46
48
  componentDidUpdate(_prevProps: Readonly<P>, _prevState: Readonly<S>, _snapshot?: any): void;
47
49
  render(): ReactElement;
50
+ componentWillUnmount(): void;
48
51
  }
49
52
  export default CarbonReact;
package/dist/index.cjs.js CHANGED
@@ -4361,10 +4361,9 @@ function isJsonString(str) {
4361
4361
  }
4362
4362
  class CarbonReact extends react.Component {
4363
4363
  static persistentStateMap = new Map();
4364
- // Context is for functional components to access the state of this class efficiently
4364
+ static activeInstances = new Map();
4365
4365
  context = react.createContext(this.state);
4366
4366
  target;
4367
- // @link https://stackoverflow.com/questions/55029032/what-is-typescripts-thistype-used-for
4368
4367
  static _instance;
4369
4368
  static getInstance() {
4370
4369
  return this._instance;
@@ -4375,7 +4374,6 @@ class CarbonReact extends react.Component {
4375
4374
  static set instance(instance) {
4376
4375
  this._instance = instance;
4377
4376
  }
4378
- // these are public but the class is abstract
4379
4377
  updateRestfulObjectArrays = (rest) => updateRestfulObjectArrays({
4380
4378
  instance: this,
4381
4379
  ...rest
@@ -4385,40 +4383,35 @@ class CarbonReact extends react.Component {
4385
4383
  ...rest
4386
4384
  });
4387
4385
  static lastLocation = window.location.pathname;
4388
- // @link https://github.com/welldone-software/why-did-you-render
4389
- // noinspection JSUnusedGlobalSymbols
4390
4386
  static whyDidYouRender = true;
4391
4387
  constructor(props) {
4392
4388
  super(props);
4393
- this.target = new.target;
4389
+ const target = new.target;
4390
+ const identifier = props.instanceId || target.name;
4391
+ if (CarbonReact.activeInstances.has(identifier)) {
4392
+ throw new Error(`Instance with ID ${identifier} already exists! CarbonReact extended classes can only be referenced once in DOM with the same identifier.`);
4393
+ }
4394
+ CarbonReact.activeInstances.set(identifier, this);
4395
+ this.target = target;
4394
4396
  console.log('CarbonORM TSX CONSTRUCTOR');
4395
- // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
4396
- // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
4397
- Object.assign(new.target, {
4397
+ Object.assign(target, {
4398
4398
  _instance: this
4399
4399
  });
4400
- if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
4401
- this.state = CarbonReact.persistentStateMap.get(this.props.instanceId);
4400
+ if (CarbonReact.persistentStateMap.has(identifier)) {
4401
+ this.state = CarbonReact.persistentStateMap.get(identifier);
4402
4402
  }
4403
4403
  else {
4404
- // This should only ever be done here, when the full state is being trashed.
4405
- // todo - does this suck in context of multiple instances?
4406
4404
  carbonnode.clearCache({
4407
4405
  ignoreWarning: true
4408
4406
  });
4409
4407
  this.state = initialCarbonReactState;
4410
4408
  }
4411
- /** We can think of our app as having one state; this state.
4412
- * Long-term, I'd like us to store this state to local storage and only load updates on reload...
4413
- * Class based components are far easier to manage state in local storage and pass state down to children.
4414
- * Children, if not faced with a local storage or other complexity should be a functional component. Functional
4415
- * components' tend to be shorter syntactically and bonus points if it's stateless.
4416
- **/
4409
+ // Save the initial state to the persistent state map with the identifier
4410
+ CarbonReact.persistentStateMap.set(identifier, this.state);
4417
4411
  }
4418
4412
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4419
- if (this.props.instanceId) {
4420
- CarbonReact.persistentStateMap.set(this.props.instanceId, nextState);
4421
- }
4413
+ const identifier = this.props.instanceId || this.constructor.name;
4414
+ CarbonReact.persistentStateMap.set(identifier, nextState);
4422
4415
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4423
4416
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
4424
4417
  return true;
@@ -4445,6 +4438,10 @@ class CarbonReact extends react.Component {
4445
4438
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4446
4439
  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, {})] });
4447
4440
  }
4441
+ componentWillUnmount() {
4442
+ const identifier = this.props.instanceId || this.constructor.name;
4443
+ CarbonReact.activeInstances.delete(identifier);
4444
+ }
4448
4445
  }
4449
4446
 
4450
4447
  var getStatefulObjectWithWhere = ({ request }) => {