Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions browser/websocket/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -13,7 +13,7 @@ export interface JoinPageRoomResult {
* `update()`で現在の本文から書き換え後の本文を作ってもらう。
* serverには書き換え前後の差分だけを送信する
*
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される
*/
patch: (
update: (before: Line[], metadata: HeadData) => string[],
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions browser/websocket/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function deletePage(
*
* @param project 書き換えたいページのproject
* @param title 書き換えたいページのタイトル
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される
*/
export async function patch(
project: string,
Expand Down Expand Up @@ -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,
Expand Down