@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.
@@ -19,14 +19,17 @@ export declare const initialCarbonReactState: iCarbonReactState & iRestfulObject
19
19
  export declare function isJsonString(str: string): boolean;
20
20
  declare abstract class CarbonReact<P = {}, S extends {
21
21
  [key: string]: any;
22
- } = {}> extends Component<{
22
+ } = typeof initialCarbonReactState> extends Component<{
23
23
  children?: ReactNode | ReactNode[];
24
24
  instanceId?: string;
25
25
  websocket?: Omit<iCarbonWebSocketProps, "instance"> | boolean;
26
- } & P, S & iCarbonReactState> {
27
- context: Context<S & iCarbonReactState>;
28
- protected static instance: CarbonReact;
26
+ } & P, S> {
27
+ private static persistentStateMap;
28
+ context: Context<S>;
29
29
  protected target: typeof CarbonReact;
30
+ protected static _instance: CarbonReact;
31
+ static get instance(): CarbonReact;
32
+ static set instance(instance: CarbonReact);
30
33
  protected updateRestfulObjectArrays: <ObjectType extends {
31
34
  [key: string]: any;
32
35
  } = {}>(rest: Omit<iUpdateRestfulObjectArrays<ObjectType, S, P>, "instance">) => void;
@@ -40,8 +43,8 @@ declare abstract class CarbonReact<P = {}, S extends {
40
43
  shouldStatePersist?: boolean | undefined;
41
44
  websocket?: boolean | iCarbonWebSocketProps | undefined;
42
45
  } & P);
43
- shouldComponentUpdate(nextProps: Readonly<any>, nextState: Readonly<iCarbonReactState>, _nextContext: any): boolean;
44
- componentDidUpdate(_prevProps: Readonly<any>, _prevState: Readonly<iCarbonReactState>, _snapshot?: any): void;
46
+ shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, _nextContext: any): boolean;
47
+ componentDidUpdate(_prevProps: Readonly<P>, _prevState: Readonly<S>, _snapshot?: any): void;
45
48
  render(): ReactElement;
46
49
  }
47
50
  export default CarbonReact;
package/dist/index.cjs.js CHANGED
@@ -4359,13 +4359,19 @@ function isJsonString(str) {
4359
4359
  }
4360
4360
  return true;
4361
4361
  }
4362
- const persistentStateMap = new Map();
4363
4362
  class CarbonReact extends react.Component {
4363
+ static persistentStateMap = new Map();
4364
+ // Context is for functional components to access the state of this class efficiently
4364
4365
  context = react.createContext(this.state);
4365
- // Private static member
4366
- // we actually implement this in the constructor todo - test this
4367
- static instance;
4368
4366
  target;
4367
+ static _instance;
4368
+ static get instance() {
4369
+ // Here `this` refers to the calling class in static context
4370
+ return this._instance;
4371
+ }
4372
+ static set instance(instance) {
4373
+ this._instance = instance;
4374
+ }
4369
4375
  updateRestfulObjectArrays = (rest) => updateRestfulObjectArrays({
4370
4376
  instance: this,
4371
4377
  ...rest
@@ -4385,10 +4391,10 @@ class CarbonReact extends react.Component {
4385
4391
  // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
4386
4392
  // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
4387
4393
  Object.assign(new.target, {
4388
- instance: this
4394
+ _instance: this
4389
4395
  });
4390
- if (this.props.instanceId && persistentStateMap.has(this.props.instanceId)) {
4391
- this.state = persistentStateMap.get(this.props.instanceId);
4396
+ if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
4397
+ this.state = CarbonReact.persistentStateMap.get(this.props.instanceId);
4392
4398
  }
4393
4399
  else {
4394
4400
  // This should only ever be done here, when the full state is being trashed.
@@ -4407,7 +4413,7 @@ class CarbonReact extends react.Component {
4407
4413
  }
4408
4414
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4409
4415
  if (this.props.instanceId) {
4410
- persistentStateMap.set(this.props.instanceId, nextState);
4416
+ CarbonReact.persistentStateMap.set(this.props.instanceId, nextState);
4411
4417
  }
4412
4418
  changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
4413
4419
  changed(this.constructor.name + ' (C6Api)', 'state', this.state, nextState);