alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
48 lines (46 loc) • 1.3 kB
JavaScript
import {
pLimit
} from "../../chunks/chunk-QQTYTFWR.js";
import "../../chunks/chunk-U5RRZUYZ.js";
// src/cli/generate/FillCache.ts
import { Database } from "alinea/backend/Database";
import { Emitter, createEmitter } from "alinea/cli/util/Emitter";
import { getCommitSha } from "../util/CommitSha.js";
import { createWatcher } from "../util/Watcher.js";
var dbCache = /* @__PURE__ */ new WeakMap();
async function* fillCache({ watch, rootDir }, localData, store, config, until) {
const db = dbCache.has(config) ? dbCache.get(config) : new Database(config, store);
dbCache.set(config, db);
const limit = pLimit(1);
const commitSha = getCommitSha();
const cache = async () => {
await db.fill(localData, commitSha ?? "", localData);
return db;
};
yield limit(cache);
if (!watch || !localData.watchFiles)
return;
const results = createEmitter();
const stopWatching = await createWatcher({
watchFiles: localData.watchFiles.bind(localData),
async onChange() {
results.emit(limit(cache));
}
});
until.then(() => {
results.cancel();
});
try {
for await (const result of results)
yield result;
} catch (e) {
if (e === Emitter.CANCELLED)
return;
throw e;
} finally {
stopWatching();
}
}
export {
fillCache
};