Skip to content

Commit d7c66d0

Browse files
committed
docs(changeset): Better type safety and minor rename cacheTarget to cacheMode
1 parent 9d7cab0 commit d7c66d0

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

.changeset/slimy-taxis-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@m2d/core": patch
3+
---
4+
5+
Better type safety and minor rename cacheTarget to cacheMode

lib/src/utils/cache.ts

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,55 @@ export const generateCacheKey = async (
159159
return h64ToString(serialized);
160160
};
161161

162+
/**
163+
* Configuration options for the persistent caching utility.
164+
*
165+
* @template TResult - Result type returned by the generator function.
166+
*/
167+
interface BaseCacheConfig<TResult> {
168+
/**
169+
* Keys to exclude when generating a stable cache key from input arguments.
170+
*/
171+
ignoreKeys?: string[];
172+
173+
/**
174+
* In-memory cache object, useful for cache sharing across plugins/tabs.
175+
*/
176+
cache?: Record<string, Promise<TResult>>;
177+
}
178+
179+
interface MemoryOnlyCacheConfig<TResult> extends BaseCacheConfig<TResult> {
180+
/**
181+
* Store only in RAM (fast, temporary). No IndexedDB fallback.
182+
*/
183+
cacheMode: "memory";
184+
185+
/**
186+
* No parallel computation needed since memory cache is synchronous.
187+
*/
188+
parallel?: never;
189+
}
190+
191+
interface IdbOrBothCacheConfig<TResult> extends BaseCacheConfig<TResult> {
192+
/**
193+
* Store in IndexedDB, or both memory and IndexedDB.
194+
*/
195+
cacheMode: "idb" | "both";
196+
197+
/**
198+
* If true, read from cache and compute in parallel.
199+
* Defaults to true.
200+
*/
201+
parallel?: boolean;
202+
}
203+
204+
/**
205+
* Type-safe configuration for createPersistentCache utility.
206+
*/
207+
export type CacheConfigType<TResult> =
208+
| MemoryOnlyCacheConfig<TResult>
209+
| IdbOrBothCacheConfig<TResult>;
210+
162211
/**
163212
* Creates a cached version of an async function with memory and/or persistent caching capabilities.
164213
*
@@ -191,35 +240,30 @@ export const generateCacheKey = async (
191240
* const fetchWithCache = createPersistentCache(fetchJson, "remote-data");
192241
* await fetchWithCache("https://example.com/api/data");
193242
*/
194-
export const createPersistentCache = <Args extends unknown[], Result>(
195-
generator: (...args: Args) => Promise<Result>,
243+
export const createPersistentCache = <TArgs extends unknown[], TResult>(
244+
generator: (...args: TArgs) => Promise<TResult>,
196245
namespace: string,
197-
config?: {
198-
ignoreKeys?: string[];
199-
cache?: Record<string, Promise<Result>>;
200-
cacheTarget?: "idb" | "memory" | "both";
201-
parallel?: boolean;
202-
},
203-
): ((...args: Args) => Promise<Result>) => {
246+
config?: CacheConfigType<TResult>,
247+
): ((...args: TArgs) => Promise<TResult>) => {
204248
const {
205249
ignoreKeys = [],
206-
cache = defaultCache as Record<string, Promise<Result>>,
207-
cacheTarget = "both",
250+
cache = defaultCache as Record<string, Promise<TResult>>,
251+
cacheMode = "both",
208252
parallel = true,
209253
} = config ?? {};
210254

211-
return async (...args: Args): Promise<Result> => {
255+
return async (...args: TArgs): Promise<TResult> => {
212256
const cacheKey = await generateCacheKey(ignoreKeys, ...args);
213257

214-
if (cacheTarget === "memory") return (cache[cacheKey] ??= generator(...args));
258+
if (cacheMode === "memory") return (cache[cacheKey] ??= generator(...args));
215259

216260
const resultPromise = (async () => {
217261
const result = parallel
218262
? await Promise.any([
219-
readFromCache<Result>(cacheKey).then(result => result ?? Promise.reject()),
263+
readFromCache<TResult>(cacheKey).then(result => result ?? Promise.reject()),
220264
generator(...args),
221265
])
222-
: ((await readFromCache<Result>(cacheKey)) ?? (await generator(...args)));
266+
: ((await readFromCache<TResult>(cacheKey)) ?? (await generator(...args)));
223267

224268
const resultsToCache = { id: cacheKey, namespace } as {
225269
id: string;
@@ -234,7 +278,7 @@ export const createPersistentCache = <Args extends unknown[], Result>(
234278
return result;
235279
})();
236280

237-
if (cacheTarget === "both") cache[cacheKey] = resultPromise;
281+
if (cacheMode === "both") cache[cacheKey] = resultPromise;
238282

239283
return resultPromise;
240284
};

scripts/publish.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ if (isPatch) {
4545
);
4646
} catch {}
4747
} else {
48-
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
49-
/** Create new release branch for every Major or Minor release */
50-
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
48+
try {
49+
require("./update-security-md")(`${newMajor}.${newMinor}`, `${oldMajor}.${oldMinor}`);
50+
/** Create new release branch for every Major or Minor release */
51+
execSync(`git checkout -b ${releaseBranch} && git push origin ${releaseBranch}`);
52+
} catch (err) {
53+
console.error("Error updating sec-md or creating release branch", err);
54+
}
5155
}
5256

5357
const { visibility } = JSON.parse(execSync("gh repo view --json visibility").toString());

0 commit comments

Comments
 (0)