diff --git a/server/src/api_server.ts b/server/src/api_server.ts index 56501d15..df3bbce8 100644 --- a/server/src/api_server.ts +++ b/server/src/api_server.ts @@ -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); }); diff --git a/server/src/page_api.ts b/server/src/page_api.ts index 04e357f4..55d41930 100644 --- a/server/src/page_api.ts +++ b/server/src/page_api.ts @@ -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); diff --git a/server/src/syscalls/page_index.ts b/server/src/syscalls/page_index.ts index 27dfa4c4..3056fb4c 100644 --- a/server/src/syscalls/page_index.ts +++ b/server/src/syscalls/page_index.ts @@ -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("page_index") - .where({ page, key }) - .update("value", JSON.stringify(value)); - if (changed === 0) { - await db("page_index").insert({ - page, - key, - value: JSON.stringify(value), - }); - } - }; - return { + const apiObj = { "indexer.clearPageIndexForPage": async (page: string) => { await db("page_index").where({ page }).del(); }, - "indexer.set": setter, + "indexer.set": async (page: string, key: string, value: any) => { + let changed = await db("page_index") + .where({ page, key }) + .update("value", JSON.stringify(value)); + if (changed === 0) { + await db("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("page_index").del(); }, }; + return apiObj; } diff --git a/webapp/src/editor.tsx b/webapp/src/editor.tsx index 066ef429..be7e921a 100644 --- a/webapp/src/editor.tsx +++ b/webapp/src/editor.tsx @@ -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; plugs: Plug[]; - 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 { + async plugCompleter(): Promise { 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 { diff --git a/webapp/src/indexer.ts b/webapp/src/indexer.ts deleted file mode 100644 index ed996a82..00000000 --- a/webapp/src/indexer.ts +++ /dev/null @@ -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); - } - } -} diff --git a/webapp/src/syscalls/space.native.ts b/webapp/src/syscalls/space.native.ts index a2f05dd4..ddc5eae4 100644 --- a/webapp/src/syscalls/space.native.ts +++ b/webapp/src/syscalls/space.native.ts @@ -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 }> => {