@carbonorm/carbonreact 4.0.11 → 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,46 +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
- if (!props.instanceId && new.target._instance) {
4392
- // todo - instanceId being unique should solve this, but.... how
4393
- // This is a singleton pattern, we can only have one instance of this class
4394
- throw new Error(`${new.target.name} instance already exists! CarbonReact extended classes can only be referenced once in DOM`);
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.`);
4395
4391
  }
4396
- new.target.instance = (this);
4397
- this.target = new.target;
4392
+ CarbonReact.activeInstances.set(identifier, this);
4393
+ this.target = target;
4398
4394
  console.log('CarbonORM TSX CONSTRUCTOR');
4399
- // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
4400
- // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
4401
- Object.assign(new.target, {
4395
+ Object.assign(target, {
4402
4396
  _instance: this
4403
4397
  });
4404
- if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
4405
- this.state = CarbonReact.persistentStateMap.get(this.props.instanceId);
4398
+ if (CarbonReact.persistentStateMap.has(identifier)) {
4399
+ this.state = CarbonReact.persistentStateMap.get(identifier);
4406
4400
  }
4407
4401
  else {
4408
- // This should only ever be done here, when the full state is being trashed.
4409
- // todo - does this suck in context of multiple instances?
4410
4402
  clearCache({
4411
4403
  ignoreWarning: true
4412
4404
  });
4413
4405
  this.state = initialCarbonReactState;
4414
4406
  }
4415
- /** We can think of our app as having one state; this state.
4416
- * Long-term, I'd like us to store this state to local storage and only load updates on reload...
4417
- * Class based components are far easier to manage state in local storage and pass state down to children.
4418
- * Children, if not faced with a local storage or other complexity should be a functional component. Functional
4419
- * components' tend to be shorter syntactically and bonus points if it's stateless.
4420
- **/
4407
+ // Save the initial state to the persistent state map with the identifier
4408
+ CarbonReact.persistentStateMap.set(identifier, this.state);
4421
4409
  }
4422
4410
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4423
- if (this.props.instanceId) {
4424
- CarbonReact.persistentStateMap.set(this.props.instanceId, nextState);
4425
- }
4411
+ const identifier = this.props.instanceId || this.constructor.name;
4412
+ CarbonReact.persistentStateMap.set(identifier, nextState);
4426
4413
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4427
4414
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
4428
4415
  return true;
@@ -4449,6 +4436,10 @@ class CarbonReact extends Component {
4449
4436
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4450
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, {})] });
4451
4438
  }
4439
+ componentWillUnmount() {
4440
+ const identifier = this.props.instanceId || this.constructor.name;
4441
+ CarbonReact.activeInstances.delete(identifier);
4442
+ }
4452
4443
  }
4453
4444
 
4454
4445
  var getStatefulObjectWithWhere = ({ request }) => {