@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.
package/dist/index.esm.js CHANGED
@@ -4359,10 +4359,9 @@ function isJsonString(str) {
4359
4359
  }
4360
4360
  class CarbonReact extends Component {
4361
4361
  static persistentStateMap = new Map();
4362
- // Context is for functional components to access the state of this class efficiently
4362
+ static activeInstances = new Map();
4363
4363
  context = createContext(this.state);
4364
4364
  target;
4365
- // @link https://stackoverflow.com/questions/55029032/what-is-typescripts-thistype-used-for
4366
4365
  static _instance;
4367
4366
  static getInstance() {
4368
4367
  return this._instance;
@@ -4373,7 +4372,6 @@ class CarbonReact extends Component {
4373
4372
  static set instance(instance) {
4374
4373
  this._instance = instance;
4375
4374
  }
4376
- // these are public but the class is abstract
4377
4375
  updateRestfulObjectArrays = (rest) => updateRestfulObjectArrays({
4378
4376
  instance: this,
4379
4377
  ...rest
@@ -4383,40 +4381,35 @@ class CarbonReact extends Component {
4383
4381
  ...rest
4384
4382
  });
4385
4383
  static lastLocation = window.location.pathname;
4386
- // @link https://github.com/welldone-software/why-did-you-render
4387
- // noinspection JSUnusedGlobalSymbols
4388
4384
  static whyDidYouRender = true;
4389
4385
  constructor(props) {
4390
4386
  super(props);
4391
- this.target = new.target;
4387
+ const target = new.target;
4388
+ const identifier = props.instanceId || target.name;
4389
+ 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.`);
4391
+ }
4392
+ CarbonReact.activeInstances.set(identifier, this);
4393
+ this.target = target;
4392
4394
  console.log('CarbonORM TSX CONSTRUCTOR');
4393
- // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
4394
- // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
4395
- Object.assign(new.target, {
4395
+ Object.assign(target, {
4396
4396
  _instance: this
4397
4397
  });
4398
- if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
4399
- this.state = CarbonReact.persistentStateMap.get(this.props.instanceId);
4398
+ if (CarbonReact.persistentStateMap.has(identifier)) {
4399
+ this.state = CarbonReact.persistentStateMap.get(identifier);
4400
4400
  }
4401
4401
  else {
4402
- // This should only ever be done here, when the full state is being trashed.
4403
- // todo - does this suck in context of multiple instances?
4404
4402
  clearCache({
4405
4403
  ignoreWarning: true
4406
4404
  });
4407
4405
  this.state = initialCarbonReactState;
4408
4406
  }
4409
- /** We can think of our app as having one state; this state.
4410
- * Long-term, I'd like us to store this state to local storage and only load updates on reload...
4411
- * Class based components are far easier to manage state in local storage and pass state down to children.
4412
- * Children, if not faced with a local storage or other complexity should be a functional component. Functional
4413
- * components' tend to be shorter syntactically and bonus points if it's stateless.
4414
- **/
4407
+ // Save the initial state to the persistent state map with the identifier
4408
+ CarbonReact.persistentStateMap.set(identifier, this.state);
4415
4409
  }
4416
4410
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4417
- if (this.props.instanceId) {
4418
- CarbonReact.persistentStateMap.set(this.props.instanceId, nextState);
4419
- }
4411
+ const identifier = this.props.instanceId || this.constructor.name;
4412
+ CarbonReact.persistentStateMap.set(identifier, nextState);
4420
4413
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4421
4414
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
4422
4415
  return true;
@@ -4443,6 +4436,10 @@ class CarbonReact extends Component {
4443
4436
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4444
4437
  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, {})] });
4445
4438
  }
4439
+ componentWillUnmount() {
4440
+ const identifier = this.props.instanceId || this.constructor.name;
4441
+ CarbonReact.activeInstances.delete(identifier);
4442
+ }
4446
4443
  }
4447
4444
 
4448
4445
  var getStatefulObjectWithWhere = ({ request }) => {