Fix materialized queries and reload

pull/109/head
Zef Hemel 2022-10-10 18:19:08 +02:00
parent 820fc83c8b
commit 61bf715c9f
5 changed files with 31 additions and 22 deletions

View File

@ -86,9 +86,8 @@ export class DiskSpacePrimitives implements SpacePrimitives {
name: string,
encoding: FileEncoding,
data: FileData,
selfUpdate?: boolean,
): Promise<FileMeta> {
let localPath = this.filenameToPath(name);
const localPath = this.filenameToPath(name);
try {
// Ensure parent folder exists
await Deno.mkdir(path.dirname(localPath), { recursive: true });
@ -96,7 +95,7 @@ export class DiskSpacePrimitives implements SpacePrimitives {
// Actually write the file
switch (encoding) {
case "string":
await Deno.writeTextFile(localPath, data as string);
await Deno.writeTextFile(`${localPath}`, data as string);
break;
case "dataurl":
await Deno.writeFile(

View File

@ -96,7 +96,7 @@ export class Space extends EventEmitter<SpaceEvents> {
async getPageMeta(name: string): Promise<PageMeta> {
let oldMeta = this.pageMetaCache.get(name);
let newMeta = fileMetaToPageMeta(
await this.space.getFileMeta(`${name}.md`)
await this.space.getFileMeta(`${name}.md`),
);
if (oldMeta) {
if (oldMeta.lastModified !== newMeta.lastModified) {
@ -111,7 +111,7 @@ export class Space extends EventEmitter<SpaceEvents> {
plug: Plug<any>,
env: string,
name: string,
args: any[]
args: any[],
): Promise<any> {
return this.space.invokeFunction(plug, env, name, args);
}
@ -159,12 +159,12 @@ export class Space extends EventEmitter<SpaceEvents> {
async writePage(
name: string,
text: string,
selfUpdate?: boolean
selfUpdate?: boolean,
): Promise<PageMeta> {
try {
this.saving = true;
let pageMeta = fileMetaToPageMeta(
await this.space.writeFile(`${name}.md`, "string", text, selfUpdate)
const pageMeta = fileMetaToPageMeta(
await this.space.writeFile(`${name}.md`, "string", text, selfUpdate),
);
if (!selfUpdate) {
this.emit("pageChanged", pageMeta);
@ -184,13 +184,13 @@ export class Space extends EventEmitter<SpaceEvents> {
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
return (await this.space.fetchFileList()).filter(
(fileMeta) =>
!fileMeta.name.endsWith(".md") && !fileMeta.name.endsWith(".plug.json")
!fileMeta.name.endsWith(".md") && !fileMeta.name.endsWith(".plug.json"),
);
}
readAttachment(
name: string,
encoding: FileEncoding
encoding: FileEncoding,
): Promise<{ data: FileData; meta: AttachmentMeta }> {
return this.space.readFile(name, encoding);
}
@ -203,7 +203,7 @@ export class Space extends EventEmitter<SpaceEvents> {
name: string,
encoding: FileEncoding,
data: FileData,
selfUpdate?: boolean | undefined
selfUpdate?: boolean | undefined,
): Promise<AttachmentMeta> {
return this.space.writeFile(name, encoding, data, selfUpdate);
}

View File

@ -28,6 +28,7 @@ export async function updateMaterializedQueriesCommand() {
currentPage,
)
) {
console.log("Going reload the page");
await reloadPage();
}
}
@ -35,7 +36,7 @@ export async function updateMaterializedQueriesCommand() {
export const templateInstRegex =
/(<!--\s*#(use|use-verbose|include)\s+\[\[([^\]]+)\]\](.*?)-->)(.+?)(<!--\s*\/\2\s*-->)/gs;
async function updateTemplateInstantiations(
function updateTemplateInstantiations(
text: string,
pageName: string,
): Promise<string> {
@ -81,11 +82,19 @@ async function updateTemplateInstantiations(
);
}
async function cleanTemplateInstantiations(text: string): Promise<string> {
function cleanTemplateInstantiations(text: string): Promise<string> {
return replaceAsync(
text,
templateInstRegex,
async (fullMatch, startInst, type, template, args, body, endInst) => {
(
fullMatch,
startInst,
type,
template,
args,
body,
endInst,
): Promise<string> => {
if (type === "use") {
body = body.replaceAll(
queryRegex,
@ -99,7 +108,7 @@ async function cleanTemplateInstantiations(text: string): Promise<string> {
},
);
}
return `${startInst}${body}${endInst}`;
return Promise.resolve(`${startInst}${body}${endInst}`);
},
);
}
@ -108,6 +117,7 @@ async function cleanTemplateInstantiations(text: string): Promise<string> {
export async function updateMaterializedQueriesOnPage(
pageName: string,
): Promise<boolean> {
// console.log("Updating queries");
let text = "";
try {
text = (await readPage(pageName)).text;

View File

@ -582,12 +582,10 @@ export class Editor {
return actualResult;
}
reloadPage() {
async reloadPage() {
console.log("Reloading page");
safeRun(async () => {
clearTimeout(this.saveTimeout);
await this.loadPage(this.currentPage!);
});
clearTimeout(this.saveTimeout);
await this.loadPage(this.currentPage!);
}
focus() {
@ -614,7 +612,9 @@ export class Editor {
if (previousPage) {
this.saveState(previousPage);
this.space.unwatchPage(previousPage);
await this.save(true);
if (previousPage !== pageName) {
await this.save(true);
}
}
this.viewDispatch({

View File

@ -51,7 +51,7 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
) => {
await editor.navigate(name, pos, replaceState);
},
"editor.reloadPage": async (ctx) => {
"editor.reloadPage": async () => {
await editor.reloadPage();
},
"editor.openUrl": async (ctx, url: string) => {