@angular-cool/repository 21.0.2 → 21.0.3

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.
@@ -37,10 +37,10 @@ class ResourceRepository {
37
37
  * Retrieves a resource cache
38
38
  *
39
39
  * @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.
40
- * @return {Resource<TItem | undefined>} A read-only resource containing the item associated with the provided key, already cached.
40
+ * @return {ReloadableSignal<TItem | undefined>} A read-only signal containing the item associated with the provided key, already cached.
41
41
  */
42
42
  get(key) {
43
- return rxResource({
43
+ const resource = rxResource({
44
44
  params: () => key(),
45
45
  stream: ({ params }) => {
46
46
  const cacheKey = this._createCacheKey(params);
@@ -53,7 +53,12 @@ class ResourceRepository {
53
53
  cache.keepDataFreshAsync();
54
54
  return cache.getObservable();
55
55
  }
56
- }).asReadonly();
56
+ });
57
+ const resultSignal = resource.asReadonly().value;
58
+ resultSignal.reload = async () => {
59
+ await this.reload(key());
60
+ };
61
+ return resultSignal;
57
62
  }
58
63
  /**
59
64
  * Reloads the cache associated with the given key, ensuring it is updated with fresh data.
@@ -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 _subject = new ReplaySubject<TItem | undefined>();\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 getObservable() {\n return this._subject.asObservable();\n }\n\n public async keepDataFreshAsync() {\n if (!this.isInvalid) {\n return;\n }\n\n const data = await this.loader();\n\n this._validUntil = Date.now() + this.maxAge;\n this._subject.next(data);\n }\n\n public invalidate() {\n this._validUntil = null;\n }\n}\n","import { Resource, Signal } from '@angular/core';\nimport { rxResource } from '@angular/core/rxjs-interop';\nimport { ResourceCache } from './resource-cache';\n\nconst DEFAULT_MAX_AGE = 5 * 60 * 1000;\n\ntype CacheKey = string;\n\nclass ResourceRepository<TParams, TItem> {\n private _cacheStore = new Map<CacheKey, ResourceCache<TItem>>();\n\n constructor(private options: ResourceRepositoryOptions<TParams, TItem>) {\n }\n\n /**\n * Retrieves a resource cache\n *\n * @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.\n * @return {Resource<TItem | undefined>} A read-only resource containing the item associated with the provided key, already cached.\n */\n public get(key: Signal<TParams>): Resource<TItem | undefined> {\n return 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.getObservable();\n }\n }).asReadonly();\n }\n\n /**\n * Reloads the cache associated with the given key, ensuring it is updated with fresh data.\n *\n * @param {TParams} key - The key used to identify the specific cache entry to reload.\n * @return {Promise<void>} A promise that resolves when the cache has been refreshed.\n * @throws {Error} If no cache is found for the provided key or if data loading fails.\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 private _createCacheKey(params: TParams): CacheKey {\n return (this.options.cacheKeyGenerator ?? JSON.stringify)(params) as CacheKey;\n }\n}\n\n/**\n * Configuration options for a ResourceRepository.\n * This interface provides options for customizing the behavior\n * of resource loading and caching mechanisms.\n *\n * @template TParams The type of the parameters used to load a resource.\n * @template TItem The type of the items being loaded or cached.\n *\n * @property loader A function responsible for loading a resource. It receives\n * parameters of type `TParams` and returns a Promise resolving to an item of type `TItem`.\n *\n * @property [maxAge] Optional. Specifies the maximum duration (in milliseconds)\n * for which a cached resource remains valid. Default: 5 minutes\n *\n * @property [cacheKeyGenerator] Optional. A function used to generate a unique\n * cache key based on the input parameters of type `TParams`. Default: JSON.stringify()\n */\nexport interface ResourceRepositoryOptions<TParams, TItem> {\n loader: (params: TParams) => Promise<TItem>;\n\n maxAge?: number;\n\n cacheKeyGenerator?: (params: TParams) => string;\n}\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 ResourceRepository<TParams, TItem>(options);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAEa,aAAa,CAAA;AAIxB,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;AATR,QAAA,IAAA,CAAA,QAAQ,GAAG,IAAI,aAAa,EAAqB;QACjD,IAAA,CAAA,WAAW,GAAkB,IAAI;IAUzC;IAEO,aAAa,GAAA;AAClB,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE;IACrC;AAEO,IAAA,MAAM,kBAAkB,GAAA;AAC7B,QAAA,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB;QACF;AAEA,QAAA,MAAM,IAAI,GAAG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AMG;IACI,MAAM,MAAM,CAAC,GAAY,EAAA;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC;QAE1C,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC;QAE5C,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,MAAM,IAAI,KAAK,CAAC,qCAAqC,GAAG,CAAA,CAAE,CAAC;QAC7D;QAEA,KAAK,CAAC,UAAU,EAAE;AAElB,QAAA,MAAM,KAAK,CAAC,kBAAkB,EAAE;IAClC;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;AA2BD;;;;;AAKG;AACG,SAAU,kBAAkB,CAAiB,OAAkD,EAAA;AACnG,IAAA,OAAO,IAAI,kBAAkB,CAAiB,OAAO,CAAC;AACxD;;ACxGA;;AAEG;;;;"}
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 _subject = new ReplaySubject<TItem | undefined>();\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 getObservable() {\n return this._subject.asObservable();\n }\n\n public async keepDataFreshAsync() {\n if (!this.isInvalid) {\n return;\n }\n\n const data = await this.loader();\n\n this._validUntil = Date.now() + this.maxAge;\n this._subject.next(data);\n }\n\n public invalidate() {\n this._validUntil = null;\n }\n}\n","import { Signal } from '@angular/core';\nimport { rxResource } from '@angular/core/rxjs-interop';\nimport { ResourceCache } from './resource-cache';\n\nconst DEFAULT_MAX_AGE = 5 * 60 * 1000;\n\ntype CacheKey = string;\n\nexport type ReloadableSignal<T> = Signal<T> & { reload: () => Promise<void> };\n\nclass ResourceRepository<TParams, TItem> {\n private _cacheStore = new Map<CacheKey, ResourceCache<TItem>>();\n\n constructor(private options: ResourceRepositoryOptions<TParams, TItem>) {\n }\n\n /**\n * Retrieves a resource cache\n *\n * @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.\n * @return {ReloadableSignal<TItem | undefined>} A read-only signal containing the item associated with the provided key, already cached.\n */\n public get(key: Signal<TParams>): ReloadableSignal<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.getObservable();\n }\n });\n\n const resultSignal = resource.asReadonly().value as ReloadableSignal<TItem | undefined>;\n\n resultSignal.reload = async () => {\n await this.reload(key());\n };\n\n return resultSignal;\n }\n\n /**\n * Reloads the cache associated with the given key, ensuring it is updated with fresh data.\n *\n * @param {TParams} key - The key used to identify the specific cache entry to reload.\n * @return {Promise<void>} A promise that resolves when the cache has been refreshed.\n * @throws {Error} If no cache is found for the provided key or if data loading fails.\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 private _createCacheKey(params: TParams): CacheKey {\n return (this.options.cacheKeyGenerator ?? JSON.stringify)(params) as CacheKey;\n }\n}\n\n/**\n * Configuration options for a ResourceRepository.\n * This interface provides options for customizing the behavior\n * of resource loading and caching mechanisms.\n *\n * @template TParams The type of the parameters used to load a resource.\n * @template TItem The type of the items being loaded or cached.\n *\n * @property loader A function responsible for loading a resource. It receives\n * parameters of type `TParams` and returns a Promise resolving to an item of type `TItem`.\n *\n * @property [maxAge] Optional. Specifies the maximum duration (in milliseconds)\n * for which a cached resource remains valid. Default: 5 minutes\n *\n * @property [cacheKeyGenerator] Optional. A function used to generate a unique\n * cache key based on the input parameters of type `TParams`. Default: JSON.stringify()\n */\nexport interface ResourceRepositoryOptions<TParams, TItem> {\n loader: (params: TParams) => Promise<TItem>;\n\n maxAge?: number;\n\n cacheKeyGenerator?: (params: TParams) => string;\n}\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 ResourceRepository<TParams, TItem>(options);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAEa,aAAa,CAAA;AAIxB,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;AATR,QAAA,IAAA,CAAA,QAAQ,GAAG,IAAI,aAAa,EAAqB;QACjD,IAAA,CAAA,WAAW,GAAkB,IAAI;IAUzC;IAEO,aAAa,GAAA;AAClB,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE;IACrC;AAEO,IAAA,MAAM,kBAAkB,GAAA;AAC7B,QAA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AAO,KAAK,CAAC,aAAa,EAAE;YAC9B;AACD,SAAA,CAAC;QAEF,MAAM,YAAY,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC,KAA4C;AAEvF,QAAA,YAAY,CAAC,MAAM,GAAG,YAAW;AAC/B,YAAA,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;AAC1B,QAAA,CAAC;AAED,QAAA,OAAO,YAAY;IACrB;AAEA;;;;;;AAMG;IACI,MAAM,MAAM,CAAC,GAAY,EAAA;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC;QAE1C,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC;QAE5C,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,MAAM,IAAI,KAAK,CAAC,qCAAqC,GAAG,CAAA,CAAE,CAAC;QAC7D;QAEA,KAAK,CAAC,UAAU,EAAE;AAElB,QAAA,MAAM,KAAK,CAAC,kBAAkB,EAAE;IAClC;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;AA2BD;;;;;AAKG;AACG,SAAU,kBAAkB,CAAiB,OAAkD,EAAA;AACnG,IAAA,OAAO,IAAI,kBAAkB,CAAiB,OAAO,CAAC;AACxD;;AClHA;;AAEG;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@angular-cool/repository",
3
3
  "description": "Cool stateful signal repository for angular",
4
- "version": "21.0.2",
4
+ "version": "21.0.3",
5
5
  "peerDependencies": {
6
6
  "@angular/common": ">=21.0.0",
7
7
  "@angular/core": ">=21.0.0"
@@ -1,5 +1,8 @@
1
- import { Signal, Resource } from '@angular/core';
1
+ import { Signal } from '@angular/core';
2
2
 
3
+ type ReloadableSignal<T> = Signal<T> & {
4
+ reload: () => Promise<void>;
5
+ };
3
6
  declare class ResourceRepository<TParams, TItem> {
4
7
  private options;
5
8
  private _cacheStore;
@@ -8,9 +11,9 @@ declare class ResourceRepository<TParams, TItem> {
8
11
  * Retrieves a resource cache
9
12
  *
10
13
  * @param {Signal<TParams>} key - A signal function used to resolve the parameters for retrieving or initializing the resource.
11
- * @return {Resource<TItem | undefined>} A read-only resource containing the item associated with the provided key, already cached.
14
+ * @return {ReloadableSignal<TItem | undefined>} A read-only signal containing the item associated with the provided key, already cached.
12
15
  */
13
- get(key: Signal<TParams>): Resource<TItem | undefined>;
16
+ get(key: Signal<TParams>): ReloadableSignal<TItem | undefined>;
14
17
  /**
15
18
  * Reloads the cache associated with the given key, ensuring it is updated with fresh data.
16
19
  *
@@ -52,4 +55,4 @@ interface ResourceRepositoryOptions<TParams, TItem> {
52
55
  declare function resourceRepository<TParams, TItem>(options: ResourceRepositoryOptions<TParams, TItem>): ResourceRepository<TParams, TItem>;
53
56
 
54
57
  export { resourceRepository };
55
- export type { ResourceRepositoryOptions };
58
+ export type { ReloadableSignal, ResourceRepositoryOptions };