j-templates 7.0.65 → 7.0.67

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.
@@ -6,12 +6,59 @@ const json_1 = require("../../Utils/json");
6
6
  const diffAsync_1 = require("../Diff/diffAsync");
7
7
  const observableNode_1 = require("../Tree/observableNode");
8
8
  const store_1 = require("./store");
9
+ /**
10
+ * StoreAsync class extends the base Store class to provide asynchronous data management operations.
11
+ * This class handles writing, patching, pushing, and splicing data in an asynchronous manner.
12
+ *
13
+ * StoreAsync is designed to work with observable data structures, allowing for efficient updates
14
+ * and notifications when data changes. It is particularly useful for scenarios where asynchronous
15
+ * operations are preferred or required, such as handling large datasets or performing complex diffs
16
+ * without blocking the main thread.
17
+ *
18
+ * @example
19
+ * // Creating a StoreAsync instance
20
+ * const store = new StoreAsync();
21
+ *
22
+ * // Writing data to the store asynchronously
23
+ * await store.Write({ name: "John", age: 30 }, "user");
24
+ *
25
+ * // Patching existing data asynchronously
26
+ * await store.Patch("user", { age: 31 });
27
+ *
28
+ * // Pushing data into an array asynchronously
29
+ * await store.Push("user.array", { id: 1 }, { id: 2 });
30
+ *
31
+ * // Splicing an array asynchronously
32
+ * const deletedItems = await store.Splice("user.array", 0, 1, { id: 3 });
33
+ *
34
+ * // Cleaning up resources
35
+ * store.Destroy();
36
+ *
37
+ * @see Store
38
+ * @see StoreSync
39
+ * @see DiffAsync
40
+ */
9
41
  class StoreAsync extends store_1.Store {
42
+ /**
43
+ * Creates an instance of StoreAsync.
44
+ * @param keyFunc Optional function to generate a key for a given data value.
45
+ */
10
46
  constructor(keyFunc) {
11
47
  super(keyFunc);
48
+ /**
49
+ * The async queue instance used to manage asynchronous operations in a non-blocking manner.
50
+ * @private
51
+ */
12
52
  this.queue = new asyncQueue_1.AsyncQueue();
13
53
  this.diff = new diffAsync_1.DiffAsync(keyFunc);
14
54
  }
55
+ /**
56
+ * Writes data to the store asynchronously.
57
+ * This method ensures that write operations are queued and executed in a non-blocking manner.
58
+ * @param data The data to be written. Can be of any type.
59
+ * @param key Optional key for the data. If not provided, the keyFunc will be used to generate a key.
60
+ * @throws Will throw an error if no key is provided for the data.
61
+ */
15
62
  async Write(data, key) {
16
63
  await this.queue.Next(async () => {
17
64
  key = key || this.keyFunc?.(data);
@@ -21,6 +68,13 @@ class StoreAsync extends store_1.Store {
21
68
  this.UpdateRootMap(diffResult);
22
69
  });
23
70
  }
71
+ /**
72
+ * Patches an existing value in the store with new data asynchronously.
73
+ * This method ensures that patch operations are queued and executed in a non-blocking manner.
74
+ * @param key The key of the value to be patched.
75
+ * @param patch The patch data to be merged with the existing value.
76
+ * @throws Will throw an error if the value to be patched is undefined.
77
+ */
24
78
  async Patch(key, patch) {
25
79
  await this.queue.Next(async () => {
26
80
  const value = this.Get(key);
@@ -32,6 +86,12 @@ class StoreAsync extends store_1.Store {
32
86
  this.UpdateRootMap(diffResult);
33
87
  });
34
88
  }
89
+ /**
90
+ * Pushes data into an array stored at the specified key asynchronously.
91
+ * This method ensures that push operations are queued and executed in a non-blocking manner.
92
+ * @param key The key of the array where data will be pushed.
93
+ * @param data The data items to be pushed into the array.
94
+ */
35
95
  async Push(key, ...data) {
36
96
  await this.queue.Next(async () => {
37
97
  const arr = this.Get(key);
@@ -45,6 +105,16 @@ class StoreAsync extends store_1.Store {
45
105
  this.UpdateRootMap(diffResult);
46
106
  });
47
107
  }
108
+ /**
109
+ * Splices an array stored at the specified key with new data asynchronously.
110
+ * This method ensures that splice operations are queued and executed in a non-blocking manner.
111
+ * It modifies the array by deleting elements and inserting new ones at the specified position.
112
+ * @param key The key of the array to be spliced.
113
+ * @param start The position at which to start changing the array.
114
+ * @param deleteCount Optional number of elements to delete. If not provided, all elements from start to end will be deleted.
115
+ * @param items Optional elements to insert into the array.
116
+ * @returns The array of deleted elements.
117
+ */
48
118
  async Splice(key, start, deleteCount, ...items) {
49
119
  return await this.queue.Next(async () => {
50
120
  const arr = this.Get(key);
@@ -56,6 +126,10 @@ class StoreAsync extends store_1.Store {
56
126
  return spliceResult;
57
127
  });
58
128
  }
129
+ /**
130
+ * Destroys the StoreAsync instance, stopping the async queue and cleaning up the diff instance.
131
+ * This method should be called when the instance is no longer needed to free up resources.
132
+ */
59
133
  Destroy() {
60
134
  this.queue.Stop();
61
135
  this.diff.Destroy();
@@ -1,9 +1,71 @@
1
1
  import { Store } from "./store";
2
+ /**
3
+ * StoreSync class extends the base Store class to provide synchronous data management operations.
4
+ * This class handles writing, patching, pushing, and splicing data in a synchronous manner.
5
+ *
6
+ * StoreSync is designed to work with observable data structures, allowing for efficient updates
7
+ * and notifications when data changes. It is particularly useful for scenarios where synchronous
8
+ * operations are preferred or required.
9
+ *
10
+ * @example
11
+ * // Creating a StoreSync instance
12
+ * const store = new StoreSync();
13
+ *
14
+ * // Writing data to the store
15
+ * store.Write({ name: "John", age: 30 }, "user");
16
+ *
17
+ * // Patching existing data
18
+ * store.Patch("user", { age: 31 });
19
+ *
20
+ * // Pushing data into an array
21
+ * store.Push("user.array", { id: 1 }, { id: 2 });
22
+ *
23
+ * // Splicing an array
24
+ * const deletedItems = store.Splice("user.array", 0, 1, { id: 3 });
25
+ *
26
+ * @see Store
27
+ * @see StoreAsync
28
+ * @see DiffSync
29
+ */
2
30
  export declare class StoreSync extends Store {
31
+ /**
32
+ * The diff instance used to compute differences between current and new data states.
33
+ * @private
34
+ */
3
35
  private diff;
36
+ /**
37
+ * Creates an instance of StoreSync.
38
+ * @param keyFunc Optional function to generate a key for a given data value.
39
+ */
4
40
  constructor(keyFunc?: (value: any) => string | undefined);
41
+ /**
42
+ * Writes data to the store synchronously.
43
+ * @param data The data to be written. Can be of any type.
44
+ * @param key Optional key for the data. If not provided, the keyFunc will be used to generate a key.
45
+ * @throws Will throw an error if no key is provided for the data.
46
+ */
5
47
  Write(data: unknown, key?: string): void;
48
+ /**
49
+ * Patches an existing value in the store with new data synchronously.
50
+ * @param key The key of the value to be patched.
51
+ * @param patch The patch data to be merged with the existing value.
52
+ * @throws Will throw an error if the value to be patched is undefined.
53
+ */
6
54
  Patch(key: string, patch: unknown): void;
55
+ /**
56
+ * Pushes data into an array stored at the specified key synchronously.
57
+ * @param key The key of the array where data will be pushed.
58
+ * @param data The data items to be pushed into the array.
59
+ */
7
60
  Push(key: string, ...data: unknown[]): void;
61
+ /**
62
+ * Splices an array stored at the specified key with new data synchronously.
63
+ * This method modifies the array by deleting elements and inserting new ones at the specified position.
64
+ * @param key The key of the array to be spliced.
65
+ * @param start The position at which to start changing the array.
66
+ * @param deleteCount Optional number of elements to delete. If not provided, all elements from start to end will be deleted.
67
+ * @param items Optional elements to insert into the array.
68
+ * @returns The array of deleted elements.
69
+ */
8
70
  Splice(key: string, start: number, deleteCount?: number, ...items: unknown[]): any[];
9
71
  }
@@ -5,11 +5,49 @@ const json_1 = require("../../Utils/json");
5
5
  const diffSync_1 = require("../Diff/diffSync");
6
6
  const observableNode_1 = require("../Tree/observableNode");
7
7
  const store_1 = require("./store");
8
+ /**
9
+ * StoreSync class extends the base Store class to provide synchronous data management operations.
10
+ * This class handles writing, patching, pushing, and splicing data in a synchronous manner.
11
+ *
12
+ * StoreSync is designed to work with observable data structures, allowing for efficient updates
13
+ * and notifications when data changes. It is particularly useful for scenarios where synchronous
14
+ * operations are preferred or required.
15
+ *
16
+ * @example
17
+ * // Creating a StoreSync instance
18
+ * const store = new StoreSync();
19
+ *
20
+ * // Writing data to the store
21
+ * store.Write({ name: "John", age: 30 }, "user");
22
+ *
23
+ * // Patching existing data
24
+ * store.Patch("user", { age: 31 });
25
+ *
26
+ * // Pushing data into an array
27
+ * store.Push("user.array", { id: 1 }, { id: 2 });
28
+ *
29
+ * // Splicing an array
30
+ * const deletedItems = store.Splice("user.array", 0, 1, { id: 3 });
31
+ *
32
+ * @see Store
33
+ * @see StoreAsync
34
+ * @see DiffSync
35
+ */
8
36
  class StoreSync extends store_1.Store {
37
+ /**
38
+ * Creates an instance of StoreSync.
39
+ * @param keyFunc Optional function to generate a key for a given data value.
40
+ */
9
41
  constructor(keyFunc) {
10
42
  super(keyFunc);
11
43
  this.diff = new diffSync_1.DiffSync(keyFunc);
12
44
  }
45
+ /**
46
+ * Writes data to the store synchronously.
47
+ * @param data The data to be written. Can be of any type.
48
+ * @param key Optional key for the data. If not provided, the keyFunc will be used to generate a key.
49
+ * @throws Will throw an error if no key is provided for the data.
50
+ */
13
51
  Write(data, key) {
14
52
  data = (0, json_1.JsonDeepClone)(data);
15
53
  key = key || this.keyFunc?.(data);
@@ -18,6 +56,12 @@ class StoreSync extends store_1.Store {
18
56
  const diffResult = this.diff.DiffPath(key, data);
19
57
  this.UpdateRootMap(diffResult);
20
58
  }
59
+ /**
60
+ * Patches an existing value in the store with new data synchronously.
61
+ * @param key The key of the value to be patched.
62
+ * @param patch The patch data to be merged with the existing value.
63
+ * @throws Will throw an error if the value to be patched is undefined.
64
+ */
21
65
  Patch(key, patch) {
22
66
  const value = this.Get(key);
23
67
  if (value === undefined)
@@ -27,6 +71,11 @@ class StoreSync extends store_1.Store {
27
71
  const diffResult = this.diff.DiffPath(key, mergedJson);
28
72
  this.UpdateRootMap(diffResult);
29
73
  }
74
+ /**
75
+ * Pushes data into an array stored at the specified key synchronously.
76
+ * @param key The key of the array where data will be pushed.
77
+ * @param data The data items to be pushed into the array.
78
+ */
30
79
  Push(key, ...data) {
31
80
  const arr = this.Get(key);
32
81
  const batch = data.map(function (d, i) {
@@ -38,6 +87,15 @@ class StoreSync extends store_1.Store {
38
87
  const diffResult = this.diff.DiffBatch(batch);
39
88
  this.UpdateRootMap(diffResult);
40
89
  }
90
+ /**
91
+ * Splices an array stored at the specified key with new data synchronously.
92
+ * This method modifies the array by deleting elements and inserting new ones at the specified position.
93
+ * @param key The key of the array to be spliced.
94
+ * @param start The position at which to start changing the array.
95
+ * @param deleteCount Optional number of elements to delete. If not provided, all elements from start to end will be deleted.
96
+ * @param items Optional elements to insert into the array.
97
+ * @returns The array of deleted elements.
98
+ */
41
99
  Splice(key, start, deleteCount, ...items) {
42
100
  const arr = this.Get(key);
43
101
  const arrValue = arr[observableNode_1.GET_OBSERVABLE_VALUE];
@@ -4,6 +4,7 @@ export declare const GET_OBSERVABLE_VALUE = "____getObservableValue";
4
4
  export declare const GET_TO_JSON = "toJSON";
5
5
  export declare namespace ObservableNode {
6
6
  function BypassProxy(value: boolean): void;
7
+ function Unwrap<T>(value: T): T;
7
8
  function Create<T>(value: T): T;
8
9
  function Touch(value: unknown, prop?: string | number): void;
9
10
  function ApplyDiff(rootNode: any, diffResult: JsonDiffResult): void;
@@ -87,7 +87,7 @@ function CreateProxyFactory(alias) {
87
87
  const ToJson = alias !== undefined ? ToJsonCopy : ToJsonDefault;
88
88
  const readOnly = alias !== undefined;
89
89
  function CreateArrayProxy(value) {
90
- const scope = observableScope_1.ObservableScope.Create(() => value);
90
+ const scope = observableScope_1.ObservableScope.Create(() => value, false, true);
91
91
  const proxy = new Proxy(value, {
92
92
  get: ArrayProxyGetter,
93
93
  set: ArrayProxySetter,
@@ -235,7 +235,7 @@ function CreateProxyFactory(alias) {
235
235
  leafScopes[prop] ??= observableScope_1.ObservableScope.Create(function () {
236
236
  const value = parent[prop];
237
237
  return CreateProxyFromValue(value);
238
- });
238
+ }, false, true);
239
239
  return observableScope_1.ObservableScope.Value(leafScopes[prop]);
240
240
  }
241
241
  function CreateProxyFromValue(value) {
@@ -268,6 +268,10 @@ var ObservableNode;
268
268
  bypassProxy = value;
269
269
  }
270
270
  ObservableNode.BypassProxy = BypassProxy;
271
+ function Unwrap(value) {
272
+ return UnwrapProxy(value);
273
+ }
274
+ ObservableNode.Unwrap = Unwrap;
271
275
  function Create(value) {
272
276
  return DefaultCreateProxy(value);
273
277
  }
@@ -1,64 +1,39 @@
1
1
  import { Emitter, EmitterCallback } from "../../Utils/emitter";
2
- import { IDestroyable } from "../../Utils/utils.types";
3
- export declare class ObservableScopeValue<T> {
4
- protected scope: IObservableScope<T>;
5
- get Value(): T;
6
- constructor(scope: IObservableScope<T>);
7
- }
8
- export declare class ObservableScopeWrapper<T> extends ObservableScopeValue<T> implements IDestroyable {
9
- private scopeEmitter;
10
- constructor(scope: IObservableScope<T>);
11
- Scope<O>(callback: {
12
- (parent: T): O;
13
- }): ObservableScope<O>;
14
- Watch(callback: {
15
- (scope: ObservableScopeValue<T>): void;
16
- }): void;
17
- Unwatch(callback: {
18
- (scope: ObservableScopeValue<T>): void;
19
- }): void;
20
- Destroy(): void;
21
- }
22
- export declare class ObservableScope<T> extends ObservableScopeWrapper<T> {
23
- constructor(getFunction: {
24
- (): T | Promise<T>;
25
- });
2
+ interface IStaticObservableScope<T> {
3
+ type: "static";
4
+ value: T;
26
5
  }
27
- export interface IObservableScope<T> extends IDestroyable {
28
- getFunction: {
29
- (): T;
30
- };
31
- setCallback: EmitterCallback;
6
+ interface IDynamicObservableScope<T> {
7
+ type: "dynamic";
32
8
  async: boolean;
33
- value: T;
34
- promise: Promise<T> | null;
9
+ greedy: boolean;
35
10
  dirty: boolean;
11
+ destroyed: boolean;
12
+ getFunction: () => Promise<T> | T;
13
+ setCallback: EmitterCallback;
14
+ value: T;
36
15
  emitter: Emitter;
37
16
  emitters: (Emitter | null)[];
17
+ onDestroyed: Emitter | null;
38
18
  calcScopes: {
39
19
  [id: string]: IObservableScope<unknown> | null;
40
20
  } | null;
41
- calc: boolean;
42
- onDestroyed: Emitter | null;
43
- destroyed: boolean;
44
21
  }
45
- export declare function CalcScope<T>(callback: () => T, idOverride?: string): unknown;
22
+ export type IObservableScope<T> = IStaticObservableScope<T> | IDynamicObservableScope<T>;
23
+ export declare function CalcScope<T>(callback: () => T, idOverride?: string): T;
46
24
  export declare namespace ObservableScope {
47
25
  function Create<T>(valueFunction: {
48
26
  (): T | Promise<T>;
49
- }, calc?: boolean): IObservableScope<T>;
50
- function CreateIf<T>(valueFunction: {
51
- (): T | Promise<T>;
52
- }): [T, IObservableScope<T> | null];
27
+ }, greedy?: boolean, force?: boolean): IObservableScope<T>;
53
28
  function Register(emitter: Emitter): void;
54
- function Init<T>(scope: IObservableScope<T>): void;
55
29
  function Peek<T>(scope: IObservableScope<T>): T;
56
30
  function Value<T>(scope: IObservableScope<T>): T;
57
31
  function Touch<T>(scope: IObservableScope<T>): void;
58
32
  function Watch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T>]>): void;
59
- function Unwatch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T> | ObservableScopeValue<T>]>): void;
33
+ function Unwatch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T>]>): void;
60
34
  function OnDestroyed(scope: IObservableScope<unknown>, callback: EmitterCallback): void;
61
35
  function Update(scope: IObservableScope<any>): void;
62
36
  function Destroy<T>(scope: IObservableScope<T>): void;
63
37
  function DestroyAll(scopes: IObservableScope<unknown>[]): void;
64
38
  }
39
+ export {};