@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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "3.6.0",
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,13 +2,13 @@ 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';
9
9
  import Nest from 'components/Nest/Nest';
10
10
  import {initialRestfulObjectsState, iRestfulObjectArrayTypes} from "variables/C6";
11
- import CarbonWebSocket from "./components/WebSocket/CarbonWebSocket";
11
+ import CarbonWebSocket, {iCarbonWebSocketProps} from "./components/WebSocket/CarbonWebSocket";
12
12
 
13
13
 
14
14
  // our central container, single page application
@@ -45,18 +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,
53
+ websocket?: iCarbonWebSocketProps | boolean
51
54
  } & P, S & iCarbonReactState> {
52
55
 
53
- static instance: Component<{
56
+ context: Context<S & iCarbonReactState> = createContext(this.state);
57
+
58
+ // Private static member
59
+ protected static instance: CarbonReact & Component<{
54
60
  children?: ReactNode | ReactNode[],
55
61
  } & any, any & iCarbonReactState>;
56
62
 
57
- 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
+ }
58
70
 
59
- static persistentState?: iCarbonReactState = undefined;
71
+ protected ;
60
72
 
61
73
  static lastLocation = window.location.pathname;
62
74
 
@@ -64,27 +76,32 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
64
76
  // noinspection JSUnusedGlobalSymbols
65
77
  static whyDidYouRender = true;
66
78
 
67
- constructor(props) {
79
+ protected constructor(props: { children?: ReactNode | ReactNode[]; shouldStatePersist?: boolean | undefined; websocket?: boolean | iCarbonWebSocketProps | undefined; } & P) {
68
80
 
69
81
  super(props);
70
82
 
71
83
  console.log('CarbonORM TSX CONSTRUCTOR');
72
84
 
73
- if (CarbonReact.persistentState !== undefined && this.props.shouldStatePersist !== false) {
85
+ Object.assign(this, {
86
+ instance: this
87
+ })
74
88
 
75
- 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;
76
92
 
77
93
  } else {
78
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
+
79
101
  this.state = initialCarbonReactState as unknown as S & iCarbonReactState;
80
102
 
81
103
  }
82
104
 
83
- // This should only ever be done here, when the full state is being trashed.
84
- clearCache({
85
- ignoreWarning: true
86
- });
87
-
88
105
  /** We can think of our app as having one state; this state.
89
106
  * Long-term, I'd like us to store this state to local storage and only load updates on reload...
90
107
  * Class based components are far easier to manage state in local storage and pass state down to children.
@@ -94,23 +111,15 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
94
111
 
95
112
  }
96
113
 
97
- static getState<S>(): S {
98
- return CarbonReact.instance.state;
99
- }
114
+
100
115
 
101
116
  shouldComponentUpdate(
102
117
  nextProps: Readonly<any>,
103
118
  nextState: Readonly<iCarbonReactState>,
104
119
  _nextContext: any): boolean {
105
120
 
106
- if (this.props.shouldStatePersist === false) {
107
-
108
- CarbonReact.persistentState = undefined;
109
-
110
- } else {
111
-
112
- CarbonReact.persistentState = nextState;
113
-
121
+ if (this.props.instanceId) {
122
+ persistentStateMap.set(this.props.instanceId, nextState);
114
123
  }
115
124
 
116
125
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
@@ -132,7 +141,7 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
132
141
  }
133
142
  }
134
143
 
135
- render() : ReactElement {
144
+ render(): ReactElement {
136
145
 
137
146
  console.log('CarbonORM TSX RENDER');
138
147
 
@@ -155,7 +164,8 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
155
164
 
156
165
  return <>
157
166
  <GlobalHistory/>
158
- <CarbonWebSocket/>
167
+ {this.props.websocket &&
168
+ <CarbonWebSocket {...(true === this.props.websocket ? {} : this.props.websocket)} />}
159
169
  <Context value={this.state}>
160
170
  {this.props.children}
161
171
  </Context>