@angular-cool/repository 21.0.4 → 21.0.6
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
CHANGED
|
@@ -42,7 +42,7 @@ export class MyService {
|
|
|
42
42
|
```typescript
|
|
43
43
|
import { signal } from '@angular/common';
|
|
44
44
|
import { MyService } from './my-service.service';
|
|
45
|
-
import {
|
|
45
|
+
import { ItemDTO, ItemId } from './my-item.model';
|
|
46
46
|
|
|
47
47
|
@Component(/*...*/)
|
|
48
48
|
export class MyComponent {
|
|
@@ -52,10 +52,10 @@ export class MyComponent {
|
|
|
52
52
|
|
|
53
53
|
protected myItem: Resource<ItemDTO | undefined> = this._myService.items.get(this.idParam);
|
|
54
54
|
|
|
55
|
-
protected updateItem() {
|
|
55
|
+
protected async updateItem() {
|
|
56
56
|
// Update item on the server here
|
|
57
57
|
|
|
58
|
-
this.
|
|
58
|
+
await this.myItem.reload(); // This reloads the item from the server and updates the signal for all subscribers
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
```
|
|
@@ -63,7 +63,27 @@ export class MyComponent {
|
|
|
63
63
|
### Manually update value if needed
|
|
64
64
|
|
|
65
65
|
```typescript
|
|
66
|
-
this.
|
|
66
|
+
this.myItem.set(newValue); // Updates the signal for all subscribers
|
|
67
|
+
```
|
|
68
|
+
OR
|
|
69
|
+
```typescript
|
|
70
|
+
await this._myService.items.setValue(this.idParam(), myValue); // Updates the signal for all subscribers
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Manually get value if needed
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const value = await this._myService.items.getValue(this.idParam());
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Manually update value if needed
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
await this._myService.items.updateValue(this.idParam(), val => {
|
|
83
|
+
// modify val
|
|
84
|
+
|
|
85
|
+
return val;
|
|
86
|
+
});
|
|
67
87
|
```
|
|
68
88
|
|
|
69
89
|
## License
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { rxResource } from '@angular/core/rxjs-interop';
|
|
2
|
-
import { ReplaySubject } from 'rxjs';
|
|
2
|
+
import { ReplaySubject, BehaviorSubject, firstValueFrom } from 'rxjs';
|
|
3
3
|
|
|
4
4
|
class ResourceCache {
|
|
5
5
|
get isInvalid() {
|
|
@@ -8,40 +8,57 @@ class ResourceCache {
|
|
|
8
8
|
constructor(loader, maxAge) {
|
|
9
9
|
this.loader = loader;
|
|
10
10
|
this.maxAge = maxAge;
|
|
11
|
-
this.
|
|
11
|
+
this._valueSubject = new ReplaySubject(1);
|
|
12
|
+
this._isLoading = false;
|
|
13
|
+
this._isLoadingSubject = new BehaviorSubject(false);
|
|
12
14
|
this._validUntil = null;
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
return this.
|
|
16
|
+
getValueObservable() {
|
|
17
|
+
return this._valueSubject.asObservable();
|
|
16
18
|
}
|
|
17
19
|
async keepDataFreshAsync() {
|
|
18
20
|
if (!this.isInvalid) {
|
|
19
21
|
return;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
await this._reloadValueAsync();
|
|
24
|
+
}
|
|
25
|
+
async _reloadValueAsync() {
|
|
26
|
+
if (this._isLoading) {
|
|
27
|
+
await firstValueFrom(this._isLoadingSubject);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this._setIsLoading(true);
|
|
31
|
+
try {
|
|
32
|
+
const data = await this.loader();
|
|
33
|
+
await this.setValueAsync(data);
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
this._setIsLoading(false);
|
|
37
|
+
}
|
|
23
38
|
}
|
|
24
39
|
async setValueAsync(value) {
|
|
25
40
|
this._validUntil = Date.now() + this.maxAge;
|
|
26
|
-
this.
|
|
41
|
+
this._valueSubject.next(value);
|
|
42
|
+
}
|
|
43
|
+
async getValueAsync() {
|
|
44
|
+
await this.keepDataFreshAsync();
|
|
45
|
+
return firstValueFrom(this._valueSubject);
|
|
27
46
|
}
|
|
28
47
|
invalidate() {
|
|
29
48
|
this._validUntil = null;
|
|
30
49
|
}
|
|
50
|
+
_setIsLoading(isLoading) {
|
|
51
|
+
this._isLoading = isLoading;
|
|
52
|
+
this._isLoadingSubject.next(isLoading);
|
|
53
|
+
}
|
|
31
54
|
}
|
|
32
55
|
|
|
33
56
|
const DEFAULT_MAX_AGE = 5 * 60 * 1000;
|
|
34
|
-
class
|
|
57
|
+
class ResourceRepositoryImpl {
|
|
35
58
|
constructor(options) {
|
|
36
59
|
this.options = options;
|
|
37
60
|
this._cacheStore = new Map();
|
|
38
61
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Retrieves a resource cache
|
|
41
|
-
*
|
|
42
|
-
* @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.
|
|
43
|
-
* @return {ReloadableSignal<TItem | undefined>} A read-only signal containing the item associated with the provided key, already cached.
|
|
44
|
-
*/
|
|
45
62
|
get(key) {
|
|
46
63
|
const resource = rxResource({
|
|
47
64
|
params: () => key(),
|
|
@@ -54,22 +71,18 @@ class ResourceRepository {
|
|
|
54
71
|
}
|
|
55
72
|
// Do not wait for it to load
|
|
56
73
|
cache.keepDataFreshAsync();
|
|
57
|
-
return cache.
|
|
74
|
+
return cache.getValueObservable();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return Object.assign(resource.asReadonly().value, {
|
|
78
|
+
reload: async () => {
|
|
79
|
+
await this.reload(key());
|
|
80
|
+
},
|
|
81
|
+
set: async (value) => {
|
|
82
|
+
await this.setValue(key(), value);
|
|
58
83
|
}
|
|
59
84
|
});
|
|
60
|
-
|
|
61
|
-
resultSignal.reload = async () => {
|
|
62
|
-
await this.reload(key());
|
|
63
|
-
};
|
|
64
|
-
return resultSignal;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Reloads the cache associated with the given key, ensuring it is updated with fresh data.
|
|
68
|
-
*
|
|
69
|
-
* @param {TParams} key - The key used to identify the specific cache entry to reload.
|
|
70
|
-
* @return {Promise<void>} A promise that resolves when the cache has been refreshed.
|
|
71
|
-
* @throws {Error} If no cache is found for the provided key or if data loading fails.
|
|
72
|
-
*/
|
|
85
|
+
}
|
|
73
86
|
async reload(key) {
|
|
74
87
|
const cacheKey = this._createCacheKey(key);
|
|
75
88
|
const cache = this._cacheStore.get(cacheKey);
|
|
@@ -79,13 +92,14 @@ class ResourceRepository {
|
|
|
79
92
|
cache.invalidate();
|
|
80
93
|
await cache.keepDataFreshAsync();
|
|
81
94
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
async getValue(key) {
|
|
96
|
+
const cacheKey = this._createCacheKey(key);
|
|
97
|
+
const cache = this._cacheStore.get(cacheKey);
|
|
98
|
+
if (!cache) {
|
|
99
|
+
throw new Error(`No cache found for the given key: ${key}`);
|
|
100
|
+
}
|
|
101
|
+
return await cache.getValueAsync();
|
|
102
|
+
}
|
|
89
103
|
async setValue(key, value) {
|
|
90
104
|
const cacheKey = this._createCacheKey(key);
|
|
91
105
|
let cache = this._cacheStore.get(cacheKey);
|
|
@@ -94,10 +108,15 @@ class ResourceRepository {
|
|
|
94
108
|
}
|
|
95
109
|
await cache.setValueAsync(value);
|
|
96
110
|
}
|
|
111
|
+
async updateValue(key, updateFn) {
|
|
112
|
+
const value = await this.getValue(key);
|
|
113
|
+
await this.setValue(key, await updateFn(value));
|
|
114
|
+
}
|
|
97
115
|
_createCacheKey(params) {
|
|
98
116
|
return (this.options.cacheKeyGenerator ?? JSON.stringify)(params);
|
|
99
117
|
}
|
|
100
118
|
}
|
|
119
|
+
|
|
101
120
|
/**
|
|
102
121
|
* Creates a new instance of a ResourceRepository with the specified options.
|
|
103
122
|
*
|
|
@@ -105,7 +124,7 @@ class ResourceRepository {
|
|
|
105
124
|
* @return {ResourceRepository<TParams, TItem>} A new instance of ResourceRepository configured with the given options.
|
|
106
125
|
*/
|
|
107
126
|
function resourceRepository(options) {
|
|
108
|
-
return new
|
|
127
|
+
return new ResourceRepositoryImpl(options);
|
|
109
128
|
}
|
|
110
129
|
|
|
111
130
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"angular-cool-repository.mjs","sources":["../../../projects/repository/src/lib/resource-cache.ts","../../../projects/repository/src/lib/resource-repository.ts","../../../projects/repository/src/angular-cool-repository.ts"],"sourcesContent":["import { ReplaySubject } from 'rxjs';\n\nexport class ResourceCache<TItem> {\n private
|
|
1
|
+
{"version":3,"file":"angular-cool-repository.mjs","sources":["../../../projects/repository/src/lib/resource-cache.ts","../../../projects/repository/src/lib/resource-repository.ts","../../../projects/repository/src/public-api.ts","../../../projects/repository/src/angular-cool-repository.ts"],"sourcesContent":["import { BehaviorSubject, firstValueFrom, ReplaySubject } from 'rxjs';\n\nexport class ResourceCache<TItem> {\n private _valueSubject = new ReplaySubject<TItem | undefined>(1);\n\n private _isLoading = false;\n private _isLoadingSubject = new BehaviorSubject<boolean>(false);\n\n private _validUntil: number | null = null;\n\n public get isInvalid(): boolean {\n return this._validUntil === null || Date.now() > this._validUntil;\n }\n\n constructor(\n private loader: () => Promise<TItem | undefined>,\n private maxAge: number,\n ) {\n }\n\n public getValueObservable() {\n return this._valueSubject.asObservable();\n }\n\n public async keepDataFreshAsync() {\n if (!this.isInvalid) {\n return;\n }\n\n await this._reloadValueAsync();\n }\n\n private async _reloadValueAsync(): Promise<void> {\n if (this._isLoading) {\n await firstValueFrom(this._isLoadingSubject);\n\n return;\n }\n\n this._setIsLoading(true);\n\n try {\n const data = await this.loader();\n\n await this.setValueAsync(data);\n } finally {\n this._setIsLoading(false);\n }\n }\n\n public async setValueAsync(value: TItem | undefined) {\n this._validUntil = Date.now() + this.maxAge;\n this._valueSubject.next(value);\n }\n\n public async getValueAsync(): Promise<TItem | undefined> {\n await this.keepDataFreshAsync();\n\n return firstValueFrom(this._valueSubject);\n }\n\n public invalidate() {\n this._validUntil = null;\n }\n\n private _setIsLoading(isLoading: boolean) {\n this._isLoading = isLoading;\n this._isLoadingSubject.next(isLoading);\n }\n}\n","import { Signal } from '@angular/core';\nimport { rxResource } from '@angular/core/rxjs-interop';\nimport { ResourceCache } from './resource-cache';\nimport { ResourceRepositorySignal } from '../interface/resource-repository-signal.interface';\nimport { ResourceRepository, ResourceRepositoryOptions } from '../interface/resource-repository.interface';\n\nconst DEFAULT_MAX_AGE = 5 * 60 * 1000;\n\ntype CacheKey = string;\n\nexport class ResourceRepositoryImpl<TParams, TItem> implements ResourceRepository<TParams, TItem> {\n private _cacheStore = new Map<CacheKey, ResourceCache<TItem>>();\n\n constructor(private options: ResourceRepositoryOptions<TParams, TItem>) {\n }\n\n public get(key: Signal<TParams>): ResourceRepositorySignal<TItem | undefined> {\n const resource = rxResource<TItem | undefined, TParams>({\n params: () => key(),\n stream: ({ params }) => {\n const cacheKey = this._createCacheKey(params);\n\n let cache = this._cacheStore.get(cacheKey);\n\n if (!cache) {\n cache = new ResourceCache(\n ((p) => () => this.options.loader(p))(params),\n this.options.maxAge ?? DEFAULT_MAX_AGE,\n );\n\n this._cacheStore.set(cacheKey, cache);\n }\n\n // Do not wait for it to load\n cache.keepDataFreshAsync();\n\n return cache.getValueObservable();\n }\n });\n\n return Object.assign(resource.asReadonly().value as ResourceRepositorySignal<TItem | undefined>, {\n reload: async () => {\n await this.reload(key());\n },\n set: async (value: TItem | undefined) => {\n await this.setValue(key(), value as TItem);\n }\n });\n }\n\n public async reload(key: TParams): Promise<void> {\n const cacheKey = this._createCacheKey(key);\n\n const cache = this._cacheStore.get(cacheKey);\n\n if (!cache) {\n throw new Error(`No cache found for the given key: ${key}`);\n }\n\n cache.invalidate();\n\n await cache.keepDataFreshAsync();\n }\n\n public async getValue(key: TParams): Promise<TItem | undefined> {\n const cacheKey = this._createCacheKey(key);\n\n const cache = this._cacheStore.get(cacheKey);\n\n if (!cache) {\n throw new Error(`No cache found for the given key: ${key}`);\n }\n\n return await cache.getValueAsync();\n }\n\n public async setValue(key: TParams, value: TItem): Promise<void> {\n const cacheKey = this._createCacheKey(key);\n\n let cache = this._cacheStore.get(cacheKey);\n\n if (!cache) {\n throw new Error(`No cache found for the given key: ${key}`);\n }\n\n await cache.setValueAsync(value);\n }\n\n public async updateValue(key: TParams, updateFn: (value: TItem | undefined) => Promise<TItem>): Promise<void> {\n const value = await this.getValue(key);\n\n await this.setValue(key, await updateFn(value));\n }\n\n private _createCacheKey(params: TParams): CacheKey {\n return (this.options.cacheKeyGenerator ?? JSON.stringify)(params) as CacheKey;\n }\n}\n","import { ResourceRepository, ResourceRepositoryOptions } from './interface/resource-repository.interface';\nimport { ResourceRepositoryImpl } from './lib/resource-repository';\n\nexport * from './interface/resource-repository-signal.interface';\nexport * from './interface/resource-repository.interface';\n\n/**\n * Creates a new instance of a ResourceRepository with the specified options.\n *\n * @param {ResourceRepositoryOptions<TParams, TItem>} options - The configuration options for the resource repository.\n * @return {ResourceRepository<TParams, TItem>} A new instance of ResourceRepository configured with the given options.\n */\nexport function resourceRepository<TParams, TItem>(options: ResourceRepositoryOptions<TParams, TItem>): ResourceRepository<TParams, TItem> {\n return new ResourceRepositoryImpl<TParams, TItem>(options);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAEa,aAAa,CAAA;AAQxB,IAAA,IAAW,SAAS,GAAA;AAClB,QAAA,OAAO,IAAI,CAAC,WAAW,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,WAAW;IACnE;IAEA,WAAA,CACU,MAAwC,EACxC,MAAc,EAAA;QADd,IAAA,CAAA,MAAM,GAAN,MAAM;QACN,IAAA,CAAA,MAAM,GAAN,MAAM;AAbR,QAAA,IAAA,CAAA,aAAa,GAAG,IAAI,aAAa,CAAoB,CAAC,CAAC;QAEvD,IAAA,CAAA,UAAU,GAAG,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,CAAC,GAAG,CAAC;AAEtC,QAAA,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,QAAQ,CAAC,KAAK,CAAC,CAAC;IACjD;AAEQ,IAAA,eAAe,CAAC,MAAe,EAAA;AACrC,QAAA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,iBAAiB,IAAI,IAAI,CAAC,SAAS,EAAE,MAAM,CAAa;IAC/E;AACD;;AC3FD;;;;;AAKG;AACG,SAAU,kBAAkB,CAAiB,OAAkD,EAAA;AACnG,IAAA,OAAO,IAAI,sBAAsB,CAAiB,OAAO,CAAC;AAC5D;;ACdA;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import { Signal } from '@angular/core';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
interface ResourceRepositorySignal<T> extends Signal<T> {
|
|
4
|
+
/**
|
|
5
|
+
* Reloads or refreshes the current value.
|
|
6
|
+
*/
|
|
7
|
+
readonly reload: () => Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Sets current value.
|
|
10
|
+
*/
|
|
11
|
+
readonly set: (value: T) => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ResourceRepository<TParams, TItem> {
|
|
10
15
|
/**
|
|
11
16
|
* Retrieves a resource cache
|
|
12
17
|
*
|
|
13
18
|
* @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.
|
|
14
|
-
* @return {
|
|
19
|
+
* @return {ResourceRepositorySignal<TItem | undefined>} A read-only signal containing the item associated with the provided key, already cached.
|
|
15
20
|
*/
|
|
16
|
-
get(key: Signal<TParams>):
|
|
21
|
+
get(key: Signal<TParams>): ResourceRepositorySignal<TItem | undefined>;
|
|
17
22
|
/**
|
|
18
23
|
* Reloads the cache associated with the given key, ensuring it is updated with fresh data.
|
|
19
24
|
*
|
|
@@ -23,14 +28,30 @@ declare class ResourceRepository<TParams, TItem> {
|
|
|
23
28
|
*/
|
|
24
29
|
reload(key: TParams): Promise<void>;
|
|
25
30
|
/**
|
|
26
|
-
*
|
|
31
|
+
* Retrieves a value from the cache associated with the specified key.
|
|
32
|
+
*
|
|
33
|
+
* @param {TParams} key - The key used to identify the cache entry.
|
|
34
|
+
* @return {Promise<TItem | undefined>} A promise that resolves with the retrieved value
|
|
35
|
+
* or undefined if the value does not exist.
|
|
36
|
+
* @throws {Error} If no cache is found for the given key.
|
|
37
|
+
*/
|
|
38
|
+
getValue(key: TParams): Promise<TItem | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* Sets a value in the cache associated with the given key and update all value subscribers
|
|
27
41
|
*
|
|
28
42
|
* @param {TParams} key - The unique key used to identify the cache.
|
|
29
|
-
* @param {TItem} value - The value to be stored in the cache
|
|
43
|
+
* @param {TItem} value - The value to be stored in the cache
|
|
30
44
|
* @return {Promise<void>} A promise that resolves when the value is successfully set.
|
|
31
45
|
*/
|
|
32
46
|
setValue(key: TParams, value: TItem): Promise<void>;
|
|
33
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Updates the value associated with the given key by applying an update function and update all value subscribers
|
|
49
|
+
*
|
|
50
|
+
* @param {TParams} key - The key whose value needs to be updated.
|
|
51
|
+
* @param {function(value: TItem | undefined): Promise<TItem>} updateFn - An asynchronous function that takes the current value (or undefined if not present) and returns the updated value.
|
|
52
|
+
* @return {Promise<void>} A promise that resolves when the update operation is complete.
|
|
53
|
+
*/
|
|
54
|
+
updateValue(key: TParams, updateFn: (value: TItem | undefined) => Promise<TItem>): Promise<void>;
|
|
34
55
|
}
|
|
35
56
|
/**
|
|
36
57
|
* Configuration options for a ResourceRepository.
|
|
@@ -54,6 +75,7 @@ interface ResourceRepositoryOptions<TParams, TItem> {
|
|
|
54
75
|
maxAge?: number;
|
|
55
76
|
cacheKeyGenerator?: (params: TParams) => string;
|
|
56
77
|
}
|
|
78
|
+
|
|
57
79
|
/**
|
|
58
80
|
* Creates a new instance of a ResourceRepository with the specified options.
|
|
59
81
|
*
|
|
@@ -63,4 +85,4 @@ interface ResourceRepositoryOptions<TParams, TItem> {
|
|
|
63
85
|
declare function resourceRepository<TParams, TItem>(options: ResourceRepositoryOptions<TParams, TItem>): ResourceRepository<TParams, TItem>;
|
|
64
86
|
|
|
65
87
|
export { resourceRepository };
|
|
66
|
-
export type {
|
|
88
|
+
export type { ResourceRepository, ResourceRepositoryOptions, ResourceRepositorySignal };
|