@carbonorm/carbonreact 4.0.2 → 4.0.4

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.
@@ -17,17 +17,17 @@ export interface iCarbonReactState {
17
17
  export declare const initialRequiredCarbonORMState: iCarbonReactState;
18
18
  export declare const initialCarbonReactState: iCarbonReactState & iRestfulObjectArrayTypes;
19
19
  export declare function isJsonString(str: string): boolean;
20
- declare abstract class CarbonReact<P = {}, S extends {
21
- [key: string]: any;
22
- } = typeof initialCarbonReactState> extends Component<{
20
+ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
23
21
  children?: ReactNode | ReactNode[];
24
22
  instanceId?: string;
25
- websocket?: Omit<iCarbonWebSocketProps, "instance"> | boolean;
23
+ websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false;
26
24
  } & P, S> {
27
25
  private static persistentStateMap;
28
- context: Context<S>;
29
- protected static instance: CarbonReact;
26
+ context: Context<S & iCarbonReactState>;
30
27
  protected target: typeof CarbonReact;
28
+ protected static _instance: typeof this;
29
+ static get instance(): typeof this;
30
+ static set instance(instance: typeof this);
31
31
  protected updateRestfulObjectArrays: <ObjectType extends {
32
32
  [key: string]: any;
33
33
  } = {}>(rest: Omit<iUpdateRestfulObjectArrays<ObjectType, S, P>, "instance">) => void;
@@ -39,7 +39,7 @@ declare abstract class CarbonReact<P = {}, S extends {
39
39
  protected constructor(props: {
40
40
  children?: ReactNode | ReactNode[];
41
41
  shouldStatePersist?: boolean | undefined;
42
- websocket?: boolean | iCarbonWebSocketProps | undefined;
42
+ websocket?: boolean | iCarbonWebSocketProps<P, S> | undefined;
43
43
  } & P);
44
44
  shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, _nextContext: any): boolean;
45
45
  componentDidUpdate(_prevProps: Readonly<P>, _prevState: Readonly<S>, _snapshot?: any): void;
@@ -1,4 +1,4 @@
1
- import CarbonReact from "CarbonReact";
1
+ import CarbonReact, { iCarbonReactState } from "CarbonReact";
2
2
  import { ReactNode } from "react";
3
3
  export interface iAlertButtonOptions {
4
4
  text: string;
@@ -6,10 +6,10 @@ export interface iAlertButtonOptions {
6
6
  className?: string;
7
7
  color: "default" | "primary" | "secondary" | "inherit" | "danger" | "info" | "success" | "warning" | undefined;
8
8
  }
9
- export interface iAlert {
9
+ export interface iAlert<P, S> {
10
10
  title: string;
11
11
  text: string;
12
- instance: CarbonReact;
12
+ instance: CarbonReact<P, S>;
13
13
  component?: ReactNode;
14
14
  icon?: "warning" | "error" | "success" | "info" | "question" | null;
15
15
  buttons?: (iAlertButtonOptions)[] | undefined;
@@ -22,7 +22,7 @@ export interface iAlert {
22
22
  [key: string]: any;
23
23
  };
24
24
  }
25
- export declare function addAlert(props: iAlert): void;
26
- export default function Alert({ instance }: {
27
- instance: CarbonReact;
25
+ export declare function addAlert<P, S extends Partial<iCarbonReactState>>(props: iAlert<P, S>): void;
26
+ export default function Alert<P, S extends Partial<iCarbonReactState>>({ instance }: {
27
+ instance: CarbonReact<P, S>;
28
28
  }): import("react/jsx-runtime").JSX.Element | null;
@@ -1,6 +1,6 @@
1
- import CarbonReact from "../../CarbonReact";
1
+ import CarbonReact, { iCarbonReactState } from "../../CarbonReact";
2
2
  import { ReactElement } from "react";
3
- declare const _default: (props: {
4
- instance: CarbonReact;
3
+ declare const _default: <P, S extends Partial<iCarbonReactState>>(props: {
4
+ instance: CarbonReact<P, S>;
5
5
  }) => ReactElement;
6
6
  export default _default;
@@ -1,10 +1,10 @@
1
- import CarbonReact from "CarbonReact";
1
+ import CarbonReact, { iCarbonReactState } from "CarbonReact";
2
2
  import { tC6Tables, tC6RestApi } from "@carbonorm/carbonnode";
3
- export interface iCarbonWebSocketProps {
3
+ export interface iCarbonWebSocketProps<P, S> {
4
4
  url?: string;
5
5
  timeoutSeconds?: number;
6
6
  heartbeatSeconds?: number;
7
- instance: CarbonReact;
7
+ instance: CarbonReact<P, S>;
8
8
  TABLES?: tC6Tables;
9
9
  WsLiveUpdates?: tC6RestApi;
10
10
  }
@@ -12,5 +12,5 @@ export interface iCarbonWebSocketProps {
12
12
  * @function connect
13
13
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
14
14
  **/
15
- export declare function initiateWebsocket(props: iCarbonWebSocketProps): void;
16
- export default function (props: iCarbonWebSocketProps): null;
15
+ export declare function initiateWebsocket<P, S extends Partial<iCarbonReactState>>(props: iCarbonWebSocketProps<P, S>): void;
16
+ export default function <P, S extends Partial<iCarbonReactState>>(props: iCarbonWebSocketProps<P, S>): null;
@@ -2,9 +2,7 @@ import CarbonReact, { tStatefulApiData } from "CarbonReact";
2
2
  import { KeysMatching } from "./KeysMatching";
3
3
  export interface iDeleteRestfulObjectArrays<ObjectType extends {
4
4
  [key: string]: any;
5
- } = {}, S extends {
6
- [key: string]: any;
7
- } = CarbonReact['state'], P = CarbonReact['props']> {
5
+ } = {}, S = CarbonReact['state'], P = CarbonReact['props']> {
8
6
  instance: CarbonReact<P, S>;
9
7
  dataOrCallback: ObjectType[] | ((state: Readonly<S>, props: Readonly<P>) => ObjectType[] | null);
10
8
  stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>;
@@ -13,6 +11,4 @@ export interface iDeleteRestfulObjectArrays<ObjectType extends {
13
11
  }
14
12
  export default function deleteRestfulObjectArrays<ObjectType extends {
15
13
  [key: string]: any;
16
- } = {}, S extends {
17
- [key: string]: any;
18
- } = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, callback }: iDeleteRestfulObjectArrays<ObjectType, S, P>): void;
14
+ } = {}, S = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, callback }: iDeleteRestfulObjectArrays<ObjectType, S, P>): void;
@@ -1,4 +1,4 @@
1
- import CarbonReact, { tStatefulApiData } from "CarbonReact";
1
+ import CarbonReact, { iCarbonReactState, tStatefulApiData } from "CarbonReact";
2
2
  import { KeysMatching } from "./KeysMatching";
3
3
  export declare enum eUpdateInsertMethod {
4
4
  REPLACE = 0,
@@ -7,9 +7,7 @@ export declare enum eUpdateInsertMethod {
7
7
  }
8
8
  export interface iUpdateRestfulObjectArrays<ObjectType extends {
9
9
  [key: string]: any;
10
- } = {}, S extends {
11
- [key: string]: any;
12
- } = CarbonReact['state'], P = CarbonReact['props']> {
10
+ } = {}, S extends CarbonReact['state'] = CarbonReact['state'], P = CarbonReact['props']> {
13
11
  instance: CarbonReact<P, S>;
14
12
  dataOrCallback: ObjectType[] | ((state: Readonly<S>, props: Readonly<P>) => ObjectType[] | null);
15
13
  stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>;
@@ -28,6 +26,4 @@ export interface iUpdateRestfulObjectArrays<ObjectType extends {
28
26
  */
29
27
  export default function updateRestfulObjectArrays<ObjectType extends {
30
28
  [key: string]: any;
31
- } = {}, S extends {
32
- [key: string]: any;
33
- } = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, insertUpdateOrder, callback, }: iUpdateRestfulObjectArrays<ObjectType, S, P>): void;
29
+ } = {}, S extends iCarbonReactState = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, insertUpdateOrder, callback, }: iUpdateRestfulObjectArrays<ObjectType, S, P>): void;
package/dist/index.cjs.js CHANGED
@@ -4361,11 +4361,16 @@ function isJsonString(str) {
4361
4361
  }
4362
4362
  class CarbonReact extends react.Component {
4363
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
+ return this;
4370
+ }
4371
+ static set instance(instance) {
4372
+ this._instance = instance;
4373
+ }
4369
4374
  updateRestfulObjectArrays = (rest) => updateRestfulObjectArrays({
4370
4375
  instance: this,
4371
4376
  ...rest
@@ -4385,7 +4390,7 @@ class CarbonReact extends react.Component {
4385
4390
  // this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
4386
4391
  // new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
4387
4392
  Object.assign(new.target, {
4388
- instance: this
4393
+ _instance: this
4389
4394
  });
4390
4395
  if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
4391
4396
  this.state = CarbonReact.persistentStateMap.get(this.props.instanceId);
@@ -4429,11 +4434,11 @@ class CarbonReact extends react.Component {
4429
4434
  console.log('%c color (' + colorHex + ')', 'color: ' + colorHex);
4430
4435
  const nest = jsxRuntime_1(Nest, { position: 'fixed', backgroundColor: '', color: hexToRgb(colorHex), count: 100 });
4431
4436
  if (this.state.backendThrowable.length > 0) {
4432
- return jsxRuntime_2(jsxRuntime_3, { children: [nest, jsxRuntime_1(BackendThrowable, { instance: CarbonReact.instance })] });
4437
+ return jsxRuntime_2(jsxRuntime_3, { children: [nest, jsxRuntime_1(BackendThrowable, { instance: this })] });
4433
4438
  }
4434
4439
  const Context = this.context.Provider;
4435
4440
  return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), this.props.websocket &&
4436
- jsxRuntime_1(CarbonWebSocket, { ...(true === this.props.websocket ? {} : this.props.websocket), instance: CarbonReact.instance }), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(reactToastify.ToastContainer, {})] });
4441
+ jsxRuntime_1(CarbonWebSocket, { ...(false !== this.props.websocket ? this.props.websocket : {}), instance: this }), jsxRuntime_1(Context, { value: this.state, children: this.props.children }), jsxRuntime_1(reactToastify.ToastContainer, {})] });
4437
4442
  }
4438
4443
  }
4439
4444