From 1b2d2470aca58b4c1fda3577f1b71e7819c7f775 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Tue, 1 Mar 2022 07:28:28 +0900 Subject: [PATCH] :sparkles: Delete pages when the callback returns an empty array --- browser/websocket/room.ts | 17 ++++++++++++++--- browser/websocket/shortcuts.ts | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/browser/websocket/room.ts b/browser/websocket/room.ts index c8bf42c..08142b6 100644 --- a/browser/websocket/room.ts +++ b/browser/websocket/room.ts @@ -4,7 +4,7 @@ import { applyCommit } from "./applyCommit.ts"; import { makeChanges } from "./makeChanges.ts"; import { HeadData, pull } from "./pull.ts"; import type { Line } from "../../deps/scrapbox.ts"; -import { pushCommit } from "./_fetch.ts"; +import { pushCommit, pushWithRetry } from "./_fetch.ts"; export type { CommitNotification }; export interface JoinPageRoomResult { @@ -13,7 +13,7 @@ export interface JoinPageRoomResult { * `update()`で現在の本文から書き換え後の本文を作ってもらう。 * serverには書き換え前後の差分だけを送信する * - * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される + * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される */ patch: ( update: (before: Line[], metadata: HeadData) => string[], @@ -74,11 +74,22 @@ export async function joinPageRoom( try { const pending = update(head.lines, head); const newLines = pending instanceof Promise ? await pending : pending; + + if (newLines.length === 0) { + await pushWithRetry(request, [{ deleted: true }], { + projectId, + pageId: head.pageId, + parentId: head.commitId, + userId, + project, + title, + }); + } + const changes = makeChanges(head.lines, newLines, { userId, head, }); - const { commitId } = await pushCommit(request, changes, { parentId: head.commitId, projectId, diff --git a/browser/websocket/shortcuts.ts b/browser/websocket/shortcuts.ts index 12f3fe6..ce55766 100644 --- a/browser/websocket/shortcuts.ts +++ b/browser/websocket/shortcuts.ts @@ -50,7 +50,7 @@ export async function deletePage( * * @param project 書き換えたいページのproject * @param title 書き換えたいページのタイトル - * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される + * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される */ export async function patch( project: string, @@ -78,8 +78,19 @@ export async function patch( try { const pending = update(head.lines, head); const newLines = pending instanceof Promise ? await pending : pending; - const changes = makeChanges(head.lines, newLines, { userId, head }); + if (newLines.length === 0) { + await pushWithRetry(request, [{ deleted: true }], { + projectId, + pageId: head.pageId, + parentId: head.commitId, + userId, + project, + title, + }); + } + + const changes = makeChanges(head.lines, newLines, { userId, head }); await pushCommit(request, changes, { parentId: head.commitId, projectId,