@carbonorm/carbonreact 4.0.1 → 4.0.3

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": "4.0.1",
4
+ "version": "4.0.3",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
@@ -48,22 +48,31 @@ export function isJsonString(str: string) {
48
48
  return true;
49
49
  }
50
50
 
51
- const persistentStateMap = new Map<string, iCarbonReactState>();
52
-
53
- abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = {}> extends Component<{
51
+ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = typeof initialCarbonReactState> extends Component<{
54
52
  children?: ReactNode | ReactNode[],
55
53
  instanceId?: string,
56
54
  websocket?: Omit<iCarbonWebSocketProps, "instance"> | boolean
57
- } & P, S & iCarbonReactState> {
55
+ } & P, S> {
58
56
 
59
- context: Context<S & iCarbonReactState> = createContext(this.state);
57
+ private static persistentStateMap = new Map<string, { [key: string]: any; }>();
60
58
 
61
- // Private static member
62
- // we actually implement this in the constructor todo - test this
63
- protected static instance: CarbonReact;
59
+ // Context is for functional components to access the state of this class efficiently
60
+ context: Context<S> = createContext(this.state);
64
61
 
65
62
  protected target: typeof CarbonReact;
66
63
 
64
+ protected static _instance: CarbonReact;
65
+
66
+ static get instance() {
67
+ // Here `this` refers to the calling class in static context
68
+ return this._instance;
69
+ }
70
+
71
+ static set instance(instance: CarbonReact) {
72
+ this._instance = instance;
73
+ }
74
+
75
+
67
76
  protected updateRestfulObjectArrays = <ObjectType extends { [key: string]: any; } = {}>
68
77
  (rest: Omit<iUpdateRestfulObjectArrays<ObjectType, S, P>, "instance">) => updateRestfulObjectArrays<ObjectType, S, P>({
69
78
  instance: this,
@@ -98,12 +107,12 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = {}> exten
98
107
  // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
99
108
  // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
100
109
  Object.assign(new.target, {
101
- instance: this
110
+ _instance: this
102
111
  })
103
112
 
104
- if (this.props.instanceId && persistentStateMap.has(this.props.instanceId)) {
113
+ if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
105
114
 
106
- this.state = persistentStateMap.get(this.props.instanceId) as S & iCarbonReactState;
115
+ this.state = CarbonReact.persistentStateMap.get(this.props.instanceId) as S & iCarbonReactState;
107
116
 
108
117
  } else {
109
118
 
@@ -128,12 +137,12 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = {}> exten
128
137
 
129
138
 
130
139
  shouldComponentUpdate(
131
- nextProps: Readonly<any>,
132
- nextState: Readonly<iCarbonReactState>,
140
+ nextProps: Readonly<P>,
141
+ nextState: Readonly<S>,
133
142
  _nextContext: any): boolean {
134
143
 
135
144
  if (this.props.instanceId) {
136
- persistentStateMap.set(this.props.instanceId, nextState);
145
+ CarbonReact.persistentStateMap.set(this.props.instanceId, nextState);
137
146
  }
138
147
 
139
148
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
@@ -144,7 +153,7 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = {}> exten
144
153
 
145
154
  }
146
155
 
147
- componentDidUpdate(_prevProps: Readonly<any>, _prevState: Readonly<iCarbonReactState>, _snapshot?: any) {
156
+ componentDidUpdate(_prevProps: Readonly<P>, _prevState: Readonly<S>, _snapshot?: any) {
148
157
  if (CarbonReact.lastLocation !== location.pathname) {
149
158
  CarbonReact.lastLocation = location.pathname;
150
159
  const websocket = this.state.websocket;