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.
package/Utils/array.d.ts CHANGED
@@ -1,5 +1,53 @@
1
+ /**
2
+ * Removes all null values from an array starting from a specified index.
3
+ * This function modifies the array in-place by shifting non-null elements
4
+ * to fill the gaps left by removed null values, then truncating the array.
5
+ *
6
+ * @param array - The array from which to remove null values. Can contain mixed types including null.
7
+ * @param startIndex - The index to start removing null values from (default: 0).
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const arr = [1, null, 2, null, 3, null, 4];
12
+ * RemoveNulls(arr); // Removes all null values, result: [1, 2, 3, 4]
13
+ *
14
+ * const arr2 = [null, null, 1, null, 2];
15
+ * RemoveNulls(arr2, 2); // Starts from index 2, result: [null, null, 1, 2]
16
+ * ```
17
+ */
1
18
  export declare function RemoveNulls(array: (unknown | null)[], startIndex?: number): void;
2
19
  export declare function ArrayDiff(source: any[], target: any[]): boolean;
20
+ /**
21
+ * Reconciles two sorted arrays by applying add and remove operations to transition
22
+ * from the left array to the right array. Elements are compared based on their first
23
+ * element (index 0) which must be a number.
24
+ *
25
+ * This function efficiently handles the process of updating a sorted collection by:
26
+ * - Removing elements that exist in the left array but not in the right array
27
+ * - Adding elements that exist in the right array but not in the left array
28
+ *
29
+ * @template T - A tuple type where the first element is a number (T extends [number])
30
+ * @param left - The initial sorted array of tuples
31
+ * @param right - The target sorted array of tuples
32
+ * @param add - Callback function to handle adding elements
33
+ * @param remove - Callback function to handle removing elements
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const left = [[1, 'a'], [3, 'c']];
38
+ * const right = [[2, 'b'], [3, 'c'], [4, 'd']];
39
+ * const added: number[] = [];
40
+ * const removed: number[] = [];
41
+ *
42
+ * ReconcileSortedEmitters(
43
+ * left,
44
+ * right,
45
+ * (value) => added.push(value[0]),
46
+ * (value) => removed.push(value[0])
47
+ * );
48
+ * // Adds [2, 4] and removes [1]
49
+ * ```
50
+ */
3
51
  export declare function ReconcileSortedEmitters<T extends [number]>(left: T[], right: T[], add: (value: T) => void, remove: (value: T) => void): void;
4
52
  export declare function InsertionSortTuples<T extends [number, ...any[]]>(arr: T[]): T[];
5
53
  export declare function ReconcileSortedArrays<T>(left: T[], right: T[], add: (value: T) => void, remove: (value: T) => void): void;
package/Utils/array.js CHANGED
@@ -5,6 +5,23 @@ exports.ArrayDiff = ArrayDiff;
5
5
  exports.ReconcileSortedEmitters = ReconcileSortedEmitters;
6
6
  exports.InsertionSortTuples = InsertionSortTuples;
7
7
  exports.ReconcileSortedArrays = ReconcileSortedArrays;
8
+ /**
9
+ * Removes all null values from an array starting from a specified index.
10
+ * This function modifies the array in-place by shifting non-null elements
11
+ * to fill the gaps left by removed null values, then truncating the array.
12
+ *
13
+ * @param array - The array from which to remove null values. Can contain mixed types including null.
14
+ * @param startIndex - The index to start removing null values from (default: 0).
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const arr = [1, null, 2, null, 3, null, 4];
19
+ * RemoveNulls(arr); // Removes all null values, result: [1, 2, 3, 4]
20
+ *
21
+ * const arr2 = [null, null, 1, null, 2];
22
+ * RemoveNulls(arr2, 2); // Starts from index 2, result: [null, null, 1, 2]
23
+ * ```
24
+ */
8
25
  function RemoveNulls(array, startIndex = 0) {
9
26
  let nullIndex = startIndex;
10
27
  for (; nullIndex < array.length && array[nullIndex] !== null; nullIndex++) { }
@@ -27,6 +44,37 @@ function ArrayDiff(source, target) {
27
44
  for (; x < source.length && source[x] === target[x]; x++) { }
28
45
  return x < source.length;
29
46
  }
47
+ /**
48
+ * Reconciles two sorted arrays by applying add and remove operations to transition
49
+ * from the left array to the right array. Elements are compared based on their first
50
+ * element (index 0) which must be a number.
51
+ *
52
+ * This function efficiently handles the process of updating a sorted collection by:
53
+ * - Removing elements that exist in the left array but not in the right array
54
+ * - Adding elements that exist in the right array but not in the left array
55
+ *
56
+ * @template T - A tuple type where the first element is a number (T extends [number])
57
+ * @param left - The initial sorted array of tuples
58
+ * @param right - The target sorted array of tuples
59
+ * @param add - Callback function to handle adding elements
60
+ * @param remove - Callback function to handle removing elements
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const left = [[1, 'a'], [3, 'c']];
65
+ * const right = [[2, 'b'], [3, 'c'], [4, 'd']];
66
+ * const added: number[] = [];
67
+ * const removed: number[] = [];
68
+ *
69
+ * ReconcileSortedEmitters(
70
+ * left,
71
+ * right,
72
+ * (value) => added.push(value[0]),
73
+ * (value) => removed.push(value[0])
74
+ * );
75
+ * // Adds [2, 4] and removes [1]
76
+ * ```
77
+ */
30
78
  function ReconcileSortedEmitters(left, right, add, remove) {
31
79
  let leftIndex = 0;
32
80
  let rightIndex = 0;
@@ -53,13 +101,21 @@ function ReconcileSortedEmitters(left, right, add, remove) {
53
101
  }
54
102
  function InsertionSortTuples(arr) {
55
103
  const n = arr.length;
104
+ // Start from the second element (index 1) as the first element (index 0)
105
+ // is trivially sorted by itself.
56
106
  for (let i = 1; i < n; i++) {
107
+ // Pick up the current element to be inserted
57
108
  const currentItem = arr[i];
109
+ // This is the index of the last element in the sorted sub-array
58
110
  let j = i - 1;
111
+ // Move elements of the sorted sub-array that are greater than currentItem,
112
+ // to one position ahead of their current position.
113
+ // We compare using the first element of the tuples (currentItem[0]).
59
114
  while (j >= 0 && arr[j][0] > currentItem[0]) {
60
115
  arr[j + 1] = arr[j];
61
116
  j--;
62
117
  }
118
+ // Place the currentItem in its correct position in the sorted sub-array
63
119
  arr[j + 1] = currentItem;
64
120
  }
65
121
  return arr;
@@ -1,17 +1,152 @@
1
+ /**
2
+ * This module provides decorators for managing state, computed values, and dependency injection in components.
3
+ * These decorators are designed to work with observable data structures, allowing for efficient updates and notifications.
4
+ *
5
+ * @module Utils/Decorators
6
+ * @see ObservableScope
7
+ * @see ObservableNode
8
+ * @see StoreAsync
9
+ * @see StoreSync
10
+ * @see Component
11
+ */
1
12
  import { IDestroyable } from "./utils.types";
2
- import { Component } from "../Node/component";
13
+ import { Injector } from "./injector";
14
+ /**
15
+ * Computed decorator factory for creating synchronous computed properties.
16
+ * A computed property is derived from other properties and automatically updates when its dependencies change.
17
+ *
18
+ * @example
19
+ * class MyComponent extends Component {
20
+ * @Computed({ count: 0 })
21
+ * get myComputed(): { count: number } {
22
+ * return { count: this.myStateValue };
23
+ * }
24
+ * }
25
+ *
26
+ * @param defaultValue The default value to be used if the computed property is not defined.
27
+ * @returns A property decorator that can be applied to a getter method.
28
+ * @throws Will throw an error if the property is not a getter or if it has a setter.
29
+ * @remarks The computed value is backed by a StoreSync instance, which caches the latest result of the getter. When any of the getter's dependencies change, the StoreSync is updated and the computed property reflects the new value synchronously. The provided `defaultValue` is returned until the first evaluation completes.
30
+ */
3
31
  export declare function Computed<T extends WeakKey, K extends keyof T, V extends T[K]>(defaultValue: V): (target: T, propertyKey: K, descriptor: PropertyDescriptor) => PropertyDescriptor;
32
+ /**
33
+ * ComputedAsync decorator factory for creating asynchronous computed properties.
34
+ * A computed property is derived from other properties and automatically updates when its dependencies change.
35
+ *
36
+ * @example
37
+ * class MyComponent extends Component {
38
+ * @ComputedAsync({ items: [] })
39
+ * get myAsyncComputed(): { items: any[] } {
40
+ * return { items: this.myStateValue };
41
+ * }
42
+ * }
43
+ *
44
+ * @param defaultValue The default value to be used if the computed property is not defined.
45
+ * @returns A property decorator that can be applied to a getter method.
46
+ * @throws Will throw an error if the property is not a getter or if it has a setter.
47
+ * @remarks The computed value is backed by a StoreAsync instance, which caches the result of the asynchronous getter. When dependencies change, the StoreAsync updates and the computed property resolves to the latest value. The provided `defaultValue` is returned until the async computation completes.
48
+ */
4
49
  export declare function ComputedAsync<T extends WeakKey, K extends keyof T, V extends T[K]>(defaultValue: V): (target: T, propertyKey: K, descriptor: PropertyDescriptor) => PropertyDescriptor;
50
+ /**
51
+ * State decorator factory for creating state properties.
52
+ * A state property is a reactive value that can be read and written synchronously.
53
+ *
54
+ * @example
55
+ * class MyComponent extends Component {
56
+ * @State()
57
+ * myState: { count: number } = { count: 0 };
58
+ * }
59
+ *
60
+ * @returns A property decorator that can be applied to a property.
61
+ * @remarks The State decorator creates an ObservableNode to store the value. Updates to the property automatically notify any watchers of dependent scopes, making it ideal for mutable complex data structures that need to trigger reactivity.
62
+ */
5
63
  export declare function State(): any;
64
+ /**
65
+ * Value decorator factory for creating value properties.
66
+ * A value property is a reactive value that can be read and written synchronously.
67
+ *
68
+ * @example
69
+ * class MyComponent extends Component {
70
+ * @Value()
71
+ * myValue: string = "Hello";
72
+ * }
73
+ *
74
+ * @returns A property decorator that can be applied to a property.
75
+ * @remarks The Value decorator lazily creates a scoped ObservableScope whose getter returns the stored value. The setter updates the stored value and invokes ObservableScope.Update on the scope, causing any dependent computed or scope decorators to re-evaluate.
76
+ */
6
77
  export declare function Value(): any;
78
+ /**
79
+ * Scope decorator factory for creating scope properties.
80
+ * A scope property is a reactive value that can be read and written synchronously.
81
+ *
82
+ * @example
83
+ * class MyComponent extends Component {
84
+ * @Scope()
85
+ * get myScopeValue(): string {
86
+ * return this.myStateValue + "!";
87
+ * }
88
+ * }
89
+ *
90
+ * @returns A property decorator that can be applied to a getter method.
91
+ * @throws Will throw an error if the property is not a getter or if it has a setter.
92
+ */
7
93
  export declare function Scope(): typeof ScopeDecorator;
94
+ /**
95
+ * Scope decorator implementation for creating scope properties.
96
+ * @private
97
+ * @param target The target object.
98
+ * @param propertyKey The property key.
99
+ * @param descriptor The property descriptor.
100
+ * @returns A property descriptor that replaces the original descriptor with a scope implementation.
101
+ * @throws Will throw an error if the property is not a getter or if it has a setter.
102
+ */
8
103
  declare function ScopeDecorator<T, K extends string>(target: T, propertyKey: K, descriptor: PropertyDescriptor): PropertyDescriptor;
9
- export declare function Inject<I, T extends Component<any, any, any>>(type: {
10
- new (...args: Array<any>): I;
11
- }): (target: T, propertyKey: string, descriptor?: PropertyDescriptor) => void;
104
+ export declare function Watch<S extends (instance: T) => any, T extends Record<K, (value: ReturnType<S>) => any>, K extends string>(scope: S): (target: T, propertyKey: K, descriptor: PropertyDescriptor) => void;
105
+ type ConstructorToken<I> = {
106
+ new (...args: any[]): I;
107
+ } | (abstract new (...args: any[]) => I);
108
+ /**
109
+ * Inject decorator factory for creating dependency-injected properties.
110
+ * An injected property is automatically provided by the framework's dependency injection system.
111
+ *
112
+ * @example
113
+ * class MyComponent extends Component {
114
+ * @Inject(Token)
115
+ * property: ServiceForToken;
116
+ * }
117
+ *
118
+ * // Setting the value in the class definition
119
+ * class MyComponent extends Component {
120
+ * @Inject(Token)
121
+ * property: ServiceForToken = new ServiceForToken();
122
+ * }
123
+ *
124
+ * @param type The constructor type of the dependency to be injected.
125
+ * @returns A property decorator that can be applied to a property.
126
+ */
127
+ export declare function Inject<I, T extends Record<K, I> & {
128
+ Injector: Injector;
129
+ }, K extends string>(type: ConstructorToken<I>): (target: T, propertyKey: K, descriptor?: PropertyDescriptor) => void;
130
+ /**
131
+ * Destroy decorator factory for marking a property to be destroyed when the component is destroyed.
132
+ * @example
133
+ * class MyComponent extends Component {
134
+ * @Destroy()
135
+ * timer: Timer;
136
+ * }
137
+ */
12
138
  export declare function Destroy(): typeof DestroyDecorator;
139
+ export declare namespace Bound {
140
+ function All<T extends WeakKey>(value: T): void;
141
+ }
142
+ /**
143
+ * Utility to destroy all observable scopes and invoke destroy on marked properties of an instance.
144
+ * @param value The instance to clean up.
145
+ * @example
146
+ * Destroy.All(this);
147
+ */
13
148
  export declare namespace Destroy {
14
- function All<T extends WeakKey, K>(value: T): void;
149
+ function All<T extends WeakKey>(value: T): void;
15
150
  }
16
151
  declare function DestroyDecorator<T extends Record<K, IDestroyable>, K extends string>(target: T, propertyKey: K): any;
17
152
  export {};