Enabled back-end running of functions, moved indexing to server.

pull/3/head
Zef Hemel 2022-03-18 16:27:40 +01:00
parent 8bff6d98e1
commit d5528fef2a
6 changed files with 41 additions and 122 deletions

View File

@ -70,7 +70,8 @@ export class SocketServer {
this.connectedSockets.delete(socket);
});
socket.on("closePage", (pageName: string) => {
socket.on("page.closePage", (pageName: string) => {
console.log("Client closed page", pageName);
disconnectPageSocket(pageName);
clientConn.openPages.delete(pageName);
});

View File

@ -197,18 +197,21 @@ export class PageApi implements ApiProvider {
);
if (textChanged) {
if (page.saveTimer) {
clearTimeout(page.saveTimer);
}
// Throttle
if (!page.saveTimer) {
page.saveTimer = setTimeout(() => {
if (page) {
console.log("Indexing", pageName);
page.saveTimer = setTimeout(() => {
console.log("This is the time to index a page");
this.system.dispatchEvent("page:index", {
name: pageName,
text: page!.text.sliceString(0),
});
this.flushPageToDisk(pageName, page!);
}, 1000);
this.system.dispatchEvent("page:index", {
name: pageName,
text: page.text.sliceString(0),
});
this.flushPageToDisk(pageName, page);
page.saveTimer = undefined;
}
}, 1000);
}
}
while (page.pending.length) {
page.pending.pop()!(transformedUpdates);

View File

@ -1,4 +1,5 @@
import { Knex } from "knex";
type IndexItem = {
page: string;
key: string;
@ -11,26 +12,25 @@ export type KV = {
};
export default function (db: Knex) {
const setter = async (page: string, key: string, value: any) => {
let changed = await db<IndexItem>("page_index")
.where({ page, key })
.update("value", JSON.stringify(value));
if (changed === 0) {
await db<IndexItem>("page_index").insert({
page,
key,
value: JSON.stringify(value),
});
}
};
return {
const apiObj = {
"indexer.clearPageIndexForPage": async (page: string) => {
await db<IndexItem>("page_index").where({ page }).del();
},
"indexer.set": setter,
"indexer.set": async (page: string, key: string, value: any) => {
let changed = await db<IndexItem>("page_index")
.where({ page, key })
.update("value", JSON.stringify(value));
if (changed === 0) {
await db<IndexItem>("page_index").insert({
page,
key,
value: JSON.stringify(value),
});
}
},
"indexer.batchSet": async (page: string, kvs: KV[]) => {
for (let { key, value } of kvs) {
await setter(page, key, value);
await apiObj["indexer.set"](page, key, value);
}
},
"indexer.get": async (page: string, key: string) => {
@ -79,4 +79,5 @@ export default function (db: Knex) {
return db<IndexItem>("page_index").del();
},
};
return apiObj;
}

View File

@ -10,13 +10,7 @@ import { indentWithTab, standardKeymap } from "@codemirror/commands";
import { history, historyKeymap } from "@codemirror/history";
import { bracketMatching } from "@codemirror/matchbrackets";
import { searchKeymap } from "@codemirror/search";
import {
EditorSelection,
EditorState,
StateField,
Text,
Transaction,
} from "@codemirror/state";
import { EditorSelection, EditorState, Text } from "@codemirror/state";
import {
drawSelection,
dropCursor,
@ -29,18 +23,15 @@ import {
import React, { useEffect, useReducer } from "react";
import ReactDOM from "react-dom";
import { Plug, System } from "../../plugbox/src/runtime";
import { createSandbox } from "../../plugbox/src/webworker_sandbox";
import { createSandbox as createIFrameSandbox } from "../../plugbox/src/iframe_sandbox";
import { AppEvent, AppEventDispatcher, ClickEvent } from "./app_event";
import { collabExtension, CollabDocument } from "./collab";
import { CollabDocument, collabExtension } from "./collab";
import * as commands from "./commands";
import { CommandPalette } from "./components/command_palette";
import { PageNavigator } from "./components/page_navigator";
import { StatusBar } from "./components/status_bar";
import { TopBar } from "./components/top_bar";
import { Cursor } from "./cursorEffect";
import coreManifest from "./generated/core.plug.json";
import { Indexer } from "./indexer";
import { lineWrapper } from "./lineWrapper";
import { markdown } from "./markdown";
import { IPageNavigator, PathPageNavigator } from "./navigator";
@ -61,7 +52,7 @@ import {
NuggetHook,
slashCommandRegexp,
} from "./types";
import { safeRun, throttle } from "./util";
import { safeRun } from "./util";
class PageState {
scrollTop: number;
@ -81,10 +72,8 @@ export class Editor implements AppEventDispatcher {
space: Space;
editorCommands: Map<string, AppCommand>;
plugs: Plug<NuggetHook>[];
indexer: Indexer;
navigationResolve?: (val: undefined) => void;
pageNavigator: IPageNavigator;
indexCurrentPageDebounced: () => any;
constructor(space: Space, parent: Element) {
this.editorCommands = new Map();
@ -102,12 +91,6 @@ export class Editor implements AppEventDispatcher {
parent: document.getElementById("editor")!,
});
this.pageNavigator = new PathPageNavigator();
this.indexer = new Indexer(space);
this.indexCurrentPageDebounced = throttle(
this.indexCurrentPage.bind(this),
2000
);
}
async init() {
@ -228,7 +211,6 @@ export class Editor implements AppEventDispatcher {
}
createEditorState(pageName: string, doc: CollabDocument): EditorState {
const editor = this;
let commandKeyBindings: KeyBinding[] = [];
for (let def of this.editorCommands.values()) {
if (def.command.key) {
@ -311,7 +293,7 @@ export class Editor implements AppEventDispatcher {
{
key: "Ctrl-k",
mac: "Cmd-k",
run: (target): boolean => {
run: (): boolean => {
this.viewDispatch({ type: "start-navigate" });
return true;
},
@ -319,7 +301,7 @@ export class Editor implements AppEventDispatcher {
{
key: "Ctrl-.",
mac: "Cmd-.",
run: (target): boolean => {
run: (): boolean => {
this.viewDispatch({
type: "show-palette",
});
@ -343,22 +325,18 @@ export class Editor implements AppEventDispatcher {
markdown({
base: customMarkDown,
}),
StateField.define({
create: () => null,
update: this.update.bind(this),
}),
],
});
}
reloadPage() {
console.log("Reloading page");
this.loadPage(this.currentPage!);
safeRun(async () => {
await this.loadPage(this.currentPage!);
});
}
async plugCompleter(
ctx: CompletionContext
): Promise<CompletionResult | null> {
async plugCompleter(): Promise<CompletionResult | null> {
let allCompletionResults = await this.dispatchAppEvent("editor:complete");
if (allCompletionResults.length === 1) {
return allCompletionResults[0];
@ -404,26 +382,6 @@ export class Editor implements AppEventDispatcher {
};
}
update(value: null, transaction: Transaction): null {
if (transaction.docChanged) {
this.indexCurrentPageDebounced();
}
return null;
}
private async indexCurrentPage() {
if (this.currentPage) {
console.log("Indexing page", this.currentPage);
await this.indexer.indexPage(
this,
this.currentPage,
this.editorView!.state.sliceDoc(),
true
);
}
}
focus() {
this.editorView!.focus();
}
@ -473,9 +431,6 @@ export class Editor implements AppEventDispatcher {
type: "page-loaded",
name: pageName,
});
// TODO: Check if indexing is required?
await this.indexCurrentPage();
}
ViewComponent(): React.ReactElement {

View File

@ -1,38 +0,0 @@
import { AppEventDispatcher, IndexEvent } from "./app_event";
import { Space } from "./space";
export class Indexer {
space: Space;
constructor(space: Space) {
this.space = space;
}
async indexPage(
appEventDispatcher: AppEventDispatcher,
pageName: string,
text: string,
withFlush: boolean
) {
if (withFlush) {
await this.space.indexDeletePrefixForPage(pageName, "");
}
let indexEvent: IndexEvent = {
name: pageName,
text,
};
// await appEventDispatcher.dispatchAppEvent("page:index", indexEvent);
// await this.setPageIndexPageMeta(pageMeta.name, pageMeta);
}
async reindexSpace(space: Space, appEventDispatcher: AppEventDispatcher) {
let allPages = await space.listPages();
// TODO: Parallelize?
for (let page of allPages) {
await space.indexDeletePrefixForPage(page.name, "");
let pageData = await space.readPage(page.name);
await this.indexPage(appEventDispatcher, page.name, pageData.text, false);
}
}
}

View File

@ -5,9 +5,6 @@ export default (editor: Editor) => ({
"space.listPages": (): PageMeta[] => {
return [...editor.viewState.allPages];
},
"space.reindex": async () => {
await editor.indexer.reindexSpace(editor.space, editor);
},
"space.readPage": async (
name: string
): Promise<{ text: string; meta: PageMeta }> => {