@carbonorm/carbonreact 3.6.0 → 3.6.2

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
@@ -1,4 +1,4 @@
1
- import react, { useRef, useState, useEffect, Component, createContext, lazy } from 'react';
1
+ import react, { useRef, useState, useEffect, Component, createContext, useContext, lazy } from 'react';
2
2
  import { toastOptions, clearCache, timeout, axiosInstance, isVerbose, checkAllRequestsComplete } from '@carbonorm/carbonnode';
3
3
  import { useNavigate } from 'react-router-dom';
4
4
  import { toast, ToastContainer } from 'react-toastify';
@@ -4255,10 +4255,18 @@ function isJsonString(str) {
4255
4255
  return true;
4256
4256
  }
4257
4257
  // Create a context
4258
- const CarbonReact = class extends Component {
4259
- static instance;
4258
+ const persistentStateMap = new Map();
4259
+ class CarbonReact extends Component {
4260
4260
  context = createContext(this.state);
4261
- static persistentState = undefined;
4261
+ // Private static member
4262
+ static instance;
4263
+ static getState() {
4264
+ return CarbonReact.instance.state;
4265
+ }
4266
+ static useContext() {
4267
+ return () => useContext(CarbonReact.instance.context);
4268
+ }
4269
+ protected;
4262
4270
  static lastLocation = window.location.pathname;
4263
4271
  // @link https://github.com/welldone-software/why-did-you-render
4264
4272
  // noinspection JSUnusedGlobalSymbols
@@ -4266,16 +4274,20 @@ const CarbonReact = class extends Component {
4266
4274
  constructor(props) {
4267
4275
  super(props);
4268
4276
  console.log('CarbonORM TSX CONSTRUCTOR');
4269
- if (CarbonReact.persistentState !== undefined && this.props.shouldStatePersist !== false) {
4270
- this.state = CarbonReact.persistentState;
4277
+ Object.assign(this, {
4278
+ instance: this
4279
+ });
4280
+ if (this.props.instanceId && persistentStateMap.has(this.props.instanceId)) {
4281
+ this.state = persistentStateMap.get(this.props.instanceId);
4271
4282
  }
4272
4283
  else {
4284
+ // This should only ever be done here, when the full state is being trashed.
4285
+ // todo - does this suck in context of multiple instances?
4286
+ clearCache({
4287
+ ignoreWarning: true
4288
+ });
4273
4289
  this.state = initialCarbonReactState;
4274
4290
  }
4275
- // This should only ever be done here, when the full state is being trashed.
4276
- clearCache({
4277
- ignoreWarning: true
4278
- });
4279
4291
  /** We can think of our app as having one state; this state.
4280
4292
  * Long-term, I'd like us to store this state to local storage and only load updates on reload...
4281
4293
  * Class based components are far easier to manage state in local storage and pass state down to children.
@@ -4283,15 +4295,9 @@ const CarbonReact = class extends Component {
4283
4295
  * components' tend to be shorter syntactically and bonus points if it's stateless.
4284
4296
  **/
4285
4297
  }
4286
- static getState() {
4287
- return CarbonReact.instance.state;
4288
- }
4289
4298
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4290
- if (this.props.shouldStatePersist === false) {
4291
- CarbonReact.persistentState = undefined;
4292
- }
4293
- else {
4294
- CarbonReact.persistentState = nextState;
4299
+ if (this.props.instanceId) {
4300
+ persistentStateMap.set(this.props.instanceId, nextState);
4295
4301
  }
4296
4302
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4297
4303
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
@@ -4316,9 +4322,10 @@ const CarbonReact = class extends Component {
4316
4322
  return jsxRuntime_2(jsxRuntime_3, { children: [nest, jsxRuntime_1(BackendThrowable, {})] });
4317
4323
  }
4318
4324
  const Context = this.context.Provider;
4319
- return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), jsxRuntime_1(CarbonWebSocket, {}), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(ToastContainer, {})] });
4325
+ return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4326
+ jsxRuntime_1(CarbonWebSocket, { ...(true === this.props.websocket ? {} : this.props.websocket) }), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(ToastContainer, {})] });
4320
4327
  }
4321
- };
4328
+ }
4322
4329
 
4323
4330
  var getStatefulObjectWithWhere = ({ request }) => {
4324
4331
  console.log('request', request);