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

View File

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

View File

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

View File

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

View File

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