@carbonorm/carbonreact 4.0.14 → 4.0.16
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/CarbonReact.d.ts +6 -5
- package/dist/index.cjs.js +31 -28
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +31 -28
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/CarbonReact.tsx +39 -35
package/package.json
CHANGED
package/src/CarbonReact.tsx
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { clearCache } from "@carbonorm/carbonnode";
|
|
2
1
|
import changed from "hoc/changed";
|
|
3
2
|
import { GlobalHistory } from "hoc/GlobalHistory";
|
|
4
3
|
import hexToRgb from "hoc/hexToRgb";
|
|
@@ -12,10 +11,7 @@ import CarbonWebSocket, { iCarbonWebSocketProps } from "./components/WebSocket/C
|
|
|
12
11
|
import updateRestfulObjectArrays, { iUpdateRestfulObjectArrays } from "./hoc/updateRestfulObjectArrays";
|
|
13
12
|
import deleteRestfulObjectArrays, { iDeleteRestfulObjectArrays } from "./hoc/deleteRestfulObjectArrays";
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
export type tStatefulApiData<T extends {
|
|
17
|
-
[key: string]: any
|
|
18
|
-
} = {}> = T[] | undefined | null;
|
|
14
|
+
export type tStatefulApiData<T extends { [key: string]: any } = {}> = T[] | undefined | null;
|
|
19
15
|
|
|
20
16
|
// our central container, single page application
|
|
21
17
|
export interface iCarbonReactState {
|
|
@@ -51,23 +47,40 @@ export function isJsonString(str: string) {
|
|
|
51
47
|
abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
|
|
52
48
|
children?: ReactNode | ReactNode[],
|
|
53
49
|
instanceId?: string,
|
|
50
|
+
persistentState?: boolean,
|
|
54
51
|
websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false
|
|
55
52
|
} & P, S> {
|
|
56
53
|
|
|
57
|
-
private static
|
|
58
|
-
private static activeInstances = new Map<string, CarbonReact<any, any>>();
|
|
54
|
+
private static allInstances = new Map<string, CarbonReact<any, any>>();
|
|
59
55
|
|
|
60
56
|
context: Context<S & iCarbonReactState> = createContext(this.state);
|
|
61
57
|
protected target: typeof CarbonReact;
|
|
62
58
|
|
|
63
59
|
protected static _instance: ThisType<CarbonReact<any, any>>;
|
|
64
60
|
|
|
65
|
-
static getInstance<T extends CarbonReact<any, any>>(): T {
|
|
61
|
+
static getInstance<T extends CarbonReact<any, any>>(instanceId?: string): T {
|
|
62
|
+
|
|
63
|
+
const identifier = this.generateIdentifier(instanceId);
|
|
64
|
+
|
|
65
|
+
if (undefined !== instanceId) {
|
|
66
|
+
if (CarbonReact.allInstances.has(identifier)) {
|
|
67
|
+
return CarbonReact.allInstances.get(identifier) as T;
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`No instance has been instantiated yet for class (${this.name}) with instanceId (${instanceId})`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!this._instance) {
|
|
73
|
+
throw new Error(`No instance has been instantiated yet for class (${this.name})`);
|
|
74
|
+
}
|
|
75
|
+
|
|
66
76
|
return this._instance as T;
|
|
77
|
+
|
|
67
78
|
}
|
|
79
|
+
|
|
68
80
|
static get instance() {
|
|
69
81
|
return this.getInstance();
|
|
70
82
|
}
|
|
83
|
+
|
|
71
84
|
static set instance(instance: CarbonReact<any, any>) {
|
|
72
85
|
this._instance = instance;
|
|
73
86
|
}
|
|
@@ -90,39 +103,38 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
|
|
|
90
103
|
|
|
91
104
|
protected constructor(props: {
|
|
92
105
|
children?: ReactNode | ReactNode[];
|
|
93
|
-
shouldStatePersist?: boolean | undefined;
|
|
94
106
|
websocket?: boolean | iCarbonWebSocketProps<P, S> | undefined;
|
|
95
107
|
instanceId?: string; // Optional instanceId from props
|
|
108
|
+
persistentState?: boolean; // Optional persistentState from props
|
|
96
109
|
} & P) {
|
|
97
110
|
super(props);
|
|
98
111
|
|
|
99
|
-
const
|
|
100
|
-
const identifier = props.instanceId || target.name;
|
|
112
|
+
const identifier = this.generateIdentifier();
|
|
101
113
|
|
|
102
|
-
if (CarbonReact.
|
|
103
|
-
|
|
114
|
+
if (props.persistentState && CarbonReact.allInstances.has(identifier)) {
|
|
115
|
+
// Reuse the state from the existing instance
|
|
116
|
+
this.state = CarbonReact.allInstances.get(identifier)!.state as S & iCarbonReactState;
|
|
117
|
+
} else {
|
|
118
|
+
this.state = initialCarbonReactState as unknown as S & iCarbonReactState;
|
|
119
|
+
CarbonReact.allInstances.set(identifier, this);
|
|
104
120
|
}
|
|
105
121
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.target = target;
|
|
122
|
+
this.target = new.target;
|
|
109
123
|
console.log('CarbonORM TSX CONSTRUCTOR');
|
|
110
124
|
|
|
111
|
-
Object.assign(target, {
|
|
125
|
+
Object.assign(this.target, {
|
|
112
126
|
_instance: this
|
|
113
127
|
});
|
|
128
|
+
}
|
|
114
129
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
ignoreWarning: true
|
|
120
|
-
});
|
|
121
|
-
this.state = initialCarbonReactState as unknown as S & iCarbonReactState;
|
|
122
|
-
}
|
|
130
|
+
private static generateIdentifier(instanceId?: string): string {
|
|
131
|
+
const className = this.name;
|
|
132
|
+
return instanceId ? `${className}-${instanceId}` : className;
|
|
133
|
+
}
|
|
123
134
|
|
|
124
|
-
|
|
125
|
-
|
|
135
|
+
private generateIdentifier(): string {
|
|
136
|
+
const className = (this.constructor as typeof CarbonReact).name;
|
|
137
|
+
return this.props.instanceId ? `${className}-${this.props.instanceId}` : className;
|
|
126
138
|
}
|
|
127
139
|
|
|
128
140
|
shouldComponentUpdate(
|
|
@@ -130,9 +142,6 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
|
|
|
130
142
|
nextState: Readonly<S>,
|
|
131
143
|
_nextContext: any): boolean {
|
|
132
144
|
|
|
133
|
-
const identifier = this.props.instanceId || (this.constructor as typeof CarbonReact).name;
|
|
134
|
-
CarbonReact.persistentStateMap.set(identifier, nextState);
|
|
135
|
-
|
|
136
145
|
changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
|
|
137
146
|
changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);
|
|
138
147
|
|
|
@@ -179,11 +188,6 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
|
|
|
179
188
|
<ToastContainer />
|
|
180
189
|
</>;
|
|
181
190
|
}
|
|
182
|
-
|
|
183
|
-
componentWillUnmount() {
|
|
184
|
-
const identifier = this.props.instanceId || (this.constructor as typeof CarbonReact).name;
|
|
185
|
-
CarbonReact.activeInstances.delete(identifier);
|
|
186
|
-
}
|
|
187
191
|
}
|
|
188
192
|
|
|
189
193
|
export default CarbonReact;
|