j-templates 8.0.6 → 8.0.8

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.
@@ -8,6 +8,7 @@ import { JsonDiffResult } from "../../Utils/json";
8
8
  */
9
9
  export declare abstract class Store {
10
10
  protected keyFunc?: (value: any) => string | undefined;
11
+ private cloneData;
11
12
  private rootMap;
12
13
  private createNode;
13
14
  /**
@@ -15,7 +16,7 @@ export declare abstract class Store {
15
16
  * @param keyFunc Optional function to generate a key for a given data value.
16
17
  * When provided, enables alias functionality where values can reference other root objects.
17
18
  */
18
- constructor(keyFunc?: (value: any) => string | undefined);
19
+ constructor(keyFunc?: (value: any) => string | undefined, cloneData?: boolean);
19
20
  /**
20
21
  * Retrieves a value from the store by id.
21
22
  * @template O - The type of the value to retrieve
@@ -16,11 +16,12 @@ class Store {
16
16
  * @param keyFunc Optional function to generate a key for a given data value.
17
17
  * When provided, enables alias functionality where values can reference other root objects.
18
18
  */
19
- constructor(keyFunc) {
19
+ constructor(keyFunc, cloneData = true) {
20
20
  this.keyFunc = keyFunc;
21
+ this.cloneData = cloneData;
21
22
  this.rootMap = new Map();
22
23
  const aliasFunc = keyFunc &&
23
- ((value) => {
24
+ ((value, reactive = true) => {
24
25
  const key = keyFunc(value);
25
26
  if (key === undefined)
26
27
  return undefined;
@@ -28,7 +29,7 @@ class Store {
28
29
  if (rootObject === undefined)
29
30
  throw `No root object found for key: ${key}`;
30
31
  const rootValue = observableNode_1.ObservableNode.Unwrap(rootObject); // rootObject[GET_OBSERVABLE_VALUE];
31
- observableNode_1.ObservableNode.Touch(rootValue, key);
32
+ reactive && observableNode_1.ObservableNode.Touch(rootValue, key);
32
33
  const alias = rootValue[key];
33
34
  return alias;
34
35
  });
@@ -72,7 +73,8 @@ class Store {
72
73
  if (rootObject === undefined) {
73
74
  if (results.length > 1 || results[0].path.length > 1)
74
75
  throw `Unable to initialize root path ${rootPath} with ${results.length} results and initial path ${results[0].path}`;
75
- const newRootObject = this.createNode({ [rootPath]: (0, json_1.JsonDeepClone)(results[0].value) });
76
+ const rootData = this.cloneData ? (0, json_1.JsonDeepClone)(results[0].value) : results[0].value;
77
+ const newRootObject = this.createNode({ [rootPath]: rootData });
76
78
  this.rootMap.set(rootPath, newRootObject);
77
79
  return;
78
80
  }
@@ -4,7 +4,6 @@ exports.StoreAsync = void 0;
4
4
  const asyncQueue_1 = require("../../Utils/asyncQueue");
5
5
  const json_1 = require("../../Utils/json");
6
6
  const diffAsync_1 = require("../Diff/diffAsync");
7
- const observableNode_1 = require("../Tree/observableNode");
8
7
  const store_1 = require("./store");
9
8
  /**
10
9
  * StoreAsync class extends the base Store class to provide asynchronous data management operations.
@@ -44,7 +43,7 @@ class StoreAsync extends store_1.Store {
44
43
  * @param keyFunc Optional function to generate a key for a given data value.
45
44
  */
46
45
  constructor(keyFunc) {
47
- super(keyFunc);
46
+ super(keyFunc, false);
48
47
  /**
49
48
  * The async queue instance used to manage asynchronous operations in a non-blocking manner.
50
49
  * @private
@@ -124,12 +123,29 @@ class StoreAsync extends store_1.Store {
124
123
  async Splice(key, start, deleteCount, ...items) {
125
124
  return await this.queue.Next(async () => {
126
125
  const arr = this.Get(key);
127
- const arrValue = observableNode_1.ObservableNode.Unwrap(arr); // (arr as any)[GET_OBSERVABLE_VALUE] as any[];
128
- const arrCopy = arrValue.slice();
129
- const spliceResult = (0, json_1.JsonDeepClone)(arrCopy.splice(start, deleteCount, ...items));
130
- const diffResult = await this.diff.DiffPath(key, arrCopy);
126
+ const arrNodes = arr.slice(start); // Array starting from the splice start index
127
+ const spliceResult = arrNodes.splice(0, deleteCount ?? arrNodes.length, ...items);
128
+ const diffBatch = new Array(arrNodes.length + 1);
129
+ for (let x = 0; x < arrNodes.length; x++)
130
+ diffBatch[x] = { path: `${key}.${x + start}`, value: arrNodes[x]?.toJSON?.() ?? arrNodes[x] };
131
+ diffBatch[diffBatch.length - 1] = { path: `${key}.length`, value: start + arrNodes.length };
132
+ const diffResult = await this.diff.DiffBatch(diffBatch);
131
133
  this.UpdateRootMap(diffResult);
132
134
  return spliceResult;
135
+ /* const arr = this.Get(key) as any[];
136
+ const arrNodes = arr.slice(start); // Array starting from the splice start index
137
+ const spliceResult = arrNodes.splice(0, deleteCount, ...items);
138
+
139
+ const diffBatch: { path: string, value: any }[] = new Array(arrNodes.length + 1);
140
+ for (let x = 0; x < arrNodes.length; x++)
141
+ diffBatch[x] = { path: `${key}.${x + start}`, value: arrNodes[x]?.toJSON?.() };
142
+
143
+ diffBatch[diffBatch.length - 1] = { path: `${key}.length`, value: arrNodes.length + start + items.length };
144
+
145
+ const diffResult = await this.diff.DiffBatch(diffBatch);
146
+ this.UpdateRootMap(diffResult);
147
+
148
+ return spliceResult; */
133
149
  });
134
150
  }
135
151
  /**
@@ -98,10 +98,13 @@ class StoreSync extends store_1.Store {
98
98
  */
99
99
  Splice(key, start, deleteCount, ...items) {
100
100
  const arr = this.Get(key);
101
- const arrValue = observableNode_1.ObservableNode.Unwrap(arr); // (arr as any)[GET_OBSERVABLE_VALUE] as any[];
102
- const arrCopy = arrValue.slice();
103
- const spliceResult = (0, json_1.JsonDeepClone)(arrCopy.splice(start, deleteCount, ...items));
104
- const diffResult = this.diff.DiffPath(key, arrCopy);
101
+ const arrNodes = arr.slice(start); // Array starting from the splice start index
102
+ const spliceResult = arrNodes.splice(0, deleteCount ?? arrNodes.length, ...items);
103
+ const diffBatch = new Array(arrNodes.length + 1);
104
+ for (let x = 0; x < arrNodes.length; x++)
105
+ diffBatch[x] = { path: `${key}.${x + start}`, value: arrNodes[x]?.toJSON?.() ?? arrNodes[x] };
106
+ diffBatch[diffBatch.length - 1] = { path: `${key}.length`, value: start + arrNodes.length };
107
+ const diffResult = this.diff.DiffBatch(diffBatch);
105
108
  this.UpdateRootMap(diffResult);
106
109
  return spliceResult;
107
110
  }
@@ -74,7 +74,7 @@ export declare namespace ObservableNode {
74
74
  * @param rootNode The observable node to apply the diff to.
75
75
  * @param diffResult The diff result from JsonDiff containing path-value pairs of changes.
76
76
  */
77
- function ApplyDiff(rootNode: any, diffResult: JsonDiffResult): void;
77
+ function ApplyDiff(rootNode: any, diffResult: JsonDiffResult, cloneData?: boolean): void;
78
78
  /**
79
79
  * Creates a factory function for making values observable with optional aliasing.
80
80
  * The alias function transforms values before creating the observable proxy,
@@ -82,5 +82,5 @@ export declare namespace ObservableNode {
82
82
  * @param alias Optional function to transform values before making them observable.
83
83
  * @returns A function that creates observable nodes from plain values.
84
84
  */
85
- function CreateFactory(alias?: (value: any) => any | undefined): <T>(value: T) => T;
85
+ function CreateFactory(alias?: (value: any, reactive?: boolean) => any | undefined): <T>(value: T) => T;
86
86
  }
@@ -99,13 +99,13 @@ function CreateProxyFactory(alias) {
99
99
  switch (type) {
100
100
  case "array": {
101
101
  const typedValue = value;
102
- const proxy = CreateProxy(typedValue);
103
- return proxy.map(ToJsonCopy);
102
+ const arrayValue = alias(typedValue, false) ?? typedValue;
103
+ return arrayValue.map(ToJsonCopy);
104
104
  }
105
105
  case "object": {
106
106
  const typedValue = alias(value) ?? value;
107
- const proxy = CreateProxy(typedValue);
108
- const keys = Object.keys(proxy);
107
+ const objectValue = alias(typedValue, false) ?? typedValue;
108
+ const keys = Object.keys(objectValue);
109
109
  const copy = {};
110
110
  for (let x = 0; x < keys.length; x++)
111
111
  copy[keys[x]] = ToJsonCopy(typedValue[keys[x]]);
@@ -116,7 +116,7 @@ function CreateProxyFactory(alias) {
116
116
  }
117
117
  }
118
118
  function ToJsonDefault(value) {
119
- return value;
119
+ return (0, json_1.JsonDeepClone)(value);
120
120
  }
121
121
  const ToJson = alias !== undefined ? ToJsonCopy : ToJsonDefault;
122
122
  const readOnly = alias !== undefined;
@@ -415,11 +415,11 @@ var ObservableNode;
415
415
  * @param rootNode The observable node to apply the diff to.
416
416
  * @param diffResult The diff result from JsonDiff containing path-value pairs of changes.
417
417
  */
418
- function ApplyDiff(rootNode, diffResult) {
418
+ function ApplyDiff(rootNode, diffResult, cloneData = true) {
419
419
  const root = rootNode[NODE_VALUE];
420
420
  if (diffResult.length === 1 && diffResult[0].path.length === 0) {
421
421
  // Replacing rootNode
422
- const rootPatch = (0, json_1.JsonDeepClone)(diffResult[0].value);
422
+ const rootPatch = cloneData ? (0, json_1.JsonDeepClone)(diffResult[0].value) : diffResult[0].value;
423
423
  const rootType = (0, json_2.JsonType)(root);
424
424
  const rootPatchType = (0, json_2.JsonType)(rootPatch);
425
425
  if (rootType !== rootPatchType)
@@ -465,9 +465,8 @@ var ObservableNode;
465
465
  }
466
466
  }
467
467
  const assignValue = pathTuples[y][1];
468
- // assignValue[path[y]] = JsonDeepClone(value);
469
- // ObservableNode.Update(assignValue, path[y]);
470
- ObservableNode.Assign(assignValue, path[y], (0, json_1.JsonDeepClone)(value));
468
+ const assignData = cloneData ? (0, json_1.JsonDeepClone)(value) : value;
469
+ ObservableNode.Assign(assignValue, path[y], assignData);
471
470
  }
472
471
  }
473
472
  ObservableNode.ApplyDiff = ApplyDiff;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "8.0.6",
3
+ "version": "8.0.8",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",