Skip to content

Commit

Permalink
Avoid builtin page attributes to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Dec 22, 2023
1 parent 8577fb9 commit 8239168
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 48 deletions.
6 changes: 5 additions & 1 deletion plugs/index/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ export async function objectSourceProvider({
}

export async function discoverSources() {
return (await datastore.query({ prefix: [indexKey, "tag"] })).map((
return (await datastore.query({
prefix: [indexKey, "tag"],
select: [{ name: "name" }],
distinct: true,
})).map((
{ value },
) => value.name);
}
1 change: 1 addition & 0 deletions plugs/index/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const builtins: Record<string, Record<string, string>> = {
attributeType: "!string",
type: "!string",
page: "!string",
readOnly: "!boolean",
},
anchor: {
ref: "!string",
Expand Down
10 changes: 5 additions & 5 deletions plugs/index/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
const diagnostics: LintDiagnostic[] = [];
const frontmatter = await extractFrontmatter(tree);
const tags = ["page", ...frontmatter.tags || []];
// Query all readOnly attributes for pages with this tag set
const readOnlyAttributes = await queryObjects<AttributeObject>("attribute", {
filter: ["and", ["=", ["attr", "tag"], [
"array",
frontmatter.tags.map((tag): QueryExpression => ["string", tag]),
tags.map((tag): QueryExpression => ["string", tag]),
]], [
"=",
["attr", "readOnly"],
Expand All @@ -26,7 +27,6 @@ export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
distinct: true,
select: [{ name: "name" }],
});
// console.log("All read only attributes", readOnlyAttributes);
await traverseTreeAsync(tree, async (node) => {
if (node.type === "FrontMatterCode") {
const lintResult = await lintYaml(
Expand Down Expand Up @@ -75,17 +75,17 @@ const errorRegex = /\((\d+):(\d+)\)/;
async function lintYaml(
yamlText: string,
from: number,
disallowedKeys: string[] = [],
readOnlyKeys: string[] = [],
): Promise<LintDiagnostic | undefined> {
try {
const parsed = await YAML.parse(yamlText);
for (const key of disallowedKeys) {
for (const key of readOnlyKeys) {
if (parsed[key]) {
return {
from,
to: from + yamlText.length,
severity: "error",
message: `Disallowed key "${key}"`,
message: `Cannot set read-only attribute "${key}"`,
};
}
}
Expand Down
4 changes: 3 additions & 1 deletion plugs/index/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export async function indexPage({ name, tree }: IndexTreeEvent) {
const toplevelAttributes = await extractAttributes(tree, false);

// Push them all into the page object
pageMeta = { ...pageMeta, ...frontmatter, ...toplevelAttributes };
// Note the order here, making sure that the actual page meta data overrules
// any attempt to manually set built-in attributes like 'name' or 'lastModified'
pageMeta = { ...frontmatter, ...toplevelAttributes, ...pageMeta };

pageMeta.tags = [...new Set(["page", ...pageMeta.tags || []])];

Expand Down
2 changes: 2 additions & 0 deletions plugs/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,7 @@ async function allQuerySources(): Promise<string[]> {
const allObjectTypes: string[] = (await events.dispatchEvent("query_", {}))
.flat();

// console.log("All object types", allObjectTypes);

return [...allSources, ...allObjectTypes];
}
2 changes: 1 addition & 1 deletion server/server_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class ServerSystem {
);

this.listInterval = setInterval(() => {
space.updatePageList().catch(console.error);
space.updatePageListCache().catch(console.error);
}, fileListInterval);

eventHook.addLocalListener("file:changed", (path, localChange) => {
Expand Down
11 changes: 1 addition & 10 deletions web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class Client {
// console.log("Operations", operations);
if (operations > 0) {
// Update the page list
await this.space.updatePageList();
await this.space.updatePageListCache();
}
if (operations !== undefined) {
// "sync:success" is called with a number of operations only from syncSpace(), not from syncing individual pages
Expand Down Expand Up @@ -499,15 +499,6 @@ export class Client {
},
);

// this.eventHook.addLocalListener("file:listed", (fileList: FileMeta[]) => {
// this.ui.viewDispatch({
// type: "update-all-pages",
// pages: fileList.filter(this.space.isListedPage).map(
// fileMetaToPageMeta,
// ),
// });
// });

this.space.watch();

return localSpacePrimitives;
Expand Down
19 changes: 1 addition & 18 deletions web/client_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,6 @@ export class ClientSystem {
}
},
);

// Debugging
// this.eventHook.addLocalListener("file:listed", (files) => {
// console.log("New file list", files);
// });

// this.eventHook.addLocalListener("file:changed", (file) => {
// console.log("File changed", file);
// });

// this.eventHook.addLocalListener("file:created", (file) => {
// console.log("File created", file);
// });

// this.eventHook.addLocalListener("file:deleted", (file) => {
// console.log("File deleted", file);
// });
}

async init() {
Expand Down Expand Up @@ -205,7 +188,7 @@ export class ClientSystem {

async reloadPlugsFromSpace(space: Space) {
console.log("Loading plugs");
await space.updatePageList();
// await space.updatePageList();
await this.system.unloadAll();
console.log("(Re)loading plugs");
await Promise.all((await space.listPlugs()).map(async (plugMeta) => {
Expand Down
4 changes: 4 additions & 0 deletions web/components/page_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function PageNavigator({
}) {
const options: FilterOption[] = [];
for (const pageMeta of allPages) {
// Sanitize the page name
if (!pageMeta.name) {
pageMeta.name = pageMeta.ref;
}
// Order by last modified date in descending order
let orderId = -new Date(pageMeta.lastModified).getTime();
// Unless it was opened in this session
Expand Down
25 changes: 14 additions & 11 deletions web/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const pageWatchInterval = 5000;
export class Space {
imageHeightCache = new LimitedMap<number>(100); // url -> height
widgetHeightCache = new LimitedMap<number>(100); // bodytext -> height
cachedPageList: PageMeta[] = [];

// Note: this is "clean" PageMeta, it doesn't contain custom attributes (it's fetched from the store)
private cachedPageList: PageMeta[] = [];

debouncedImageCacheFlush = throttle(() => {
this.ds.set(["cache", "imageHeight"], this.imageHeightCache).catch(
Expand Down Expand Up @@ -57,7 +59,7 @@ export class Space {
constructor(
readonly spacePrimitives: SpacePrimitives,
private ds: DataStore,
private eventHook: EventHook,
eventHook: EventHook,
) {
// super();
this.ds.batchGet([["cache", "imageHeight"], ["cache", "widgetHeight"]])
Expand All @@ -70,23 +72,25 @@ export class Space {
this.widgetHeightCache = new LimitedMap(100, widgetCache);
}
});
eventHook.addLocalListener("file:listed", (files: FileMeta[]) => {
// console.log("Files listed", files);
this.cachedPageList = files.filter(this.isListedPage).map(
fileMetaToPageMeta,
);
});
// eventHook.addLocalListener("file:listed", (files: FileMeta[]) => {
// // console.log("Files listed", files);
// this.cachedPageList = files.filter(this.isListedPage).map(
// fileMetaToPageMeta,
// );
// });
eventHook.addLocalListener("page:deleted", (pageName: string) => {
if (this.watchedPages.has(pageName)) {
// Stop watching deleted pages already
this.watchedPages.delete(pageName);
}
});
this.updatePageListCache().catch(console.error);
}

public async updatePageList() {
public async updatePageListCache() {
console.log("Updating page list cache");
// This will trigger appropriate events automatically
await this.fetchPageList();
this.cachedPageList = await this.fetchPageList();
}

async deletePage(name: string): Promise<void> {
Expand Down Expand Up @@ -224,7 +228,6 @@ export class Space {
}
});
}, pageWatchInterval);
this.updatePageList().catch(console.error);
}

unwatch() {
Expand Down
2 changes: 1 addition & 1 deletion web/sync_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class NoSyncSyncService implements ISyncService {
start() {
setInterval(() => {
// Trigger a page upload for change events
this.space.updatePageList().catch(console.error);
this.space.updatePageListCache().catch(console.error);
}, spaceSyncInterval);
}

Expand Down

0 comments on commit 8239168

Please sign in to comment.