@mmstack/resource 20.2.7 → 20.2.9

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/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { HttpResponse, HttpInterceptorFn, HttpContext, HttpResourceOptions, HttpResourceRequest, HttpResourceRef, HttpHeaders } from '@angular/common/http';
2
2
  import { Signal, Injector, Provider, WritableSignal, ValueEqualityFn } from '@angular/core';
3
3
 
4
+ type StoredEntry<T> = Omit<CacheEntry<T>, 'timeout'>;
5
+ type CacheDB<T> = {
6
+ getAll: () => Promise<StoredEntry<T>[]>;
7
+ store: (value: StoredEntry<T>) => Promise<void>;
8
+ remove: (key: string) => Promise<void>;
9
+ };
10
+
4
11
  /**
5
12
  * Options for configuring the Least Recently Used (LRU) cache cleanup strategy.
6
13
  * @internal
@@ -40,6 +47,7 @@ type OldsetCleanupType = {
40
47
  type CacheEntry<T> = {
41
48
  value: T;
42
49
  created: number;
50
+ updated: number;
43
51
  stale: number;
44
52
  useCount: number;
45
53
  expiresAt: number;
@@ -60,8 +68,16 @@ type CleanupType = LRUCleanupType | OldsetCleanupType;
60
68
  declare class Cache<T> {
61
69
  protected readonly ttl: number;
62
70
  protected readonly staleTime: number;
71
+ private readonly db;
63
72
  private readonly internal;
64
73
  private readonly cleanupOpt;
74
+ private readonly id;
75
+ /**
76
+ * Destroys the cache instance, cleaning up any resources used by the cache.
77
+ * This method is called automatically when the cache instance is garbage collected.
78
+ */
79
+ readonly destroy: () => void;
80
+ private readonly broadcast;
65
81
  /**
66
82
  * Creates a new `Cache` instance.
67
83
  *
@@ -70,8 +86,14 @@ declare class Cache<T> {
70
86
  * stale but can still be used while revalidation occurs in the background. Defaults to 1 hour.
71
87
  * @param cleanupOpt - Options for configuring the cache cleanup strategy. Defaults to LRU with a
72
88
  * `maxSize` of 200 and a `checkInterval` of one hour.
73
- */
74
- constructor(ttl?: number, staleTime?: number, cleanupOpt?: Partial<CleanupType>);
89
+ * @param syncTabs - If provided, the cache will use the options a BroadcastChannel to send updates between tabs.
90
+ * Defaults to `undefined`, meaning no synchronization across tabs.
91
+ */
92
+ constructor(ttl?: number, staleTime?: number, cleanupOpt?: Partial<CleanupType>, syncTabs?: {
93
+ id: string;
94
+ serialize: (value: T) => string;
95
+ deserialize: (value: string) => T | null;
96
+ }, db?: Promise<CacheDB<T>>);
75
97
  /** @internal */
76
98
  private getInternal;
77
99
  /**
@@ -112,13 +134,15 @@ declare class Cache<T> {
112
134
  * @param staleTime - (Optional) The stale time for this entry, in milliseconds. Overrides the default `staleTime`.
113
135
  * @param ttl - (Optional) The TTL for this entry, in milliseconds. Overrides the default `ttl`.
114
136
  */
115
- store(key: string, value: T, staleTime?: number, ttl?: number): void;
137
+ store(key: string, value: T, staleTime?: number, ttl?: number, persist?: boolean): void;
138
+ private storeInternal;
116
139
  /**
117
140
  * Invalidates (removes) a cache entry.
118
141
  *
119
142
  * @param key - The key of the entry to invalidate.
120
143
  */
121
144
  invalidate(key: string): void;
145
+ private invalidateInternal;
122
146
  /** @internal */
123
147
  private cleanup;
124
148
  }
@@ -139,6 +163,23 @@ type CacheOptions = {
139
163
  * Options for configuring the cache cleanup strategy.
140
164
  */
141
165
  cleanup?: Partial<CleanupType>;
166
+ /**
167
+ * Whether to synchronize cache across tabs. If true, the cache will use a BroadcastChannel to send updates between tabs.
168
+ */
169
+ syncTabs?: boolean;
170
+ /**
171
+ * Globally disable persistence of cache entries.
172
+ * If set to `false`, cache entries will not be persisted to the database.
173
+ * `true` means, cache entries can be persisted, they must still be opted into on the resource level & allowed by server headers.
174
+ * @default true
175
+ */
176
+ persist?: boolean;
177
+ /**
178
+ * Version of the caches database, increment this if the interfaces change, this will cause the old data to be deleted.
179
+ * Minimum value is 1, so first increment should be 2.
180
+ * @default 1
181
+ */
182
+ version?: number;
142
183
  };
143
184
  /**
144
185
  * Provides the instance of the QueryCache for queryResource. This should probably be called
@@ -409,6 +450,11 @@ type ResourceCacheOptions = true | {
409
450
  * @default false - By default the resource will respect `Cache-Control` headers.
410
451
  */
411
452
  ignoreCacheControl?: boolean;
453
+ /**
454
+ * Whether to persist the cache entry in the local DB instance.
455
+ * @default false - By default, the cache entry is not persisted.
456
+ */
457
+ persist?: boolean;
412
458
  };
413
459
  /**
414
460
  * Options for configuring a `queryResource`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/resource",
3
- "version": "20.2.7",
3
+ "version": "20.2.9",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",
@@ -18,7 +18,7 @@
18
18
  "homepage": "https://github.com/mihajm/mmstack/blob/master/packages/resource",
19
19
  "dependencies": {
20
20
  "uuid": "~11.1.0",
21
- "@mmstack/primitives": "^20.0.2",
21
+ "@mmstack/primitives": "^20.0.3",
22
22
  "@mmstack/object": "^20.0.0",
23
23
  "tslib": "^2.3.0"
24
24
  },