refactor: cached and reloaded file-index

This commit is contained in:
2026-03-13 15:50:50 +08:00
parent 2e04934881
commit 27bc2fc747
4 changed files with 107 additions and 134 deletions
+5 -18
View File
@@ -5,6 +5,7 @@ import {loadElementSrc, resolvePath} from "../utils/path";
import {createStore} from "solid-js/store";
import {RunnerOptions} from "../../yarn-spinner/runtime/runner";
import {getTtrpgFunctions} from "./ttrpgRunner";
import { getIndexedData } from "../../data-loader/file-index";
type YarnSpinnerStore = {
dialogueHistory: (RuntimeResult | OptionsResult['options'][0])[],
@@ -43,8 +44,8 @@ export function createYarnStore(element: HTMLElement, props: {start: string}){
startAt: props.start,
});
const {commands, functions} = getTtrpgFunctions(runner);
runner.registerFunctions(functions);
runner.registerCommands(commands);
runner.registerFunctions(functions);
return runner;
} catch (error) {
console.error('Failed to initialize YarnRunner:', error);
@@ -77,7 +78,7 @@ export function createYarnStore(element: HTMLElement, props: {start: string}){
const processRunnerOutput = () => {
const runner = store.runnerInstance;
if(!runner)return;
const result = runner.currentResult;
if (!result) return;
@@ -100,7 +101,7 @@ export function createYarnStore(element: HTMLElement, props: {start: string}){
});
processRunnerOutput();
};
return {
store,
advance,
@@ -108,22 +109,8 @@ export function createYarnStore(element: HTMLElement, props: {start: string}){
}
}
// 缓存已加载的 yarn 文件内容
const yarnCache = new Map<string, string>();
// 加载 yarn 文件内容
async function loadYarnFile(path: string): Promise<string> {
if (yarnCache.has(path)) {
return yarnCache.get(path)!;
}
const response = await fetch(path);
const content = await response.text();
yarnCache.set(path, content);
return content;
}
// 加载多个 yarn 文件并拼接
async function loadYarnFiles(paths: string[]): Promise<string> {
const contents = await Promise.all(paths.map(path => loadYarnFile(path)));
const contents = await Promise.all(paths.map(path => getIndexedData(path)));
return contents.join('\n');
}