@vived/core 2.0.0 → 2.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.
- package/README.md +275 -65
- package/dist/cjs/AppObject/AppObjectEntityRepo.js +16 -2
- package/dist/cjs/AppObject/AppObjectEntityRepo.js.map +1 -1
- package/dist/cjs/ExampleFeature/Entities/ExampleRepo.js +3 -4
- package/dist/cjs/ExampleFeature/Entities/ExampleRepo.js.map +1 -1
- package/dist/cjs/ExampleFeature/index.js +23 -0
- package/dist/cjs/ExampleFeature/index.js.map +1 -0
- package/dist/esm/AppObject/AppObjectEntityRepo.js +16 -2
- package/dist/esm/AppObject/AppObjectEntityRepo.js.map +1 -1
- package/dist/esm/ExampleFeature/Entities/ExampleRepo.js +3 -4
- package/dist/esm/ExampleFeature/Entities/ExampleRepo.js.map +1 -1
- package/dist/esm/ExampleFeature/index.js +7 -0
- package/dist/esm/ExampleFeature/index.js.map +1 -0
- package/dist/types/AppObject/AppObjectEntityRepo.d.ts +9 -1
- package/dist/types/AppObject/AppObjectEntityRepo.d.ts.map +1 -1
- package/dist/types/ExampleFeature/Entities/ExampleRepo.d.ts +1 -1
- package/dist/types/ExampleFeature/Entities/ExampleRepo.d.ts.map +1 -1
- package/dist/types/ExampleFeature/index.d.ts +5 -0
- package/dist/types/ExampleFeature/index.d.ts.map +1 -0
- package/package.json +3 -2
- package/src/AppObject/README.md +476 -0
- package/src/DomainFactories/README.md +154 -0
- package/src/Entities/README.md +340 -0
- package/src/ExampleFeature/README.md +804 -0
- package/src/Types/README.md +549 -0
- package/src/Utilities/README.md +478 -0
- package/src/ValueObjects/README.md +552 -0
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Core Components for VIVED Apps and Hosts
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`@vived/core` provides a comprehensive architecture framework for building scalable, maintainable applications using Clean Architecture principles. The library combines a component-based architecture (AppObject pattern) with reactive entities, immutable value objects, and utility functions optimized for 3D graphics and interactive applications.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
@@ -12,93 +12,303 @@ Core Components for VIVED Apps and Hosts
|
|
|
12
12
|
npm install @vived/core
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Documentation
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
This package includes comprehensive documentation for each module:
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
- **[AppObject Architecture](src/AppObject/README.md)** - Component-based architecture pattern with entities, use cases, presentation managers, views, and controllers
|
|
20
|
+
- **[DomainFactories](src/DomainFactories/README.md)** - Multi-phase initialization system for organizing domain components
|
|
21
|
+
- **[Entities](src/Entities/README.md)** - Observable entities with memoized properties for automatic change detection
|
|
22
|
+
- **[ValueObjects](src/ValueObjects/README.md)** - Immutable mathematical primitives for 3D graphics and geometry
|
|
23
|
+
- **[Utilities](src/Utilities/README.md)** - Helper functions for animations, colors, conversions, and file operations
|
|
24
|
+
- **[Types](src/Types/README.md)** - TypeScript interfaces for adapters and application boundaries
|
|
25
|
+
- **[ExampleFeature](src/ExampleFeature/README.md)** - Complete reference implementation demonstrating best practices
|
|
20
26
|
|
|
21
|
-
|
|
22
|
-
- Each AppObject has a unique ID and belongs to an AppObjectRepo
|
|
23
|
-
- Components can be added, removed, and queried
|
|
24
|
-
- AppObjects are observable entities, notifying observers when components change
|
|
27
|
+
## Quick Start
|
|
25
28
|
|
|
26
|
-
###
|
|
29
|
+
### AppObject Architecture
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
- Provides methods to find, create, and manage AppObjects
|
|
30
|
-
- Handles singleton components across the application
|
|
31
|
-
- Includes logging infrastructure for debugging
|
|
31
|
+
The core architecture follows Clean Architecture dependency rules with a component-based design:
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
```typescript
|
|
34
|
+
import { makeAppObjectRepo } from "@vived/core";
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
// Create the application repository
|
|
37
|
+
const appObjects = makeAppObjectRepo();
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- Provides change notifications when state is modified
|
|
39
|
+
// Create an AppObject
|
|
40
|
+
const myObject = appObjects.getOrCreate("myObject");
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
// Add components to it
|
|
43
|
+
const entity = makeMyEntity(myObject);
|
|
44
|
+
const useCase = makeMyUseCase(myObject);
|
|
45
|
+
const presentationManager = makeMyPM(myObject);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**Key Components:**
|
|
49
|
+
- **Entities** - Store and manage domain data with automatic change notifications
|
|
50
|
+
- **Use Cases** - Implement business logic and operations
|
|
51
|
+
- **Presentation Managers** - Transform entity data into view models for UI
|
|
52
|
+
- **Controllers** - Provide simplified APIs for UI interactions
|
|
53
|
+
- **Adapters** - Connect UI frameworks (React, Vue, etc.) to presentation managers
|
|
54
|
+
|
|
55
|
+
### Domain Factories
|
|
56
|
+
|
|
57
|
+
Organize features using domain factories with phased initialization:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { makeDomainFactoryRepo } from "@vived/core";
|
|
61
|
+
|
|
62
|
+
// Create domain factory repository
|
|
63
|
+
const domainFactoryRepo = makeDomainFactoryRepo(appObjects);
|
|
64
|
+
|
|
65
|
+
// Create your feature factories
|
|
66
|
+
new MyFeatureDomainFactory(appObjects.getOrCreate("MyFeature"));
|
|
67
|
+
|
|
68
|
+
// Initialize all features in proper order
|
|
69
|
+
domainFactoryRepo.setupDomain();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Reactive Entities
|
|
73
|
+
|
|
74
|
+
Build reactive data models with automatic change detection:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { MemoizedString, MemoizedNumber, ObservableEntity } from "@vived/core";
|
|
78
|
+
|
|
79
|
+
class UserEntity extends ObservableEntity
|
|
80
|
+
{
|
|
81
|
+
private nameProperty = new MemoizedString("", () => this.notify());
|
|
82
|
+
private ageProperty = new MemoizedNumber(0, () => this.notify());
|
|
83
|
+
|
|
84
|
+
get name() { return this.nameProperty.val; }
|
|
85
|
+
set name(value: string) { this.nameProperty.val = value; }
|
|
86
|
+
|
|
87
|
+
get age() { return this.ageProperty.val; }
|
|
88
|
+
set age(value: number) { this.ageProperty.val = value; }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Changes automatically notify observers
|
|
92
|
+
const user = new UserEntity();
|
|
93
|
+
user.addObserver(() => console.log("User changed!"));
|
|
94
|
+
user.name = "John"; // Triggers notification
|
|
95
|
+
```
|
|
45
96
|
|
|
46
|
-
|
|
47
|
-
- Implements business logic
|
|
48
|
-
- Coordinates between data layer and presentation
|
|
97
|
+
### Value Objects for 3D Graphics
|
|
49
98
|
|
|
50
|
-
|
|
51
|
-
- Handles external inputs (user actions, system events)
|
|
52
|
-
- Delegates to appropriate use cases
|
|
99
|
+
Work with immutable mathematical primitives:
|
|
53
100
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
- Consumes view models and renders UI
|
|
101
|
+
```typescript
|
|
102
|
+
import { Vector3, Quaternion, Matrix, Color } from "@vived/core";
|
|
57
103
|
|
|
58
|
-
|
|
104
|
+
// Vector operations
|
|
105
|
+
const position = new Vector3(10, 20, 30);
|
|
106
|
+
const direction = Vector3.Forward();
|
|
107
|
+
const sum = Vector3.Add(position, direction);
|
|
59
108
|
|
|
60
|
-
|
|
109
|
+
// Rotations with quaternions
|
|
110
|
+
const rotation = Quaternion.FromYawPitchRoll(
|
|
111
|
+
Angle.FromDegrees(45),
|
|
112
|
+
Angle.FromDegrees(0),
|
|
113
|
+
Angle.FromDegrees(0)
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
// Transformation matrices
|
|
117
|
+
const transform = Matrix.Translation(position);
|
|
118
|
+
const rotationMatrix = Matrix.FromQuaternion(rotation);
|
|
119
|
+
|
|
120
|
+
// Colors
|
|
121
|
+
const red = Color.RGB(255, 0, 0);
|
|
122
|
+
const transparent = Color.RGBA(0, 0, 0, 128);
|
|
123
|
+
const named = Color.X11("CornflowerBlue");
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Animation Utilities
|
|
127
|
+
|
|
128
|
+
Create smooth animations with easing functions:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { LerpNumber, quintInOut } from "@vived/core";
|
|
132
|
+
|
|
133
|
+
const lerper = new LerpNumber();
|
|
134
|
+
|
|
135
|
+
// Animate a value from 0 to 100 over 2 seconds
|
|
136
|
+
await lerper.lerp({
|
|
137
|
+
start: 0,
|
|
138
|
+
end: 100,
|
|
139
|
+
durationMS: 2000,
|
|
140
|
+
ease: quintInOut,
|
|
141
|
+
update: (value) => {
|
|
142
|
+
element.style.opacity = value / 100;
|
|
143
|
+
},
|
|
144
|
+
onComplete: () => {
|
|
145
|
+
console.log("Animation complete!");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### UI Integration with Adapters
|
|
151
|
+
|
|
152
|
+
Connect your UI framework to the architecture:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { useEffect, useState } from "react";
|
|
156
|
+
import { myFeatureAdapter } from "@vived/core";
|
|
157
|
+
|
|
158
|
+
function MyComponent({ id, appObjects })
|
|
159
|
+
{
|
|
160
|
+
const [viewModel, setViewModel] = useState(myFeatureAdapter.defaultVM);
|
|
161
|
+
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
// Subscribe to updates
|
|
164
|
+
myFeatureAdapter.subscribe(id, appObjects, setViewModel);
|
|
165
|
+
|
|
166
|
+
// Cleanup on unmount
|
|
167
|
+
return () => {
|
|
168
|
+
myFeatureAdapter.unsubscribe(id, appObjects, setViewModel);
|
|
169
|
+
};
|
|
170
|
+
}, [id]);
|
|
171
|
+
|
|
172
|
+
return <div>{viewModel.someProperty}</div>;
|
|
173
|
+
}
|
|
174
|
+
```
|
|
61
175
|
|
|
62
|
-
|
|
63
|
-
- **Entities/** - Domain models for storing and managing state
|
|
64
|
-
- **PMs/** - Presentation Managers that transform data for UI consumption
|
|
65
|
-
- **UCs/** - Use Cases that implement business logic operations
|
|
66
|
-
- **Controllers/** - Simplified API for UI interaction
|
|
67
|
-
- **Adapters/** - Connect UI frameworks to PMs
|
|
68
|
-
- **Mocks/** - Test doubles for unit testing
|
|
69
|
-
- **Factory/** - Factories for creating the features at runtime
|
|
176
|
+
## Package Contents
|
|
70
177
|
|
|
71
|
-
|
|
178
|
+
### Architecture Components
|
|
72
179
|
|
|
73
|
-
|
|
180
|
+
- **AppObject System** - Component container pattern with dependency injection
|
|
181
|
+
- **Entity System** - Observable entities with memoized properties
|
|
182
|
+
- **Repository Pattern** - Manage collections of entities
|
|
183
|
+
- **Use Cases** - Business logic layer
|
|
184
|
+
- **Presentation Managers** - View model transformation layer
|
|
185
|
+
- **Controllers** - Simplified UI interaction APIs
|
|
186
|
+
- **Adapters** - Framework-agnostic UI integration
|
|
74
187
|
|
|
75
|
-
|
|
188
|
+
### Value Objects
|
|
76
189
|
|
|
77
|
-
|
|
78
|
-
-
|
|
79
|
-
-
|
|
80
|
-
-
|
|
81
|
-
-
|
|
82
|
-
-
|
|
83
|
-
-
|
|
190
|
+
**Vectors & Geometry:**
|
|
191
|
+
- Vector2, Vector3 - 2D and 3D vectors with full operations
|
|
192
|
+
- Quaternion - 3D rotation representation
|
|
193
|
+
- Matrix - 4x4 transformation matrices
|
|
194
|
+
- Angle - Type-safe angle conversions
|
|
195
|
+
- Rectangle - 2D rectangular bounds
|
|
196
|
+
- LineSegment2D - 2D line segment operations
|
|
197
|
+
- ParametricLine - 3D parametric line representation
|
|
198
|
+
- ParametricPlane - 3D plane representation
|
|
84
199
|
|
|
85
|
-
|
|
86
|
-
-
|
|
87
|
-
- **ParametricPlane**: Represents planes with point and normal for 3D geometric operations
|
|
88
|
-
- **LineSegment2D**: Represents finite line segments with intersection testing
|
|
200
|
+
**Graphics:**
|
|
201
|
+
- Color - RGBA color with hex and X11 color support
|
|
89
202
|
|
|
90
|
-
|
|
91
|
-
-
|
|
92
|
-
- **Version**: Semantic versioning implementation with comparison operators
|
|
203
|
+
**Versioning:**
|
|
204
|
+
- Version - Semantic version parsing and comparison
|
|
93
205
|
|
|
94
|
-
|
|
206
|
+
### Utilities
|
|
95
207
|
|
|
96
|
-
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
|
|
101
|
-
|
|
208
|
+
**Animation:**
|
|
209
|
+
- LerpNumber - Smooth numeric interpolation
|
|
210
|
+
- Easing functions - 20+ easing functions (quad, cubic, quart, quint, sin, expo, circ)
|
|
211
|
+
- interpolateNumber - Basic numeric interpolation
|
|
212
|
+
|
|
213
|
+
**Color:**
|
|
214
|
+
- addAlphaToHex - Add transparency to hex colors
|
|
215
|
+
- alphaToHex - Convert alpha values to hex
|
|
216
|
+
|
|
217
|
+
**Conversion:**
|
|
218
|
+
- degreesToRadians - Angle conversion
|
|
219
|
+
- Length converters - Inches/feet to meters and vice versa
|
|
220
|
+
|
|
221
|
+
**File Operations:**
|
|
222
|
+
- downloadFile - Trigger browser downloads
|
|
223
|
+
- generateUniqueID - UUID v4 generation
|
|
224
|
+
|
|
225
|
+
### Types
|
|
226
|
+
|
|
227
|
+
- PmAdapter - Interface for connecting UI to presentation managers
|
|
228
|
+
- SingletonPmAdapter - Interface for singleton presentation managers
|
|
229
|
+
- EaseFn - Type definition for easing functions
|
|
230
|
+
- AppBoundary - Application embedding interfaces
|
|
231
|
+
|
|
232
|
+
## Architecture Principles
|
|
233
|
+
|
|
234
|
+
### Clean Architecture
|
|
235
|
+
|
|
236
|
+
The library enforces dependency rules where inner layers never depend on outer layers:
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
UI Layer
|
|
240
|
+
↓
|
|
241
|
+
Adapters & Controllers
|
|
242
|
+
↓
|
|
243
|
+
Presentation Managers
|
|
244
|
+
↓
|
|
245
|
+
Use Cases
|
|
246
|
+
↓
|
|
247
|
+
Entities
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Component-Based Design
|
|
251
|
+
|
|
252
|
+
Everything is a component attached to an AppObject:
|
|
253
|
+
- Promotes composition over inheritance
|
|
254
|
+
- Enables dependency injection
|
|
255
|
+
- Facilitates testing with mock components
|
|
256
|
+
- Supports both instance and singleton patterns
|
|
257
|
+
|
|
258
|
+
### Reactive by Default
|
|
259
|
+
|
|
260
|
+
Entities notify observers when they change:
|
|
261
|
+
- Automatic UI updates
|
|
262
|
+
- No manual refresh logic needed
|
|
263
|
+
- Performance optimized with memoization
|
|
264
|
+
|
|
265
|
+
### Immutable Value Objects
|
|
266
|
+
|
|
267
|
+
Mathematical primitives are immutable:
|
|
268
|
+
- Thread-safe operations
|
|
269
|
+
- Predictable behavior
|
|
270
|
+
- Easy to reason about
|
|
271
|
+
- Optimal for 3D graphics pipelines
|
|
272
|
+
|
|
273
|
+
## Example Feature
|
|
274
|
+
|
|
275
|
+
See [ExampleFeature](src/ExampleFeature/README.md) for a complete, production-ready reference implementation showing:
|
|
276
|
+
- Feature folder structure
|
|
277
|
+
- Entity, Use Case, and PM implementation
|
|
278
|
+
- Controller and Adapter patterns
|
|
279
|
+
- React integration examples
|
|
280
|
+
- Testing strategies
|
|
281
|
+
- Factory-based initialization
|
|
282
|
+
|
|
283
|
+
## Use Cases
|
|
284
|
+
|
|
285
|
+
- **3D Graphics Applications** - WebGL, Three.js, Babylon.js applications
|
|
286
|
+
- **VR/AR Experiences** - Interactive virtual environments
|
|
287
|
+
- **Data Visualization** - Charts, graphs, and interactive dashboards
|
|
288
|
+
- **Game Development** - Game engines and interactive experiences
|
|
289
|
+
- **Educational Software** - Interactive learning applications
|
|
290
|
+
- **Configuration Tools** - Complex application settings and preferences
|
|
291
|
+
|
|
292
|
+
## TypeScript Support
|
|
293
|
+
|
|
294
|
+
Fully typed with TypeScript definitions included. Supports both ESM and CommonJS:
|
|
295
|
+
|
|
296
|
+
```json
|
|
297
|
+
{
|
|
298
|
+
"exports": {
|
|
299
|
+
".": {
|
|
300
|
+
"import": "./dist/esm/index.js",
|
|
301
|
+
"require": "./dist/cjs/index.js",
|
|
302
|
+
"types": "./dist/types/index.d.ts"
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Contributing
|
|
309
|
+
|
|
310
|
+
This is a foundational library for VIVED applications. For detailed architecture decisions and patterns, refer to the comprehensive documentation in each module's README.
|
|
102
311
|
|
|
103
312
|
## License
|
|
313
|
+
|
|
104
314
|
ISC
|
|
@@ -165,6 +165,19 @@ class AppObjectEntityRepo extends AppObjectEntity_1.AppObjectEntity {
|
|
|
165
165
|
this.getAll = () => {
|
|
166
166
|
return Array.from(this.entityLookup.values());
|
|
167
167
|
};
|
|
168
|
+
/**
|
|
169
|
+
* Gets an entity by ID, or creates it if it doesn't exist.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} id - The ID of the entity to get or create
|
|
172
|
+
* @returns {T} The existing or newly created entity
|
|
173
|
+
*/
|
|
174
|
+
this.getOrCreate = (id) => {
|
|
175
|
+
const existing = this.getById(id);
|
|
176
|
+
if (existing) {
|
|
177
|
+
return existing;
|
|
178
|
+
}
|
|
179
|
+
return this.create(id);
|
|
180
|
+
};
|
|
168
181
|
}
|
|
169
182
|
/**
|
|
170
183
|
* Adds an entity to the repository.
|
|
@@ -195,7 +208,8 @@ class AppObjectEntityRepo extends AppObjectEntity_1.AppObjectEntity {
|
|
|
195
208
|
*/
|
|
196
209
|
create(id) {
|
|
197
210
|
const idToUse = id ?? (0, Utilities_1.generateUniqueID)();
|
|
198
|
-
const
|
|
211
|
+
const ao = this.appObjects.getOrCreate(idToUse);
|
|
212
|
+
const entity = this.entityFactory(ao);
|
|
199
213
|
this.add(entity);
|
|
200
214
|
return entity;
|
|
201
215
|
}
|
|
@@ -209,7 +223,7 @@ class AppObjectEntityRepo extends AppObjectEntity_1.AppObjectEntity {
|
|
|
209
223
|
* @returns {T} A new entity instance
|
|
210
224
|
* @throws {Error} If not overridden in derived class
|
|
211
225
|
*/
|
|
212
|
-
entityFactory(
|
|
226
|
+
entityFactory(appObject) {
|
|
213
227
|
// Override this method in derived classes to provide entity creation logic
|
|
214
228
|
throw new Error("Entity factory not implemented.");
|
|
215
229
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AppObjectEntityRepo.js","sourceRoot":"","sources":["../../../src/AppObject/AppObjectEntityRepo.ts"],"names":[],"mappings":";;;AAAA,0CAA2C;AAC3C,4CAAgD;AAChD,uDAAoD;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,mBAEX,SAAQ,iCAAe;IAFzB;;QAGU,iBAAY,GAAG,IAAI,GAAG,EAAa,CAAC;QAEpC,2BAAsB,GAAG,IAAI,uBAAY,EAAK,CAAC;QACvD;;;;WAIG;QACH,2BAAsB,GAAG,CAAC,QAAkC,EAAE,EAAE;YAC9D,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CAAC;QAEF;;;;WAIG;QACH,8BAAyB,GAAG,CAAC,QAAkC,EAAQ,EAAE;YACvE,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/C,CAAC,CAAC;QAEM,6BAAwB,GAAG,IAAI,uBAAY,EAAK,CAAC;QACzD;;;;WAIG;QACH,6BAAwB,GAAG,CAAC,QAAoC,EAAE,EAAE;YAClE,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9C,CAAC,CAAC;QAEF;;;;WAIG;QACH,gCAA2B,GAAG,CAC5B,QAAoC,EAC9B,EAAE;YACR,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACjD,CAAC,CAAC;Q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C;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAU;QACtB,2EAA2E;QAC3E,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;CA8EF;AAjMD,kDAiMC","sourcesContent":["import { ObserverList } from \"../Entities\";\r\nimport { generateUniqueID } from \"../Utilities\";\r\nimport { AppObjectEntity } from \"./AppObjectEntity\";\r\n\r\n/**\r\n * A repository for managing collections of AppObjectEntity instances.\r\n *\r\n * This generic class provides a centralized way to store, retrieve, and manage\r\n * entities that are associated with specific AppObjects. It implements the observer\r\n * pattern to notify listeners when entities are added or removed.\r\n *\r\n * Derived classes should override the `entityFactory` method to provide custom\r\n * entity creation logic for the `create` method.\r\n *\r\n * @template T - The type of entities managed by this repository, must extend AppObjectEntity\r\n * @extends AppObjectEntity\r\n *\r\n * @example\r\n * ```typescript\r\n * // Create a custom repository with entity factory\r\n * class PlayerRepo extends AppObjectEntityRepo<PlayerEntity> {\r\n * entityFactory(id: string): PlayerEntity {\r\n * const appObject = makeAppObject(id, this.appObjects);\r\n * return new PlayerEntity(appObject);\r\n * }\r\n * }\r\n *\r\n * const playerRepo = new PlayerRepo(repoAppObject);\r\n *\r\n * // Create a new player with auto-generated ID\r\n * const player = playerRepo.create();\r\n *\r\n * // Create a new player with specific ID\r\n * const specificPlayer = playerRepo.create(\"player1\");\r\n *\r\n * // Get a player by its ID\r\n * const retrievedPlayer = playerRepo.getById(\"player1\");\r\n * ```\r\n */\r\nexport class AppObjectEntityRepo<\r\n T extends AppObjectEntity,\r\n> extends AppObjectEntity {\r\n private entityLookup = new Map<string, T>();\r\n\r\n private onEntityAddedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is added to the repository.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to invoke when an entity is added\r\n */\r\n addEntityAddedObserver = (observer: (addedEntity: T) => void) => {\r\n this.onEntityAddedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity addition notifications.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityAddedObserver = (observer: (addedEntity: T) => void): void => {\r\n this.onEntityAddedObservers.remove(observer);\r\n };\r\n\r\n private onEntityRemovedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is removed from the repository.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to invoke when an entity is removed\r\n */\r\n addEntityRemovedObserver = (observer: (removedEntity: T) => void) => {\r\n this.onEntityRemovedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity removal notifications.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityRemovedObserver = (\r\n observer: (removedEntity: T) => void\r\n ): void => {\r\n this.onEntityRemovedObservers.remove(observer);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given ID, false otherwise\r\n */\r\n has = (id: string): boolean => {\r\n return this.entityLookup.has(id);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given AppObject ID, false otherwise\r\n * @deprecated Use has instead\r\n */\r\n hasForAppObject = (appObjectID: string): boolean => {\r\n return this.has(appObjectID);\r\n };\r\n\r\n /**\r\n * Adds an entity to the repository.\r\n *\r\n * If an entity with the same AppObject ID already exists, it is replaced.\r\n * Notifies all registered observers after the entity is added.\r\n *\r\n * @param {T} entity - The entity to add\r\n */\r\n add(entity: T) {\r\n const existing = this.entityLookup.get(entity.appObject.id);\r\n if (existing) {\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n }\r\n\r\n this.entityLookup.set(entity.appObject.id, entity);\r\n entity.addChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityAddedObservers.notify(entity);\r\n }\r\n\r\n /**\r\n * Creates a new entity with the specified or auto-generated ID and adds it to the repository.\r\n *\r\n * This method uses the `entityFactory` method to create the entity instance.\r\n * If no ID is provided, a unique ID is generated automatically.\r\n *\r\n * @param {string} [id] - Optional ID for the entity. If not provided, a unique ID is generated.\r\n * @returns {T} The newly created entity\r\n */\r\n create(id?: string): T {\r\n const idToUse = id ?? generateUniqueID();\r\n const entity = this.entityFactory(idToUse);\r\n this.add(entity);\r\n return entity;\r\n }\r\n\r\n /**\r\n * Factory method for creating entity instances.\r\n *\r\n * This method must be overridden in derived classes to provide custom entity creation logic.\r\n * It is called by the `create` method to instantiate new entities.\r\n *\r\n * @param {string} id - The ID to use for the new entity\r\n * @returns {T} A new entity instance\r\n * @throws {Error} If not overridden in derived class\r\n */\r\n entityFactory(id: string): T {\r\n // Override this method in derived classes to provide entity creation logic\r\n throw new Error(\"Entity factory not implemented.\");\r\n }\r\n\r\n /**\r\n * Removes the entity associated with the specified ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n */\r\n removeById = (id: string) => {\r\n const existing = this.entityLookup.get(id);\r\n if (!existing) return;\r\n\r\n this.entityLookup.delete(id);\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityRemovedObservers.notify(existing);\r\n };\r\n\r\n /**\r\n * Removes the entity associated with the specified AppObject ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n * @deprecated Use removeById instead\r\n */\r\n removeForAppObject = (id: string) => {\r\n this.removeById(id);\r\n };\r\n\r\n /**\r\n * Removes all entities from the repository.\r\n *\r\n * Unsubscribes from all entity change observers and notifies all registered\r\n * removal observers for each entity before clearing the repository.\r\n */\r\n deleteAll = () => {\r\n const entities = Array.from(this.entityLookup.values());\r\n\r\n entities.forEach((entity) => {\r\n entity.removeChangeObserver(this.notifyOnChange);\r\n this.onEntityRemovedObservers.notify(entity);\r\n });\r\n\r\n this.entityLookup.clear();\r\n this.notifyOnChange();\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n */\r\n getById = (id: string): T | undefined => {\r\n return this.entityLookup.get(id);\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n * @deprecated Use getById instead\r\n */\r\n getForAppObject = (appObjectID: string): T | undefined => {\r\n return this.getById(appObjectID);\r\n };\r\n\r\n /**\r\n * Gets all entities in the repository.\r\n *\r\n * @returns {T[]} An array of all entities\r\n */\r\n getAll = (): T[] => {\r\n return Array.from(this.entityLookup.values());\r\n };\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"AppObjectEntityRepo.js","sourceRoot":"","sources":["../../../src/AppObject/AppObjectEntityRepo.ts"],"names":[],"mappings":";;;AAAA,0CAA2C;AAC3C,4CAAgD;AAEhD,uDAAoD;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,mBAEX,SAAQ,iCAAe;IAFzB;;QAGU,iBAAY,GAAG,IAAI,GAAG,EAAa,CAAC;QAEpC,2BAAsB,GAAG,IAAI,uBAAY,EAAK,CAAC;QACvD;;;;WAIG;QACH,2BAAsB,GAAG,CAAC,QAAkC,EAAE,EAAE;YAC9D,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CAAC;QAEF;;;;WAIG;QACH,8BAAyB,GAAG,CAAC,QAAkC,EAAQ,EAAE;YACvE,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/C,CAAC,CAAC;QAEM,6BAAwB,GAAG,IAAI,uBAAY,E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C;YACb,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACrD,CAAC;QAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAC9C,IAAI,CAAC,cAAc,EAAE,CAAC;QACtB,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAW;QAChB,MAAM,OAAO,GAAG,EAAE,IAAI,IAAA,4BAAgB,GAAE,CAAC;QACzC,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;;;;OASG;IACH,aAAa,CAAC,SAAoB;QAChC,2EAA2E;QAC3E,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;CA4FF;AAhND,kDAgNC","sourcesContent":["import { ObserverList } from \"../Entities\";\r\nimport { generateUniqueID } from \"../Utilities\";\r\nimport { AppObject } from \"./AppObject\";\r\nimport { AppObjectEntity } from \"./AppObjectEntity\";\r\n\r\n/**\r\n * A repository for managing collections of AppObjectEntity instances.\r\n *\r\n * This generic class provides a centralized way to store, retrieve, and manage\r\n * entities that are associated with specific AppObjects. It implements the observer\r\n * pattern to notify listeners when entities are added or removed.\r\n *\r\n * Derived classes should override the `entityFactory` method to provide custom\r\n * entity creation logic for the `create` method.\r\n *\r\n * @template T - The type of entities managed by this repository, must extend AppObjectEntity\r\n * @extends AppObjectEntity\r\n *\r\n * @example\r\n * ```typescript\r\n * // Create a custom repository with entity factory\r\n * class PlayerRepo extends AppObjectEntityRepo<PlayerEntity> {\r\n * entityFactory(id: string): PlayerEntity {\r\n * const appObject = makeAppObject(id, this.appObjects);\r\n * return new PlayerEntity(appObject);\r\n * }\r\n * }\r\n *\r\n * const playerRepo = new PlayerRepo(repoAppObject);\r\n *\r\n * // Create a new player with auto-generated ID\r\n * const player = playerRepo.create();\r\n *\r\n * // Create a new player with specific ID\r\n * const specificPlayer = playerRepo.create(\"player1\");\r\n *\r\n * // Get a player by its ID\r\n * const retrievedPlayer = playerRepo.getById(\"player1\");\r\n * ```\r\n */\r\nexport class AppObjectEntityRepo<\r\n T extends AppObjectEntity,\r\n> extends AppObjectEntity {\r\n private entityLookup = new Map<string, T>();\r\n\r\n private onEntityAddedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is added to the repository.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to invoke when an entity is added\r\n */\r\n addEntityAddedObserver = (observer: (addedEntity: T) => void) => {\r\n this.onEntityAddedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity addition notifications.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityAddedObserver = (observer: (addedEntity: T) => void): void => {\r\n this.onEntityAddedObservers.remove(observer);\r\n };\r\n\r\n private onEntityRemovedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is removed from the repository.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to invoke when an entity is removed\r\n */\r\n addEntityRemovedObserver = (observer: (removedEntity: T) => void) => {\r\n this.onEntityRemovedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity removal notifications.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityRemovedObserver = (\r\n observer: (removedEntity: T) => void\r\n ): void => {\r\n this.onEntityRemovedObservers.remove(observer);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given ID, false otherwise\r\n */\r\n has = (id: string): boolean => {\r\n return this.entityLookup.has(id);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given AppObject ID, false otherwise\r\n * @deprecated Use has instead\r\n */\r\n hasForAppObject = (appObjectID: string): boolean => {\r\n return this.has(appObjectID);\r\n };\r\n\r\n /**\r\n * Adds an entity to the repository.\r\n *\r\n * If an entity with the same AppObject ID already exists, it is replaced.\r\n * Notifies all registered observers after the entity is added.\r\n *\r\n * @param {T} entity - The entity to add\r\n */\r\n add(entity: T) {\r\n const existing = this.entityLookup.get(entity.appObject.id);\r\n if (existing) {\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n }\r\n\r\n this.entityLookup.set(entity.appObject.id, entity);\r\n entity.addChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityAddedObservers.notify(entity);\r\n }\r\n\r\n /**\r\n * Creates a new entity with the specified or auto-generated ID and adds it to the repository.\r\n *\r\n * This method uses the `entityFactory` method to create the entity instance.\r\n * If no ID is provided, a unique ID is generated automatically.\r\n *\r\n * @param {string} [id] - Optional ID for the entity. If not provided, a unique ID is generated.\r\n * @returns {T} The newly created entity\r\n */\r\n create(id?: string): T {\r\n const idToUse = id ?? generateUniqueID();\r\n const ao = this.appObjects.getOrCreate(idToUse);\r\n const entity = this.entityFactory(ao);\r\n this.add(entity);\r\n return entity;\r\n }\r\n\r\n /**\r\n * Factory method for creating entity instances.\r\n *\r\n * This method must be overridden in derived classes to provide custom entity creation logic.\r\n * It is called by the `create` method to instantiate new entities.\r\n *\r\n * @param {string} id - The ID to use for the new entity\r\n * @returns {T} A new entity instance\r\n * @throws {Error} If not overridden in derived class\r\n */\r\n entityFactory(appObject: AppObject): T {\r\n // Override this method in derived classes to provide entity creation logic\r\n throw new Error(\"Entity factory not implemented.\");\r\n }\r\n\r\n /**\r\n * Removes the entity associated with the specified ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n */\r\n removeById = (id: string) => {\r\n const existing = this.entityLookup.get(id);\r\n if (!existing) return;\r\n\r\n this.entityLookup.delete(id);\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityRemovedObservers.notify(existing);\r\n };\r\n\r\n /**\r\n * Removes the entity associated with the specified AppObject ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n * @deprecated Use removeById instead\r\n */\r\n removeForAppObject = (id: string) => {\r\n this.removeById(id);\r\n };\r\n\r\n /**\r\n * Removes all entities from the repository.\r\n *\r\n * Unsubscribes from all entity change observers and notifies all registered\r\n * removal observers for each entity before clearing the repository.\r\n */\r\n deleteAll = () => {\r\n const entities = Array.from(this.entityLookup.values());\r\n\r\n entities.forEach((entity) => {\r\n entity.removeChangeObserver(this.notifyOnChange);\r\n this.onEntityRemovedObservers.notify(entity);\r\n });\r\n\r\n this.entityLookup.clear();\r\n this.notifyOnChange();\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n */\r\n getById = (id: string): T | undefined => {\r\n return this.entityLookup.get(id);\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n * @deprecated Use getById instead\r\n */\r\n getForAppObject = (appObjectID: string): T | undefined => {\r\n return this.getById(appObjectID);\r\n };\r\n\r\n /**\r\n * Gets all entities in the repository.\r\n *\r\n * @returns {T[]} An array of all entities\r\n */\r\n getAll = (): T[] => {\r\n return Array.from(this.entityLookup.values());\r\n };\r\n\r\n /**\r\n * Gets an entity by ID, or creates it if it doesn't exist.\r\n *\r\n * @param {string} id - The ID of the entity to get or create\r\n * @returns {T} The existing or newly created entity\r\n */\r\n getOrCreate = (id: string): T => {\r\n const existing = this.getById(id);\r\n if (existing) {\r\n return existing;\r\n }\r\n return this.create(id);\r\n };\r\n}\r\n"]}
|
|
@@ -77,12 +77,11 @@ function makeExampleRepo(appObject) {
|
|
|
77
77
|
class ExampleRepoImp extends ExampleRepo {
|
|
78
78
|
/**
|
|
79
79
|
* Factory implementation for creating ExampleEntity instances
|
|
80
|
-
* @param
|
|
80
|
+
* @param appObject The AppObject for the entity
|
|
81
81
|
* @returns A newly created ExampleEntity
|
|
82
82
|
*/
|
|
83
|
-
entityFactory(
|
|
84
|
-
|
|
85
|
-
return (0, ExampleEntity_1.makeExampleEntity)(ao);
|
|
83
|
+
entityFactory(appObject) {
|
|
84
|
+
return (0, ExampleEntity_1.makeExampleEntity)(appObject);
|
|
86
85
|
}
|
|
87
86
|
/**
|
|
88
87
|
* Deletes an ExampleEntity from the repository by its AppObject ID
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExampleRepo.js","sourceRoot":"","sources":["../../../../src/ExampleFeature/Entities/ExampleRepo.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AA6DH,0CAEC;AA7DD,+CAAgF;AAChF,mDAAmE;AAKnE;;;GAGG;AACH,MAAsB,WAAY,SAAQ,+BAAkC;IAO1E;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAC,MAAiB;QAC1B,OAAO,MAAM,CAAC,YAAY,CAAc,IAAI,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CACZ,EAAU,EACV,UAAyB;QAEzB,OAAO,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,YAAY,CAAc,IAAI,CAAC,IAAI,C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EAAE,CAAC;YACb,OAAO,QAAQ,CAAC;QAClB,CAAC;aAAM,CAAC;YACN,OAAO,eAAe,CAAC,SAAS,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;;AAzCH,kCA0CC;AAzCC,gDAAgD;AAChC,gBAAI,GAAG,iBAAiB,CAAC;AA0C3C;;;;GAIG;AACH,SAAgB,eAAe,CAAC,SAAoB;IAClD,OAAO,IAAI,cAAc,CAAC,SAAS,CAAC,CAAC;AACvC,CAAC;AAED;;;GAGG;AACH,MAAM,cAAe,SAAQ,WAAW;IACtC;;;;OAIG;IACH,aAAa,CAAC,SAAoB;QAChC,OAAO,IAAA,iCAAiB,EAAC,SAAS,CAAC,CAAC;IACtC,CAAC;IAED;;;OAGG;IACH,mBAAmB,CAAC,EAAU;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAChC,IAAI,CAAC,MAAM;YAAE,OAAO;QAEpB,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAC3B,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACtB,CAAC;IAED,YAAY,SAAoB;QAC9B,KAAK,CAAC,SAAS,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;CACF","sourcesContent":["/**\r\n * ExampleRepo.ts\r\n *\r\n * This file demonstrates how to implement a repository to manage collections of entities.\r\n * Repositories are responsible for creating, retrieving, and deleting entities.\r\n *\r\n * Key concepts:\r\n * - Repositories extend AppObjectEntityRepo<T> where T is the entity type\r\n * - They provide methods to create and delete entities\r\n * - They manage collections of entities and provide access to them\r\n * - They can use custom entity factories to create specialized entities\r\n *\r\n * Usage pattern:\r\n * 1. Get or create a repository using getById, get, or addIfMissing\r\n * 2. Use the repository to create new entities\r\n * 3. Access entities through the repository's getters\r\n * 4. Delete entities through the repository when they're no longer needed\r\n */\r\n\r\nimport { AppObject, AppObjectEntityRepo, AppObjectRepo } from \"../../AppObject\";\r\nimport { ExampleEntity, makeExampleEntity } from \"./ExampleEntity\";\r\n\r\n/** Type definition for a factory function that creates ExampleEntity instances */\r\nexport type ExampleEntityFactory = (appObject: AppObject) => ExampleEntity;\r\n\r\n/**\r\n * ExampleRepo manages a collection of ExampleEntity instances.\r\n * Abstract class provides the interface and static helper methods.\r\n */\r\nexport abstract class ExampleRepo extends AppObjectEntityRepo<ExampleEntity> {\r\n /** Unique type identifier for this component */\r\n static readonly type = \"ExampleRepoType\";\r\n\r\n /** Deletes an entity by its AppObject ID */\r\n abstract deleteExampleEntity(id: string): void;\r\n\r\n /**\r\n * Retrieves an ExampleRepo component from an AppObject\r\n * @param appObj The AppObject to get the component from\r\n * @returns The ExampleRepo component or undefined if not found\r\n */\r\n static get(appObj: AppObject): ExampleRepo | undefined {\r\n return appObj.getComponent<ExampleRepo>(this.type);\r\n }\r\n\r\n /**\r\n * Retrieves an ExampleRepo by its parent AppObject's ID\r\n * @param id The ID of the parent AppObject\r\n * @param appObjects The AppObjectRepo to search in\r\n * @returns The ExampleRepo component or undefined if not found\r\n */\r\n static getById(\r\n id: string,\r\n appObjects: AppObjectRepo\r\n ): ExampleRepo | undefined {\r\n return appObjects.get(id)?.getComponent<ExampleRepo>(this.type);\r\n }\r\n\r\n /**\r\n * Ensures an ExampleRepo exists on the AppObject, creating one if needed\r\n * @param appObject The AppObject to check/add the component to\r\n * @returns The existing or newly created ExampleRepo\r\n */\r\n static addIfMissing(appObject: AppObject): ExampleRepo {\r\n const existing = appObject.getComponent<ExampleRepo>(ExampleRepo.type);\r\n if (existing) {\r\n return existing;\r\n } else {\r\n return makeExampleRepo(appObject);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Factory function to create a new ExampleRepo\r\n * @param appObject The AppObject to attach the repo to\r\n * @returns A new ExampleRepo instance\r\n */\r\nexport function makeExampleRepo(appObject: AppObject): ExampleRepo {\r\n return new ExampleRepoImp(appObject);\r\n}\r\n\r\n/**\r\n * Concrete implementation of ExampleRepo\r\n * This private class handles the actual implementation details\r\n */\r\nclass ExampleRepoImp extends ExampleRepo {\r\n /**\r\n * Factory implementation for creating ExampleEntity instances\r\n * @param appObject The AppObject for the entity\r\n * @returns A newly created ExampleEntity\r\n */\r\n entityFactory(appObject: AppObject): ExampleEntity {\r\n return makeExampleEntity(appObject);\r\n }\r\n\r\n /**\r\n * Deletes an ExampleEntity from the repository by its AppObject ID\r\n * @param id The ID of the entity's AppObject\r\n */\r\n deleteExampleEntity(id: string): void {\r\n const entity = this.getById(id);\r\n if (!entity) return;\r\n\r\n entity.appObject.dispose();\r\n this.removeById(id);\r\n }\r\n\r\n constructor(appObject: AppObject) {\r\n super(appObject, ExampleRepo.type);\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Controllers
|
|
18
|
+
__exportStar(require("./Controllers/setExampleText"), exports);
|
|
19
|
+
__exportStar(require("./Controllers/toggleExampleBoolean"), exports);
|
|
20
|
+
// Adapters
|
|
21
|
+
__exportStar(require("./Adapters/examplePmAdapter"), exports);
|
|
22
|
+
__exportStar(require("./Adapters/exampleSingletonPmAdapter"), exports);
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/ExampleFeature/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,cAAc;AACd,+DAA6C;AAC7C,qEAAmD;AAEnD,WAAW;AACX,8DAA4C;AAC5C,uEAAqD","sourcesContent":["// Controllers\r\nexport * from \"./Controllers/setExampleText\";\r\nexport * from \"./Controllers/toggleExampleBoolean\";\r\n\r\n// Adapters\r\nexport * from \"./Adapters/examplePmAdapter\";\r\nexport * from \"./Adapters/exampleSingletonPmAdapter\";\r\n"]}
|
|
@@ -162,6 +162,19 @@ export class AppObjectEntityRepo extends AppObjectEntity {
|
|
|
162
162
|
this.getAll = () => {
|
|
163
163
|
return Array.from(this.entityLookup.values());
|
|
164
164
|
};
|
|
165
|
+
/**
|
|
166
|
+
* Gets an entity by ID, or creates it if it doesn't exist.
|
|
167
|
+
*
|
|
168
|
+
* @param {string} id - The ID of the entity to get or create
|
|
169
|
+
* @returns {T} The existing or newly created entity
|
|
170
|
+
*/
|
|
171
|
+
this.getOrCreate = (id) => {
|
|
172
|
+
const existing = this.getById(id);
|
|
173
|
+
if (existing) {
|
|
174
|
+
return existing;
|
|
175
|
+
}
|
|
176
|
+
return this.create(id);
|
|
177
|
+
};
|
|
165
178
|
}
|
|
166
179
|
/**
|
|
167
180
|
* Adds an entity to the repository.
|
|
@@ -192,7 +205,8 @@ export class AppObjectEntityRepo extends AppObjectEntity {
|
|
|
192
205
|
*/
|
|
193
206
|
create(id) {
|
|
194
207
|
const idToUse = id ?? generateUniqueID();
|
|
195
|
-
const
|
|
208
|
+
const ao = this.appObjects.getOrCreate(idToUse);
|
|
209
|
+
const entity = this.entityFactory(ao);
|
|
196
210
|
this.add(entity);
|
|
197
211
|
return entity;
|
|
198
212
|
}
|
|
@@ -206,7 +220,7 @@ export class AppObjectEntityRepo extends AppObjectEntity {
|
|
|
206
220
|
* @returns {T} A new entity instance
|
|
207
221
|
* @throws {Error} If not overridden in derived class
|
|
208
222
|
*/
|
|
209
|
-
entityFactory(
|
|
223
|
+
entityFactory(appObject) {
|
|
210
224
|
// Override this method in derived classes to provide entity creation logic
|
|
211
225
|
throw new Error("Entity factory not implemented.");
|
|
212
226
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AppObjectEntityRepo.js","sourceRoot":"","sources":["../../../src/AppObject/AppObjectEntityRepo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,OAAO,mBAEX,SAAQ,eAAe;IAFzB;;QAGU,iBAAY,GAAG,IAAI,GAAG,EAAa,CAAC;QAEpC,2BAAsB,GAAG,IAAI,YAAY,EAAK,CAAC;QACvD;;;;WAIG;QACH,2BAAsB,GAAG,CAAC,QAAkC,EAAE,EAAE;YAC9D,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CAAC;QAEF;;;;WAIG;QACH,8BAAyB,GAAG,CAAC,QAAkC,EAAQ,EAAE;YACvE,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/C,CAAC,CAAC;QAEM,6BAAwB,GAAG,IAAI,YAAY,EAAK,CAAC;QACzD;;;;WAIG;QACH,6BAAwB,GAAG,CAAC,QAAoC,EAAE,EAAE;YAClE,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9C,CAAC,CAAC;QAEF;;;;WAIG;QACH,gCAA2B,GAAG,CAC5B,QAAo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G,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QAC3C,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAU;QACtB,2EAA2E;QAC3E,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;CA8EF","sourcesContent":["import { ObserverList } from \"../Entities\";\r\nimport { generateUniqueID } from \"../Utilities\";\r\nimport { AppObjectEntity } from \"./AppObjectEntity\";\r\n\r\n/**\r\n * A repository for managing collections of AppObjectEntity instances.\r\n *\r\n * This generic class provides a centralized way to store, retrieve, and manage\r\n * entities that are associated with specific AppObjects. It implements the observer\r\n * pattern to notify listeners when entities are added or removed.\r\n *\r\n * Derived classes should override the `entityFactory` method to provide custom\r\n * entity creation logic for the `create` method.\r\n *\r\n * @template T - The type of entities managed by this repository, must extend AppObjectEntity\r\n * @extends AppObjectEntity\r\n *\r\n * @example\r\n * ```typescript\r\n * // Create a custom repository with entity factory\r\n * class PlayerRepo extends AppObjectEntityRepo<PlayerEntity> {\r\n * entityFactory(id: string): PlayerEntity {\r\n * const appObject = makeAppObject(id, this.appObjects);\r\n * return new PlayerEntity(appObject);\r\n * }\r\n * }\r\n *\r\n * const playerRepo = new PlayerRepo(repoAppObject);\r\n *\r\n * // Create a new player with auto-generated ID\r\n * const player = playerRepo.create();\r\n *\r\n * // Create a new player with specific ID\r\n * const specificPlayer = playerRepo.create(\"player1\");\r\n *\r\n * // Get a player by its ID\r\n * const retrievedPlayer = playerRepo.getById(\"player1\");\r\n * ```\r\n */\r\nexport class AppObjectEntityRepo<\r\n T extends AppObjectEntity,\r\n> extends AppObjectEntity {\r\n private entityLookup = new Map<string, T>();\r\n\r\n private onEntityAddedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is added to the repository.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to invoke when an entity is added\r\n */\r\n addEntityAddedObserver = (observer: (addedEntity: T) => void) => {\r\n this.onEntityAddedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity addition notifications.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityAddedObserver = (observer: (addedEntity: T) => void): void => {\r\n this.onEntityAddedObservers.remove(observer);\r\n };\r\n\r\n private onEntityRemovedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is removed from the repository.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to invoke when an entity is removed\r\n */\r\n addEntityRemovedObserver = (observer: (removedEntity: T) => void) => {\r\n this.onEntityRemovedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity removal notifications.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityRemovedObserver = (\r\n observer: (removedEntity: T) => void\r\n ): void => {\r\n this.onEntityRemovedObservers.remove(observer);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given ID, false otherwise\r\n */\r\n has = (id: string): boolean => {\r\n return this.entityLookup.has(id);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given AppObject ID, false otherwise\r\n * @deprecated Use has instead\r\n */\r\n hasForAppObject = (appObjectID: string): boolean => {\r\n return this.has(appObjectID);\r\n };\r\n\r\n /**\r\n * Adds an entity to the repository.\r\n *\r\n * If an entity with the same AppObject ID already exists, it is replaced.\r\n * Notifies all registered observers after the entity is added.\r\n *\r\n * @param {T} entity - The entity to add\r\n */\r\n add(entity: T) {\r\n const existing = this.entityLookup.get(entity.appObject.id);\r\n if (existing) {\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n }\r\n\r\n this.entityLookup.set(entity.appObject.id, entity);\r\n entity.addChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityAddedObservers.notify(entity);\r\n }\r\n\r\n /**\r\n * Creates a new entity with the specified or auto-generated ID and adds it to the repository.\r\n *\r\n * This method uses the `entityFactory` method to create the entity instance.\r\n * If no ID is provided, a unique ID is generated automatically.\r\n *\r\n * @param {string} [id] - Optional ID for the entity. If not provided, a unique ID is generated.\r\n * @returns {T} The newly created entity\r\n */\r\n create(id?: string): T {\r\n const idToUse = id ?? generateUniqueID();\r\n const entity = this.entityFactory(idToUse);\r\n this.add(entity);\r\n return entity;\r\n }\r\n\r\n /**\r\n * Factory method for creating entity instances.\r\n *\r\n * This method must be overridden in derived classes to provide custom entity creation logic.\r\n * It is called by the `create` method to instantiate new entities.\r\n *\r\n * @param {string} id - The ID to use for the new entity\r\n * @returns {T} A new entity instance\r\n * @throws {Error} If not overridden in derived class\r\n */\r\n entityFactory(id: string): T {\r\n // Override this method in derived classes to provide entity creation logic\r\n throw new Error(\"Entity factory not implemented.\");\r\n }\r\n\r\n /**\r\n * Removes the entity associated with the specified ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n */\r\n removeById = (id: string) => {\r\n const existing = this.entityLookup.get(id);\r\n if (!existing) return;\r\n\r\n this.entityLookup.delete(id);\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityRemovedObservers.notify(existing);\r\n };\r\n\r\n /**\r\n * Removes the entity associated with the specified AppObject ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n * @deprecated Use removeById instead\r\n */\r\n removeForAppObject = (id: string) => {\r\n this.removeById(id);\r\n };\r\n\r\n /**\r\n * Removes all entities from the repository.\r\n *\r\n * Unsubscribes from all entity change observers and notifies all registered\r\n * removal observers for each entity before clearing the repository.\r\n */\r\n deleteAll = () => {\r\n const entities = Array.from(this.entityLookup.values());\r\n\r\n entities.forEach((entity) => {\r\n entity.removeChangeObserver(this.notifyOnChange);\r\n this.onEntityRemovedObservers.notify(entity);\r\n });\r\n\r\n this.entityLookup.clear();\r\n this.notifyOnChange();\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n */\r\n getById = (id: string): T | undefined => {\r\n return this.entityLookup.get(id);\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n * @deprecated Use getById instead\r\n */\r\n getForAppObject = (appObjectID: string): T | undefined => {\r\n return this.getById(appObjectID);\r\n };\r\n\r\n /**\r\n * Gets all entities in the repository.\r\n *\r\n * @returns {T[]} An array of all entities\r\n */\r\n getAll = (): T[] => {\r\n return Array.from(this.entityLookup.values());\r\n };\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"AppObjectEntityRepo.js","sourceRoot":"","sources":["../../../src/AppObject/AppObjectEntityRepo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,OAAO,mBAEX,SAAQ,eAAe;IAFzB;;QAGU,iBAAY,GAAG,IAAI,GAAG,EAAa,CAAC;QAEpC,2BAAsB,GAAG,IAAI,YAAY,EAAK,CAAC;QACvD;;;;WAIG;QACH,2BAAsB,GAAG,CAAC,QAAkC,EAAE,EAAE;YAC9D,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CA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CAAC;IA9IC;;;;;;;OAOG;IACH,GAAG,CAAC,MAAS;QACX,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QAC5D,IAAI,QAAQ,EAAE,CAAC;YACb,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACrD,CAAC;QAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAC9C,IAAI,CAAC,cAAc,EAAE,CAAC;QACtB,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAW;QAChB,MAAM,OAAO,GAAG,EAAE,IAAI,gBAAgB,EAAE,CAAC;QACzC,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;;;;OASG;IACH,aAAa,CAAC,SAAoB;QAChC,2EAA2E;QAC3E,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;CA4FF","sourcesContent":["import { ObserverList } from \"../Entities\";\r\nimport { generateUniqueID } from \"../Utilities\";\r\nimport { AppObject } from \"./AppObject\";\r\nimport { AppObjectEntity } from \"./AppObjectEntity\";\r\n\r\n/**\r\n * A repository for managing collections of AppObjectEntity instances.\r\n *\r\n * This generic class provides a centralized way to store, retrieve, and manage\r\n * entities that are associated with specific AppObjects. It implements the observer\r\n * pattern to notify listeners when entities are added or removed.\r\n *\r\n * Derived classes should override the `entityFactory` method to provide custom\r\n * entity creation logic for the `create` method.\r\n *\r\n * @template T - The type of entities managed by this repository, must extend AppObjectEntity\r\n * @extends AppObjectEntity\r\n *\r\n * @example\r\n * ```typescript\r\n * // Create a custom repository with entity factory\r\n * class PlayerRepo extends AppObjectEntityRepo<PlayerEntity> {\r\n * entityFactory(id: string): PlayerEntity {\r\n * const appObject = makeAppObject(id, this.appObjects);\r\n * return new PlayerEntity(appObject);\r\n * }\r\n * }\r\n *\r\n * const playerRepo = new PlayerRepo(repoAppObject);\r\n *\r\n * // Create a new player with auto-generated ID\r\n * const player = playerRepo.create();\r\n *\r\n * // Create a new player with specific ID\r\n * const specificPlayer = playerRepo.create(\"player1\");\r\n *\r\n * // Get a player by its ID\r\n * const retrievedPlayer = playerRepo.getById(\"player1\");\r\n * ```\r\n */\r\nexport class AppObjectEntityRepo<\r\n T extends AppObjectEntity,\r\n> extends AppObjectEntity {\r\n private entityLookup = new Map<string, T>();\r\n\r\n private onEntityAddedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is added to the repository.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to invoke when an entity is added\r\n */\r\n addEntityAddedObserver = (observer: (addedEntity: T) => void) => {\r\n this.onEntityAddedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity addition notifications.\r\n *\r\n * @param {(addedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityAddedObserver = (observer: (addedEntity: T) => void): void => {\r\n this.onEntityAddedObservers.remove(observer);\r\n };\r\n\r\n private onEntityRemovedObservers = new ObserverList<T>();\r\n /**\r\n * Registers an observer to be notified when an entity is removed from the repository.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to invoke when an entity is removed\r\n */\r\n addEntityRemovedObserver = (observer: (removedEntity: T) => void) => {\r\n this.onEntityRemovedObservers.add(observer);\r\n };\r\n\r\n /**\r\n * Removes an observer previously registered for entity removal notifications.\r\n *\r\n * @param {(removedEntity: T) => void} observer - The callback function to remove\r\n */\r\n removeEntityRemovedObserver = (\r\n observer: (removedEntity: T) => void\r\n ): void => {\r\n this.onEntityRemovedObservers.remove(observer);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given ID, false otherwise\r\n */\r\n has = (id: string): boolean => {\r\n return this.entityLookup.has(id);\r\n };\r\n\r\n /**\r\n * Checks if an entity exists for the given AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {boolean} True if an entity exists for the given AppObject ID, false otherwise\r\n * @deprecated Use has instead\r\n */\r\n hasForAppObject = (appObjectID: string): boolean => {\r\n return this.has(appObjectID);\r\n };\r\n\r\n /**\r\n * Adds an entity to the repository.\r\n *\r\n * If an entity with the same AppObject ID already exists, it is replaced.\r\n * Notifies all registered observers after the entity is added.\r\n *\r\n * @param {T} entity - The entity to add\r\n */\r\n add(entity: T) {\r\n const existing = this.entityLookup.get(entity.appObject.id);\r\n if (existing) {\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n }\r\n\r\n this.entityLookup.set(entity.appObject.id, entity);\r\n entity.addChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityAddedObservers.notify(entity);\r\n }\r\n\r\n /**\r\n * Creates a new entity with the specified or auto-generated ID and adds it to the repository.\r\n *\r\n * This method uses the `entityFactory` method to create the entity instance.\r\n * If no ID is provided, a unique ID is generated automatically.\r\n *\r\n * @param {string} [id] - Optional ID for the entity. If not provided, a unique ID is generated.\r\n * @returns {T} The newly created entity\r\n */\r\n create(id?: string): T {\r\n const idToUse = id ?? generateUniqueID();\r\n const ao = this.appObjects.getOrCreate(idToUse);\r\n const entity = this.entityFactory(ao);\r\n this.add(entity);\r\n return entity;\r\n }\r\n\r\n /**\r\n * Factory method for creating entity instances.\r\n *\r\n * This method must be overridden in derived classes to provide custom entity creation logic.\r\n * It is called by the `create` method to instantiate new entities.\r\n *\r\n * @param {string} id - The ID to use for the new entity\r\n * @returns {T} A new entity instance\r\n * @throws {Error} If not overridden in derived class\r\n */\r\n entityFactory(appObject: AppObject): T {\r\n // Override this method in derived classes to provide entity creation logic\r\n throw new Error(\"Entity factory not implemented.\");\r\n }\r\n\r\n /**\r\n * Removes the entity associated with the specified ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n */\r\n removeById = (id: string) => {\r\n const existing = this.entityLookup.get(id);\r\n if (!existing) return;\r\n\r\n this.entityLookup.delete(id);\r\n existing.removeChangeObserver(this.notifyOnChange);\r\n this.notifyOnChange();\r\n this.onEntityRemovedObservers.notify(existing);\r\n };\r\n\r\n /**\r\n * Removes the entity associated with the specified AppObject ID.\r\n *\r\n * Notifies all registered observers after the entity is removed.\r\n *\r\n * @param {string} id - The ID of the AppObject whose entity should be removed\r\n * @deprecated Use removeById instead\r\n */\r\n removeForAppObject = (id: string) => {\r\n this.removeById(id);\r\n };\r\n\r\n /**\r\n * Removes all entities from the repository.\r\n *\r\n * Unsubscribes from all entity change observers and notifies all registered\r\n * removal observers for each entity before clearing the repository.\r\n */\r\n deleteAll = () => {\r\n const entities = Array.from(this.entityLookup.values());\r\n\r\n entities.forEach((entity) => {\r\n entity.removeChangeObserver(this.notifyOnChange);\r\n this.onEntityRemovedObservers.notify(entity);\r\n });\r\n\r\n this.entityLookup.clear();\r\n this.notifyOnChange();\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified ID.\r\n *\r\n * @param {string} id - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n */\r\n getById = (id: string): T | undefined => {\r\n return this.entityLookup.get(id);\r\n };\r\n\r\n /**\r\n * Gets the entity associated with the specified AppObject ID.\r\n *\r\n * @param {string} appObjectID - The ID of the AppObject\r\n * @returns {T | undefined} The entity if found, undefined otherwise\r\n * @deprecated Use getById instead\r\n */\r\n getForAppObject = (appObjectID: string): T | undefined => {\r\n return this.getById(appObjectID);\r\n };\r\n\r\n /**\r\n * Gets all entities in the repository.\r\n *\r\n * @returns {T[]} An array of all entities\r\n */\r\n getAll = (): T[] => {\r\n return Array.from(this.entityLookup.values());\r\n };\r\n\r\n /**\r\n * Gets an entity by ID, or creates it if it doesn't exist.\r\n *\r\n * @param {string} id - The ID of the entity to get or create\r\n * @returns {T} The existing or newly created entity\r\n */\r\n getOrCreate = (id: string): T => {\r\n const existing = this.getById(id);\r\n if (existing) {\r\n return existing;\r\n }\r\n return this.create(id);\r\n };\r\n}\r\n"]}
|