Fix Lua stdlib issues in Sync mode

pull/1212/head^2
Zef Hemel 2025-01-30 14:43:30 +01:00
parent 1e9581baff
commit 548bcfd239
3 changed files with 29 additions and 4 deletions

View File

@ -84,14 +84,14 @@ event.listen {
-- Function defined in Lua
table.insert(options, {
label = key .. "(" .. table.concat(val.body.parameters, ", ") ..")",
apply = key .. "(",
apply = key,
detail = "Lua function"
})
else
-- Builtin
table.insert(options, {
label = key .. "()",
apply = key .. "(",
apply = key,
detail = "Lua built-in"
})
end

View File

@ -1,10 +1,13 @@
import {
asset,
codeWidget,
datastore,
editor,
mq,
system,
} from "@silverbulletmd/silverbullet/syscalls";
import type { FileMeta } from "@silverbulletmd/silverbullet/types";
import { sleep } from "$lib/async.ts";
export async function listFiles(): Promise<FileMeta[]> {
return await asset.listFiles("core");
@ -49,9 +52,11 @@ export async function init() {
}
// Iterate over the current file listing, check if any new files have been added, removed or modified
const newListing = await listFiles();
let anythingChanged = false;
// First check for files that were removed
for (const cachedFile of Object.keys(stdLibCache)) {
if (!newListing.find((f) => f.name === cachedFile)) {
anythingChanged = true;
console.log(`Clearing index for removed file ${cachedFile}`);
await system.invokeFunction("index.clearDSIndex", cachedFile);
delete stdLibCache[cachedFile];
@ -64,7 +69,7 @@ export async function init() {
// Check if file is new or modified compared to cache
if (!stdLibCache[file.name] || stdLibCache[file.name] !== lastModified) {
// console.log(`Queuing for indexing ${file.name}`);
anythingChanged = true;
await system.invokeFunction("index.clearDSIndex", file.name);
await mq.send("indexQueue", file.name);
stdLibCache[file.name] = lastModified;
@ -73,4 +78,21 @@ export async function init() {
// Save updated cache
await datastore.set(stdLibCacheKey, stdLibCache);
// If anything changed and we're in sync mode, we can auto trigger reloading of the system after the index queue is processed
if (anythingChanged && !await system.getEnv()) {
// We're in sync mode
console.log("Waiting for index queue to be processed");
let queueProcessed = false;
while (!queueProcessed) {
const stats = await mq.getQueueStats("indexQueue");
if (stats.queued === 0 && stats.processing === 0) {
queueProcessed = true;
}
await sleep(100);
}
console.log("Index queue processed, reloading system");
await editor.reloadConfigAndCommands();
await codeWidget.refreshAll();
}
}

View File

@ -188,7 +188,9 @@ export class Client implements ConfigContainer {
// Exclude all plug space primitives paths
return !this.plugSpaceRemotePrimitives.isLikelyHandled(path) ||
// Except federated ones
path.startsWith("!");
path.startsWith("!") ||
// Also exclude Library/Std
path.startsWith("Library/Std");
},
)
: new NoSyncSyncService(this.space);
@ -565,6 +567,7 @@ export class Client implements ConfigContainer {
},
);
} else {
// Not in sync mode
localSpacePrimitives = new EventedSpacePrimitives(
this.plugSpaceRemotePrimitives,
this.eventHook,