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
11 changes: 8 additions & 3 deletions browser/websocket/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | undefined>,
) => Promise<void>;
/** ページの更新情報を購読する */
listenPageUpdate: () => AsyncGenerator<CommitNotification, void, unknown>;
Expand Down Expand Up @@ -68,13 +71,15 @@ export async function joinPageRoom(
update: (
before: Line[],
metadata: HeadData,
) => string[] | Promise<string[]>,
) => string[] | undefined | Promise<string[] | undefined>,
) => {
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,
Expand Down
9 changes: 7 additions & 2 deletions browser/websocket/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>,
update: (
lines: Line[],
metadata: HeadData,
) => string[] | undefined | Promise<string[] | undefined>,
): Promise<void> {
const [
head_,
Expand All @@ -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,
Expand Down