From 0bf25a3dc22b11fe20f62212da07ad3a38237fd0 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Tue, 1 Mar 2022 07:58:02 +0900 Subject: [PATCH] :sparkles: Abort patch when the callback returns undefined --- browser/websocket/room.ts | 11 ++++++++--- browser/websocket/shortcuts.ts | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/browser/websocket/room.ts b/browser/websocket/room.ts index 08142b6..1b9d9f2 100644 --- a/browser/websocket/room.ts +++ b/browser/websocket/room.ts @@ -13,10 +13,13 @@ export interface JoinPageRoomResult { * `update()`で現在の本文から書き換え後の本文を作ってもらう。 * serverには書き換え前後の差分だけを送信する * - * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される + * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される。undefinedを返すと書き換えを中断する */ patch: ( - update: (before: Line[], metadata: HeadData) => string[], + update: ( + before: Line[], + metadata: HeadData, + ) => string[] | undefined | Promise, ) => Promise; /** ページの更新情報を購読する */ listenPageUpdate: () => AsyncGenerator; @@ -68,13 +71,15 @@ export async function joinPageRoom( update: ( before: Line[], metadata: HeadData, - ) => string[] | Promise, + ) => string[] | undefined | Promise, ) => { for (let i = 0; i < 3; i++) { try { const pending = update(head.lines, head); const newLines = pending instanceof Promise ? await pending : pending; + if (!newLines) return; + if (newLines.length === 0) { await pushWithRetry(request, [{ deleted: true }], { projectId, diff --git a/browser/websocket/shortcuts.ts b/browser/websocket/shortcuts.ts index ce55766..0070336 100644 --- a/browser/websocket/shortcuts.ts +++ b/browser/websocket/shortcuts.ts @@ -50,12 +50,15 @@ export async function deletePage( * * @param project 書き換えたいページのproject * @param title 書き換えたいページのタイトル - * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される + * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される。undefinedを返すと書き換えを中断する */ export async function patch( project: string, title: string, - update: (lines: Line[], metadata: HeadData) => string[] | Promise, + update: ( + lines: Line[], + metadata: HeadData, + ) => string[] | undefined | Promise, ): Promise { const [ head_, @@ -79,6 +82,8 @@ export async function patch( const pending = update(head.lines, head); const newLines = pending instanceof Promise ? await pending : pending; + if (!newLines) return; + if (newLines.length === 0) { await pushWithRetry(request, [{ deleted: true }], { projectId,