2022-04-05 15:02:17 +00:00
|
|
|
import { flashNotification, getCurrentPage, reloadPage, save } from "plugos-silverbullet-syscall/editor";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { invokeFunction } from "plugos-silverbullet-syscall/system";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { parseQuery } from "./engine";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { replaceTemplateVars } from "../core/template";
|
|
|
|
import { queryRegex } from "./util";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { dispatch } from "plugos-syscall/event";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
async function replaceAsync(
|
|
|
|
str: string,
|
|
|
|
regex: RegExp,
|
|
|
|
asyncFn: (match: string, ...args: any[]) => Promise<string>
|
|
|
|
) {
|
|
|
|
const promises: Promise<string>[] = [];
|
|
|
|
str.replace(regex, (match: string, ...args: any[]): string => {
|
|
|
|
const promise = asyncFn(match, ...args);
|
|
|
|
promises.push(promise);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
const data = await Promise.all(promises);
|
|
|
|
return str.replace(regex, () => data.shift()!);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateMaterializedQueriesCommand() {
|
2022-04-01 15:07:08 +00:00
|
|
|
const currentPage = await getCurrentPage();
|
|
|
|
await save();
|
2022-04-05 15:02:17 +00:00
|
|
|
await invokeFunction(
|
|
|
|
"server",
|
|
|
|
"updateMaterializedQueriesOnPage",
|
|
|
|
currentPage
|
|
|
|
);
|
2022-04-01 15:07:08 +00:00
|
|
|
await reloadPage();
|
|
|
|
await flashNotification("Updated materialized queries");
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from client, running on server
|
|
|
|
export async function updateMaterializedQueriesOnPage(pageName: string) {
|
2022-04-01 15:07:08 +00:00
|
|
|
let { text } = await readPage(pageName);
|
2022-04-08 15:46:09 +00:00
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
text = await replaceAsync(
|
|
|
|
text,
|
|
|
|
queryRegex,
|
|
|
|
async (fullMatch, startQuery, query, body, endQuery) => {
|
2022-04-19 14:54:47 +00:00
|
|
|
let parsedQuery = parseQuery(replaceTemplateVars(query, pageName));
|
2022-04-06 13:39:20 +00:00
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
console.log("Parsed query", parsedQuery);
|
2022-04-19 14:54:47 +00:00
|
|
|
// Let's dispatch an event and see what happens
|
|
|
|
let results = await dispatch(
|
|
|
|
`query:${parsedQuery.table}`,
|
|
|
|
{ query: parsedQuery, pageName: pageName },
|
|
|
|
5000
|
|
|
|
);
|
|
|
|
if (results.length === 0) {
|
|
|
|
return `${startQuery}\n${endQuery}`;
|
|
|
|
} else if (results.length === 1) {
|
|
|
|
return `${startQuery}\n${results[0]}\n${endQuery}`;
|
|
|
|
} else {
|
|
|
|
console.error("Too many query results", results);
|
|
|
|
return fullMatch;
|
2022-04-12 11:33:07 +00:00
|
|
|
}
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|
2022-04-12 11:33:07 +00:00
|
|
|
);
|
2022-03-29 15:02:28 +00:00
|
|
|
// console.log("New text", text);
|
2022-04-01 15:07:08 +00:00
|
|
|
await writePage(pageName, text);
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|