@adaas/a-concept 0.1.29 → 0.1.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaas/a-concept",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "A-Concept is a framework to build new Applications within or outside the ADAAS ecosystem. This framework is designed to be modular structure regardless environment and program goal.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.cjs",
@@ -1,62 +1,317 @@
1
+ import { A_TYPES__DeepPartial } from "@adaas/a-concept/types/A_Common.types";
1
2
  import { A_Meta } from "../A-Meta/A-Meta.class";
2
- import { A_TYPES__Fragment_Init } from "./A-Fragment.types";
3
-
3
+ import { A_TYPES__Fragment_Init, A_TYPES__Fragment_Serialized } from "./A-Fragment.types";
4
4
 
5
5
 
6
+ /**
7
+ * A_Fragment is a core architectural component that represents a singleton execution context
8
+ * within the A-Concept framework. It serves as a shared memory container that can be passed
9
+ * between Components, Entities, and Commands throughout the application pipeline.
10
+ *
11
+ * Key Features:
12
+ * - Singleton pattern: Only one instance per fragment type per scope
13
+ * - Meta storage: Built-in key-value storage for pipeline data
14
+ * - Type-safe: Full TypeScript generics support for meta items and serialization
15
+ * - Serializable: Can be converted to JSON for persistence or transmission
16
+ *
17
+ * @template _MetaItems - Type definition for the meta storage structure
18
+ * @template _SerializedType - Type definition for the serialized output format
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Basic usage with typed meta
23
+ * class UserFragment extends A_Fragment<{ userId: string; role: string }> {
24
+ * constructor() {
25
+ * super({ name: 'UserFragment' });
26
+ * }
27
+ * }
28
+ *
29
+ * // Custom serialization
30
+ * class SessionFragment extends A_Fragment<
31
+ * { sessionId: string; timestamp: number },
32
+ * { name: string; sessionData: string }
33
+ * > {
34
+ * toJSON() {
35
+ * return {
36
+ * name: this.name,
37
+ * sessionData: `${this.get('sessionId')}-${this.get('timestamp')}`
38
+ * };
39
+ * }
40
+ * }
41
+ * ```
42
+ */
6
43
  export class A_Fragment<
7
- _MemoryItems extends Record<string, any> = any
44
+ _MetaItems extends Record<string, any> = any,
45
+ _SerializedType extends A_TYPES__Fragment_Serialized = A_TYPES__Fragment_Serialized & _MetaItems
8
46
  > {
9
47
  /**
10
- * Fragment Name
48
+ * The unique identifier/name for this fragment instance.
49
+ * Used for identification and debugging purposes.
11
50
  */
12
- name: string;
51
+ protected _name: string;
52
+
13
53
  /**
14
- * Memory storage for the Fragment instance
54
+ * Internal meta storage using A_Meta for type-safe key-value operations.
55
+ * This stores all the fragment's runtime data that can be accessed and modified
56
+ * throughout the execution pipeline.
15
57
  */
16
- protected _meta: A_Meta<_MemoryItems> = new A_Meta<_MemoryItems>();
58
+ protected _meta: A_Meta<_MetaItems> = new A_Meta<_MetaItems>();
17
59
 
18
60
 
19
61
  /**
20
- * A-Fragment is a singleton, a piece of execution Context that can be shared between the Components/Entities/Commands
21
- * For every A_Scope can be defined only One A_Fragment of the same type.
22
- * This class is useful for the design purpose and maintainance of the application
62
+ * Creates a new A_Fragment instance.
63
+ *
64
+ * A_Fragment implements the singleton pattern for execution contexts, allowing
65
+ * shared state management across different parts of the application pipeline.
66
+ * Each fragment serves as a memory container that can store typed data and be
67
+ * serialized for persistence or transmission.
23
68
  *
69
+ * Key Benefits:
70
+ * - Centralized state management for related operations
71
+ * - Type-safe meta operations with full IntelliSense support
72
+ * - Serialization support for data persistence
73
+ * - Singleton pattern ensures consistent state within scope
24
74
  *
25
- * [!] Every A_Fragment is a Memory Class that can store data in memory between the steps of the pipeline.
26
- * [!] So if it necessary to store some information in the Execution Context - use memory of the Fragment
75
+ * @param params - Initialization parameters
76
+ * @param params.name - Optional custom name for the fragment (defaults to class name)
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const fragment = new A_Fragment<{ userId: string }>({
81
+ * name: 'UserSessionFragment'
82
+ * });
83
+ * fragment.set('userId', '12345');
84
+ * ```
27
85
  */
28
86
  constructor(params: Partial<A_TYPES__Fragment_Init> = {}) {
29
- /**
30
- * Register the Namespace in the global Namespace provider
31
- */
32
- this.name = params.name || this.constructor.name;
87
+ this._name = params.name || this.constructor.name;
88
+ }
89
+
90
+ /**
91
+ * Gets the fragment's unique name/identifier.
92
+ *
93
+ * @returns The fragment name
94
+ */
95
+ get name(): string {
96
+ return this._name;
33
97
  }
34
98
 
35
99
  /**
36
- * Returns the Meta object that allows to store data in the Fragment memory
100
+ * Gets direct access to the underlying Meta object for advanced meta operations.
37
101
  *
38
- * @returns
102
+ * Use this when you need to perform bulk operations or access Meta-specific methods.
103
+ * For simple get/set operations, prefer using the direct methods on the fragment.
104
+ *
105
+ * @returns The Meta instance containing the fragment's meta
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const fragment = new A_Fragment<{ users: string[], count: number }>();
110
+ *
111
+ * // Advanced operations using meta
112
+ * fragment.meta.setMultiple({
113
+ * users: ['alice', 'bob'],
114
+ * count: 2
115
+ * });
116
+ *
117
+ * // Get all keys
118
+ * const keys = fragment.meta.keys();
119
+ * ```
39
120
  */
40
- get memory(): A_Meta<_MemoryItems> {
121
+ get meta(): A_Meta<_MetaItems> {
41
122
  return this._meta;
42
123
  }
43
124
 
125
+ /**
126
+ * Checks if a specific meta key exists in the fragment.
127
+ *
128
+ * @param param - The key to check for existence
129
+ * @returns True if the key exists, false otherwise
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * if (fragment.has('userId')) {
134
+ * console.log('User ID is set');
135
+ * }
136
+ * ```
137
+ */
138
+ has(param: keyof _MetaItems): boolean {
139
+ return this._meta.has(param);
140
+ }
141
+
142
+ /**
143
+ * Retrieves a value from the fragment's meta.
144
+ *
145
+ * @param param - The key to retrieve
146
+ * @returns The value associated with the key, or undefined if not found
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const userId = fragment.get('userId');
151
+ * if (userId) {
152
+ * console.log(`Current user: ${userId}`);
153
+ * }
154
+ * ```
155
+ */
156
+ get<K extends keyof _MetaItems>(param: K): _MetaItems[K] | undefined {
157
+ return this._meta.get(param);
158
+ }
159
+
160
+ /**
161
+ * Stores a value in the fragment's meta.
162
+ *
163
+ * @param param - The key to store the value under
164
+ * @param value - The value to store
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * fragment.set('userId', '12345');
169
+ * fragment.set('role', 'admin');
170
+ * ```
171
+ */
172
+ set<K extends keyof _MetaItems>(param: K, value: _MetaItems[K]): void {
173
+ this._meta.set(param, value);
174
+ }
175
+
176
+ /**
177
+ * Removes a specific key from the fragment's meta.
178
+ *
179
+ * @param param - The key to remove
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * fragment.drop('temporaryData');
184
+ * ```
185
+ */
186
+ drop(param: keyof _MetaItems): void {
187
+ this._meta.delete(param);
188
+ }
189
+
190
+ /**
191
+ * Clears all data from the fragment's meta.
192
+ *
193
+ * Use with caution as this will remove all stored data in the fragment.
194
+ *
195
+ * @example
196
+ * ```typescript
197
+ * fragment.clear(); // All meta data is now gone
198
+ * ```
199
+ */
200
+ clear(): void {
201
+ this._meta.clear();
202
+ }
203
+
204
+ /**
205
+ * Gets the number of items stored in the fragment's meta.
206
+ *
207
+ * @returns The count of stored meta items
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * console.log(`Fragment contains ${fragment.size()} items`);
212
+ * ```
213
+ */
214
+ size(): number {
215
+ return this._meta.size();
216
+ }
217
+
218
+ /**
219
+ * Gets all keys currently stored in the fragment's meta.
220
+ *
221
+ * @returns Array of all meta keys
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const keys = fragment.keys();
226
+ * console.log('Stored keys:', keys);
227
+ * ```
228
+ */
229
+ keys(): (keyof _MetaItems)[] {
230
+ return this._meta.toArray().map(([key]) => key);
231
+ }
232
+
233
+ /**
234
+ * Sets multiple values at once in the fragment's meta.
235
+ *
236
+ * @param data - Object containing key-value pairs to set
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * fragment.setMultiple({
241
+ * userId: '12345',
242
+ * role: 'admin',
243
+ * lastLogin: new Date()
244
+ * });
245
+ * ```
246
+ */
247
+ setMultiple(data: A_TYPES__DeepPartial<_MetaItems>): void {
248
+ Object.entries(data).forEach(([key, value]) => {
249
+ if (value !== undefined) {
250
+ this._meta.set(key as keyof _MetaItems, value);
251
+ }
252
+ });
253
+ }
44
254
 
255
+ /**
256
+ * Creates a shallow copy of the fragment with the same meta data.
257
+ *
258
+ * @param newName - Optional new name for the cloned fragment
259
+ * @returns A new fragment instance with copied meta
260
+ *
261
+ * @example
262
+ * ```typescript
263
+ * const original = new A_Fragment<{ data: string }>({ name: 'original' });
264
+ * original.set('data', 'test');
265
+ *
266
+ * const clone = original.clone('cloned');
267
+ * console.log(clone.get('data')); // 'test'
268
+ * ```
269
+ */
270
+ clone(newName?: string): A_Fragment<_MetaItems, _SerializedType> {
271
+ const cloned = new (this.constructor as any)({
272
+ name: newName || `${this._name}_copy`
273
+ });
274
+
275
+ // Copy all meta data
276
+ this._meta.toArray().forEach(([key, value]) => {
277
+ cloned.set(key, value);
278
+ });
279
+
280
+ return cloned;
281
+ }
45
282
 
46
283
 
47
284
  /**
48
- * Returns the JSON representation of the Fragment
285
+ * Serializes the fragment to a JSON-compatible object.
286
+ *
287
+ * This method combines the fragment's name with all meta data to create
288
+ * a serializable representation. The return type is determined by the
289
+ * _SerializedType generic parameter, allowing for custom serialization formats.
290
+ *
291
+ * @returns A serialized representation of the fragment
49
292
  *
50
- * @returns
293
+ * @example
294
+ * ```typescript
295
+ * const fragment = new A_Fragment<{ userId: string, role: string }>({
296
+ * name: 'UserFragment'
297
+ * });
298
+ * fragment.set('userId', '12345');
299
+ * fragment.set('role', 'admin');
300
+ *
301
+ * const json = fragment.toJSON();
302
+ * // Result: { name: 'UserFragment', userId: '12345', role: 'admin' }
303
+ * ```
51
304
  */
52
- toJSON(): _MemoryItems & { name: string } {
53
- return {
305
+ toJSON(): _SerializedType {
306
+ const result = {
54
307
  name: this.name,
55
- ...this.memory.toArray().reduce((acc, [key, value]) => {
308
+
309
+ ...this.meta.toArray().reduce((acc, [key, value]) => {
56
310
  acc[key] = value;
57
311
  return acc;
58
- }, {} as _MemoryItems)
312
+ }, {} as _MetaItems)
59
313
  };
60
- }
61
314
 
315
+ return result as unknown as _SerializedType;
316
+ }
62
317
  }
@@ -20,9 +20,9 @@ export type A_TYPES__Fragment_Init = {
20
20
  */
21
21
  export type A_TYPES__Fragment_Serialized = {
22
22
  /**
23
- * The ASEID of the fragment
23
+ * The Name of the fragment
24
24
  */
25
- aseid: string
25
+ name: string
26
26
  };
27
27
 
28
28
 
@@ -0,0 +1,290 @@
1
+ import { A_Fragment } from "@adaas/a-concept/global/A-Fragment/A-Fragment.class";
2
+
3
+ jest.retryTimes(0);
4
+
5
+ describe('A-Fragment Tests', () => {
6
+
7
+ it('It Should be possible to create an A_Fragment instance', async () => {
8
+
9
+ const fragment = new A_Fragment();
10
+
11
+ expect(fragment).toBeDefined();
12
+ expect(fragment.name).toBe('A_Fragment');
13
+ expect(fragment.size()).toBe(0);
14
+ });
15
+
16
+ it('It Should be possible to create an A_Fragment instance with custom name', async () => {
17
+
18
+ const fragment = new A_Fragment({ name: 'CustomFragment' });
19
+
20
+ expect(fragment).toBeDefined();
21
+ expect(fragment.name).toBe('CustomFragment');
22
+ expect(fragment.size()).toBe(0);
23
+ });
24
+
25
+ it('It Should be possible to create a typed A_Fragment instance', async () => {
26
+
27
+ const fragment = new A_Fragment<{ userId: string; role: string }>({
28
+ name: 'UserFragment'
29
+ });
30
+
31
+ expect(fragment).toBeDefined();
32
+ expect(fragment.name).toBe('UserFragment');
33
+ expect(fragment.size()).toBe(0);
34
+ });
35
+
36
+ it('It Should be possible to store and retrieve data from fragment meta', async () => {
37
+
38
+ const fragment = new A_Fragment<{ userId: string; role: string }>({
39
+ name: 'UserFragment'
40
+ });
41
+
42
+ fragment.set('userId', '12345');
43
+ fragment.set('role', 'admin');
44
+
45
+ expect(fragment.get('userId')).toBe('12345');
46
+ expect(fragment.get('role')).toBe('admin');
47
+ expect(fragment.has('userId')).toBe(true);
48
+ expect(fragment.has('role')).toBe(true);
49
+ expect(fragment.size()).toBe(2);
50
+ });
51
+
52
+ it('It Should be possible to check if keys exist in fragment meta', async () => {
53
+
54
+ const fragment = new A_Fragment<{ test: string; value: number }>();
55
+
56
+ expect(fragment.has('test')).toBe(false);
57
+ expect(fragment.has('value')).toBe(false);
58
+
59
+ fragment.set('test', 'hello');
60
+
61
+ expect(fragment.has('test')).toBe(true);
62
+ expect(fragment.has('value')).toBe(false);
63
+ });
64
+
65
+ it('It Should be possible to get all keys from fragment meta', async () => {
66
+
67
+ const fragment = new A_Fragment<{ userId: string; role: string; lastLogin: Date }>();
68
+
69
+ fragment.set('userId', '12345');
70
+ fragment.set('role', 'admin');
71
+
72
+ const keys = fragment.keys();
73
+
74
+ expect(keys).toContain('userId');
75
+ expect(keys).toContain('role');
76
+ expect(keys).not.toContain('lastLogin');
77
+ expect(keys.length).toBe(2);
78
+ });
79
+
80
+ it('It Should be possible to set multiple values at once', async () => {
81
+
82
+ const fragment = new A_Fragment<{ userId: string; role: string; isActive: boolean }>();
83
+
84
+ fragment.setMultiple({
85
+ userId: '12345',
86
+ role: 'admin',
87
+ isActive: true
88
+ });
89
+
90
+ expect(fragment.get('userId')).toBe('12345');
91
+ expect(fragment.get('role')).toBe('admin');
92
+ expect(fragment.get('isActive')).toBe(true);
93
+ expect(fragment.size()).toBe(3);
94
+ });
95
+
96
+ it('It Should be possible to drop specific keys from fragment meta', async () => {
97
+
98
+ const fragment = new A_Fragment<{ userId: string; role: string; temp: string }>();
99
+
100
+ fragment.set('userId', '12345');
101
+ fragment.set('role', 'admin');
102
+ fragment.set('temp', 'temporary');
103
+
104
+ expect(fragment.size()).toBe(3);
105
+
106
+ fragment.drop('temp');
107
+
108
+ expect(fragment.has('temp')).toBe(false);
109
+ expect(fragment.has('userId')).toBe(true);
110
+ expect(fragment.has('role')).toBe(true);
111
+ expect(fragment.size()).toBe(2);
112
+ });
113
+
114
+ it('It Should be possible to clear all data from fragment meta', async () => {
115
+
116
+ const fragment = new A_Fragment<{ userId: string; role: string }>();
117
+
118
+ fragment.set('userId', '12345');
119
+ fragment.set('role', 'admin');
120
+
121
+ expect(fragment.size()).toBe(2);
122
+
123
+ fragment.clear();
124
+
125
+ expect(fragment.size()).toBe(0);
126
+ expect(fragment.has('userId')).toBe(false);
127
+ expect(fragment.has('role')).toBe(false);
128
+ });
129
+
130
+ it('It Should be possible to clone a fragment with its data', async () => {
131
+
132
+ const original = new A_Fragment<{ userId: string; role: string }>({
133
+ name: 'OriginalFragment'
134
+ });
135
+
136
+ original.set('userId', '12345');
137
+ original.set('role', 'admin');
138
+
139
+ const clone = original.clone('ClonedFragment');
140
+
141
+ expect(clone).toBeDefined();
142
+ expect(clone.name).toBe('ClonedFragment');
143
+ expect(clone.get('userId')).toBe('12345');
144
+ expect(clone.get('role')).toBe('admin');
145
+ expect(clone.size()).toBe(2);
146
+
147
+ // Verify they are separate instances
148
+ clone.set('userId', '67890');
149
+ expect(original.get('userId')).toBe('12345');
150
+ expect(clone.get('userId')).toBe('67890');
151
+ });
152
+
153
+ it('It Should be possible to clone a fragment with auto-generated name', async () => {
154
+
155
+ const original = new A_Fragment<{ test: string }>({
156
+ name: 'TestFragment'
157
+ });
158
+
159
+ original.set('test', 'value');
160
+
161
+ const clone = original.clone();
162
+
163
+ expect(clone.name).toBe('TestFragment_copy');
164
+ expect(clone.get('test')).toBe('value');
165
+ });
166
+
167
+ it('It Should be possible to serialize a fragment to JSON', async () => {
168
+
169
+ const fragment = new A_Fragment<{ userId: string; role: string }>({
170
+ name: 'UserFragment'
171
+ });
172
+
173
+ fragment.set('userId', '12345');
174
+ fragment.set('role', 'admin');
175
+
176
+ const json = fragment.toJSON();
177
+
178
+ expect(json).toBeDefined();
179
+ expect(json.name).toBe('UserFragment');
180
+ expect(json.userId).toBe('12345');
181
+ expect(json.role).toBe('admin');
182
+ });
183
+
184
+ it('It Should be possible to create an inherited A_Fragment instance', async () => {
185
+
186
+ class CustomFragment extends A_Fragment<{ sessionId: string; timestamp: number }> {
187
+ constructor() {
188
+ super({ name: 'CustomFragment' });
189
+ }
190
+
191
+ getSessionInfo(): string {
192
+ const sessionId = this.get('sessionId') || 'unknown';
193
+ const timestamp = this.get('timestamp') || 0;
194
+ return `${sessionId}-${timestamp}`;
195
+ }
196
+ }
197
+
198
+ const fragment = new CustomFragment();
199
+
200
+ expect(fragment).toBeDefined();
201
+ expect(fragment.name).toBe('CustomFragment');
202
+ expect(fragment instanceof A_Fragment).toBe(true);
203
+ expect(fragment instanceof CustomFragment).toBe(true);
204
+
205
+ fragment.set('sessionId', 'sess123');
206
+ fragment.set('timestamp', 1698765432);
207
+
208
+ expect(fragment.getSessionInfo()).toBe('sess123-1698765432');
209
+ });
210
+
211
+ it('It Should be possible to create a fragment with custom serialization', async () => {
212
+
213
+ class SessionFragment extends A_Fragment<
214
+ { sessionId: string; timestamp: number },
215
+ { name: string; sessionData: string }
216
+ > {
217
+ constructor() {
218
+ super({ name: 'SessionFragment' });
219
+ }
220
+
221
+ toJSON(): { name: string; sessionData: string } {
222
+ const sessionId = this.get('sessionId') || 'unknown';
223
+ const timestamp = this.get('timestamp') || 0;
224
+ return {
225
+ name: this.name,
226
+ sessionData: `${sessionId}-${timestamp}`
227
+ };
228
+ }
229
+ }
230
+
231
+ const fragment = new SessionFragment();
232
+ fragment.set('sessionId', 'sess123');
233
+ fragment.set('timestamp', 1698765432);
234
+
235
+ const json = fragment.toJSON();
236
+
237
+ expect(json).toBeDefined();
238
+ expect(json.name).toBe('SessionFragment');
239
+ expect(json.sessionData).toBe('sess123-1698765432');
240
+ expect(json).not.toHaveProperty('sessionId');
241
+ expect(json).not.toHaveProperty('timestamp');
242
+ });
243
+
244
+ it('It Should be possible to access the underlying meta object', async () => {
245
+
246
+ const fragment = new A_Fragment<{ test: string; value: number }>();
247
+
248
+ expect(fragment.meta).toBeDefined();
249
+ expect(fragment.meta.size()).toBe(0);
250
+
251
+ fragment.set('test', 'hello');
252
+ fragment.set('value', 42);
253
+
254
+ expect(fragment.meta.size()).toBe(2);
255
+ expect(fragment.meta.has('test')).toBe(true);
256
+ expect(fragment.meta.get('test')).toBe('hello');
257
+ });
258
+
259
+ it('It Should handle undefined values correctly', async () => {
260
+
261
+ const fragment = new A_Fragment<{ optional?: string; required: string }>();
262
+
263
+ expect(fragment.get('optional')).toBeUndefined();
264
+ expect(fragment.get('required')).toBeUndefined();
265
+
266
+ fragment.set('required', 'value');
267
+
268
+ expect(fragment.get('required')).toBe('value');
269
+ expect(fragment.get('optional')).toBeUndefined();
270
+ expect(fragment.has('required')).toBe(true);
271
+ expect(fragment.has('optional')).toBe(false);
272
+ });
273
+
274
+ it('It Should handle setMultiple with undefined values correctly', async () => {
275
+
276
+ const fragment = new A_Fragment<{ a?: string; b?: number; c: boolean }>();
277
+
278
+ fragment.setMultiple({
279
+ a: 'test',
280
+ b: undefined, // Should be ignored
281
+ c: true
282
+ });
283
+
284
+ expect(fragment.has('a')).toBe(true);
285
+ expect(fragment.has('b')).toBe(false);
286
+ expect(fragment.has('c')).toBe(true);
287
+ expect(fragment.size()).toBe(2);
288
+ });
289
+
290
+ });