@carbonorm/carbonreact 3.6.1 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "3.6.1",
4
+ "version": "3.6.2",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
@@ -2,7 +2,7 @@ import {clearCache} from "@carbonorm/carbonnode";
2
2
  import changed from "hoc/changed";
3
3
  import {GlobalHistory} from "hoc/GlobalHistory";
4
4
  import hexToRgb from "hoc/hexToRgb";
5
- import {Component, Context, createContext, ReactElement, ReactNode} from 'react';
5
+ import {Component, Context, createContext, useContext, ReactElement, ReactNode} from 'react';
6
6
  import {ToastContainer} from 'react-toastify';
7
7
  import 'react-toastify/dist/ReactToastify.min.css';
8
8
  import BackendThrowable from 'components/Errors/BackendThrowable';
@@ -45,19 +45,30 @@ export function isJsonString(str: string) {
45
45
 
46
46
  // Create a context
47
47
 
48
- const CarbonReact = class<P = {}, S = {}> extends Component<{
48
+ const persistentStateMap = new Map<string, iCarbonReactState>();
49
+
50
+ abstract class CarbonReact<P = {}, S = {}> extends Component<{
49
51
  children?: ReactNode | ReactNode[],
50
- shouldStatePersist?: boolean,
52
+ instanceId?: string,
51
53
  websocket?: iCarbonWebSocketProps | boolean
52
54
  } & P, S & iCarbonReactState> {
53
55
 
54
- static instance: Component<{
56
+ context: Context<S & iCarbonReactState> = createContext(this.state);
57
+
58
+ // Private static member
59
+ protected static instance: CarbonReact & Component<{
55
60
  children?: ReactNode | ReactNode[],
56
61
  } & any, any & iCarbonReactState>;
57
62
 
58
- context: Context<S & iCarbonReactState> = createContext(this.state);
63
+ protected static getState() {
64
+ return CarbonReact.instance.state;
65
+ }
66
+
67
+ protected static useContext(){
68
+ return () => useContext(CarbonReact.instance.context);
69
+ }
59
70
 
60
- static persistentState?: iCarbonReactState = undefined;
71
+ protected ;
61
72
 
62
73
  static lastLocation = window.location.pathname;
63
74
 
@@ -65,27 +76,32 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
65
76
  // noinspection JSUnusedGlobalSymbols
66
77
  static whyDidYouRender = true;
67
78
 
68
- constructor(props) {
79
+ protected constructor(props: { children?: ReactNode | ReactNode[]; shouldStatePersist?: boolean | undefined; websocket?: boolean | iCarbonWebSocketProps | undefined; } & P) {
69
80
 
70
81
  super(props);
71
82
 
72
83
  console.log('CarbonORM TSX CONSTRUCTOR');
73
84
 
74
- if (CarbonReact.persistentState !== undefined && this.props.shouldStatePersist !== false) {
85
+ Object.assign(this, {
86
+ instance: this
87
+ })
75
88
 
76
- this.state = CarbonReact.persistentState as S & iCarbonReactState;
89
+ if (this.props.instanceId && persistentStateMap.has(this.props.instanceId)) {
90
+
91
+ this.state = persistentStateMap.get(this.props.instanceId) as S & iCarbonReactState;
77
92
 
78
93
  } else {
79
94
 
95
+ // This should only ever be done here, when the full state is being trashed.
96
+ // todo - does this suck in context of multiple instances?
97
+ clearCache({
98
+ ignoreWarning: true
99
+ });
100
+
80
101
  this.state = initialCarbonReactState as unknown as S & iCarbonReactState;
81
102
 
82
103
  }
83
104
 
84
- // This should only ever be done here, when the full state is being trashed.
85
- clearCache({
86
- ignoreWarning: true
87
- });
88
-
89
105
  /** We can think of our app as having one state; this state.
90
106
  * Long-term, I'd like us to store this state to local storage and only load updates on reload...
91
107
  * Class based components are far easier to manage state in local storage and pass state down to children.
@@ -95,23 +111,15 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
95
111
 
96
112
  }
97
113
 
98
- static getState<S>(): S {
99
- return CarbonReact.instance.state;
100
- }
114
+
101
115
 
102
116
  shouldComponentUpdate(
103
117
  nextProps: Readonly<any>,
104
118
  nextState: Readonly<iCarbonReactState>,
105
119
  _nextContext: any): boolean {
106
120
 
107
- if (this.props.shouldStatePersist === false) {
108
-
109
- CarbonReact.persistentState = undefined;
110
-
111
- } else {
112
-
113
- CarbonReact.persistentState = nextState;
114
-
121
+ if (this.props.instanceId) {
122
+ persistentStateMap.set(this.props.instanceId, nextState);
115
123
  }
116
124
 
117
125
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);