@carbonorm/carbonreact 6.0.0 → 6.0.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.
@@ -4,6 +4,7 @@ import { iRestfulObjectArrayTypes } from "schema/C6";
4
4
  import { iCarbonWebSocketProps } from "components/WebSocket/CarbonWebSocket";
5
5
  import { iUpdateRestfulObjectArrays } from "state/updateRestfulObjectArrays";
6
6
  import { iDeleteRestfulObjectArrays } from "state/deleteRestfulObjectArrays";
7
+ import { iStateAdapter } from "state/stateAdapter";
7
8
  export type tStatefulApiData<T extends {
8
9
  [key: string]: any;
9
10
  } = {}> = T[] | undefined | null;
@@ -28,6 +29,7 @@ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbon
28
29
  persistentState?: boolean;
29
30
  routerType?: eRouterType;
30
31
  websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false;
32
+ stateAdapter?: iStateAdapter<S>;
31
33
  } & P, S> {
32
34
  private static allInstances;
33
35
  context: Context<S & iCarbonReactState>;
@@ -49,7 +51,9 @@ declare abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbon
49
51
  websocket?: boolean | iCarbonWebSocketProps<P, S> | undefined;
50
52
  instanceId?: string;
51
53
  persistentState?: boolean;
54
+ stateAdapter?: iStateAdapter<S>;
52
55
  } & P);
56
+ stateAdapter?: iStateAdapter<S>;
53
57
  private static generateIdentifier;
54
58
  private generateIdentifier;
55
59
  shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, _nextContext: any): boolean;
package/dist/index.cjs CHANGED
@@ -9,7 +9,6 @@ var Skeleton = require('react-loading-skeleton');
9
9
  var reactRouterDom = require('react-router-dom');
10
10
  var reactToastify = require('react-toastify');
11
11
  var carbonnode = require('@carbonorm/carbonnode');
12
- var module$1 = require('module');
13
12
 
14
13
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
15
14
  var getStatefulObjectWithWhere = ({ request }) => {
@@ -2655,9 +2654,10 @@ exports.eUpdateInsertMethod = void 0;
2655
2654
  * @param insertUpdateOrder - The order in which new data should be inserted/updated.
2656
2655
  * @param callback - Optional callback function to run after state update.
2657
2656
  */
2658
- function updateRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueObjectId, insertUpdateOrder = exports.eUpdateInsertMethod.LAST, callback, }) {
2657
+ function updateRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueObjectId, insertUpdateOrder = exports.eUpdateInsertMethod.LAST, callback, stateAdapter, }) {
2659
2658
  const uniqueObjectIds = Array.isArray(uniqueObjectId) ? uniqueObjectId : [uniqueObjectId];
2660
- instance.setState((previousBootstrapState, props) => {
2659
+ const resolvedStateAdapter = stateAdapter ?? instance.stateAdapter;
2660
+ const computeNextState = (previousBootstrapState, props) => {
2661
2661
  let newOrReplacementData = [];
2662
2662
  if (Array.isArray(dataOrCallback)) {
2663
2663
  newOrReplacementData = dataOrCallback;
@@ -2712,12 +2712,23 @@ function updateRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueO
2712
2712
  throw new Error("The insertUpdateOrder (eUpdateInsertMethod) was not implemented");
2713
2713
  }
2714
2714
  return newState;
2715
- }, callback);
2715
+ };
2716
+ if (resolvedStateAdapter) {
2717
+ const previousBootstrapState = resolvedStateAdapter.getState();
2718
+ const nextState = computeNextState(previousBootstrapState, instance.props);
2719
+ if (nextState !== null) {
2720
+ resolvedStateAdapter.setState(nextState);
2721
+ callback?.();
2722
+ }
2723
+ return;
2724
+ }
2725
+ instance.setState(computeNextState, callback);
2716
2726
  }
2717
2727
 
2718
- function deleteRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueObjectId, callback }) {
2728
+ function deleteRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueObjectId, callback, stateAdapter }) {
2719
2729
  const uniqueObjectIds = Array.isArray(uniqueObjectId) ? uniqueObjectId : [uniqueObjectId];
2720
- instance.setState((previousBootstrapState, props) => {
2730
+ const resolvedStateAdapter = stateAdapter ?? instance.stateAdapter;
2731
+ const computeNextState = (previousBootstrapState, props) => {
2721
2732
  let newOrReplacementData = [];
2722
2733
  if (Array.isArray(dataOrCallback)) {
2723
2734
  newOrReplacementData = dataOrCallback;
@@ -2737,7 +2748,17 @@ function deleteRestfulObjectArrays({ instance, dataOrCallback, stateKey, uniqueO
2737
2748
  return {
2738
2749
  [stateKey]: updatedStateProperty
2739
2750
  };
2740
- }, callback);
2751
+ };
2752
+ if (resolvedStateAdapter) {
2753
+ const previousBootstrapState = resolvedStateAdapter.getState();
2754
+ const nextState = computeNextState(previousBootstrapState, instance.props);
2755
+ if (nextState !== null) {
2756
+ resolvedStateAdapter.setState(nextState);
2757
+ callback?.();
2758
+ }
2759
+ return;
2760
+ }
2761
+ instance.setState(computeNextState, callback);
2741
2762
  }
2742
2763
 
2743
2764
  const initialRequiredCarbonORMState = {
@@ -2813,11 +2834,13 @@ class CarbonReact extends react.Component {
2813
2834
  CarbonReact.allInstances.set(identifier, this);
2814
2835
  }
2815
2836
  this.target = new.target;
2837
+ this.stateAdapter = props.stateAdapter;
2816
2838
  console.log('CarbonORM TSX CONSTRUCTOR');
2817
2839
  Object.assign(this.target, {
2818
2840
  _instance: this
2819
2841
  });
2820
2842
  }
2843
+ stateAdapter;
2821
2844
  static generateIdentifier(instanceId) {
2822
2845
  const className = this.name;
2823
2846
  return instanceId ? `${className}-${instanceId}` : className;
@@ -3263,6 +3286,29 @@ function useWindowDimensions() {
3263
3286
  return windowDimensions;
3264
3287
  }
3265
3288
 
3289
+ class InMemoryStateAdapter {
3290
+ state;
3291
+ constructor(initialState) {
3292
+ this.state = initialState;
3293
+ }
3294
+ getState() {
3295
+ return this.state;
3296
+ }
3297
+ setState(partial) {
3298
+ this.state = { ...this.state, ...partial };
3299
+ }
3300
+ snapshot() {
3301
+ return this.state;
3302
+ }
3303
+ hydrate(snapshot) {
3304
+ this.state = snapshot;
3305
+ }
3306
+ }
3307
+ const serializeStateSnapshot = (state) => JSON.stringify(state);
3308
+ const deserializeStateSnapshot = (serialized) => JSON.parse(serialized);
3309
+ const getStateSnapshot = (adapter) => adapter.getState();
3310
+ const createInMemoryStateAdapter = (initialState) => new InMemoryStateAdapter(initialState);
3311
+
3266
3312
  const addValidSQL = [];
3267
3313
  function addValidSQL$1 (sql) {
3268
3314
  const { expect } = require("@jest/globals");
@@ -3275,11 +3321,28 @@ function ValidSQL (sql) {
3275
3321
  validSQL.push({ [expect.getState().currentTestName.replaceAll(" ", "_").toLowerCase()]: sql });
3276
3322
  }
3277
3323
 
3278
- const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
3324
+ const nodeModulePromise = typeof process !== "undefined" && process.versions?.node
3325
+ ? Promise.all([
3326
+ import('node:fs'),
3327
+ import('node:util'),
3328
+ import('@testing-library/react'),
3329
+ ])
3330
+ : null;
3331
+ const getNodeModules = async () => {
3332
+ if (!nodeModulePromise) {
3333
+ throw new Error("setupTests requires a Node runtime.");
3334
+ }
3335
+ const [fsModule, utilModule, testingLib] = await nodeModulePromise;
3336
+ return {
3337
+ fs: fsModule,
3338
+ inspect: utilModule.inspect,
3339
+ waitFor: testingLib.waitFor,
3340
+ };
3341
+ };
3279
3342
  function setupTests ({ sqlDirectory = './logs/rest/', logsDirectory = './logs/tests/' } = {}) {
3280
- const fs = require$1("fs");
3281
- const { inspect } = require$1("util");
3282
- const { waitFor } = require$1("@testing-library/react");
3343
+ if (!nodeModulePromise) {
3344
+ throw new Error("setupTests requires a Node runtime.");
3345
+ }
3283
3346
  const originalWindowLocation = window.location.href;
3284
3347
  const consoleOriginal = console;
3285
3348
  const isVerboseEnabled = carbonnode.isVerbose();
@@ -3294,7 +3357,10 @@ function setupTests ({ sqlDirectory = './logs/rest/', logsDirectory = './logs/te
3294
3357
  return response;
3295
3358
  });
3296
3359
  // @link https://stackoverflow.com/questions/13542667/create-directory-when-writing-to-file-in-node-js
3297
- const asyncFileLogging = async (...args) => fs.writeFileSync(logsFile(), '\n' + inspect(args.length === 1 ? args.pop() : args, false, 10, true), { flag: "a+" });
3360
+ const asyncFileLogging = async (...args) => {
3361
+ const { fs, inspect } = await getNodeModules();
3362
+ fs.writeFileSync(logsFile(), '\n' + inspect(args.length === 1 ? args.pop() : args, false, 10, true), { flag: "a+" });
3363
+ };
3298
3364
  global.console = {
3299
3365
  ...console,
3300
3366
  // use jest.fn() to silence, comment out to leave as it is
@@ -3313,6 +3379,7 @@ function setupTests ({ sqlDirectory = './logs/rest/', logsDirectory = './logs/te
3313
3379
  warn: (...args) => (isVerboseEnabled && consoleOriginal.warn(...args), asyncFileLogging('warn', ...args)),
3314
3380
  };
3315
3381
  afterEach(async () => {
3382
+ const { waitFor, fs } = await getNodeModules();
3316
3383
  await waitFor(async () => {
3317
3384
  expect(carbonnode.checkAllRequestsComplete()).toEqual(true);
3318
3385
  }, { timeout: 3000, interval: 1000 });
@@ -3370,6 +3437,7 @@ exports.CarbonReact = CarbonReact;
3370
3437
  exports.CarbonWebSocket = CarbonWebSocket;
3371
3438
  exports.ErrorHttpCode = ErrorHttpCode;
3372
3439
  exports.GlobalHistory = GlobalHistory;
3440
+ exports.InMemoryStateAdapter = InMemoryStateAdapter;
3373
3441
  exports.Loading = Loading;
3374
3442
  exports.OutsideClickHandler = OutsideClickHandler;
3375
3443
  exports.PageNotFound = PageNotFound;
@@ -3380,12 +3448,15 @@ exports.addValidSQL = addValidSQL$1;
3380
3448
  exports.carbons = carbons;
3381
3449
  exports.changed = changed;
3382
3450
  exports.comments = comments;
3451
+ exports.createInMemoryStateAdapter = createInMemoryStateAdapter;
3383
3452
  exports.deleteRestfulObjectArrays = deleteRestfulObjectArrays;
3453
+ exports.deserializeStateSnapshot = deserializeStateSnapshot;
3384
3454
  exports.documentation = documentation;
3385
3455
  exports.feature_group_references = feature_group_references;
3386
3456
  exports.features = features;
3387
3457
  exports.getEnv = getEnv;
3388
3458
  exports.getRootStyleValue = getRootStyleValue;
3459
+ exports.getStateSnapshot = getStateSnapshot;
3389
3460
  exports.getStatefulObjectWithWhere = getStatefulObjectWithWhere;
3390
3461
  exports.getStyles = getStyles;
3391
3462
  exports.group_references = group_references;
@@ -3405,6 +3476,7 @@ exports.parseMultipleJson = parseMultipleJson;
3405
3476
  exports.photos = photos;
3406
3477
  exports.reports = reports;
3407
3478
  exports.scrollIntoView = ScrollIntoViewDirective;
3479
+ exports.serializeStateSnapshot = serializeStateSnapshot;
3408
3480
  exports.setCookies = setCookies;
3409
3481
  exports.setupTests = setupTests;
3410
3482
  exports.toDataURL = toDataURL;