Skip to content

Commit 2da8506

Browse files
committed
feat(patch): update()の第2引数から、Pageの全データを取り出せるようにした
1 parent 7243e71 commit 2da8506

File tree

6 files changed

+49
-63
lines changed

6 files changed

+49
-63
lines changed

browser/websocket/_codeBlock.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Change, Socket, wrap } from "../../deps/socket.ts";
2+
import {Page} from "../../deps/scrapbox-rest.ts";
23
import { TinyCodeBlock } from "../../rest/getCodeBlocks.ts";
3-
import { HeadData } from "./pull.ts";
44
import { getProjectId, getUserId } from "./id.ts";
55
import { pushWithRetry } from "./_fetch.ts";
66

@@ -14,7 +14,7 @@ export interface CodeTitle {
1414
/** コミットを送信する一連の処理 */
1515
export const applyCommit = async (
1616
commits: Change[],
17-
head: HeadData,
17+
page: Page,
1818
projectName: string,
1919
pageTitle: string,
2020
socket: Socket,
@@ -26,9 +26,9 @@ export const applyCommit = async (
2626
]);
2727
const { request } = wrap(socket);
2828
return await pushWithRetry(request, commits, {
29-
parentId: head.commitId,
29+
parentId: page.commitId,
3030
projectId: projectId,
31-
pageId: head.pageId,
31+
pageId: page.id,
3232
userId: userId_,
3333
project: projectName,
3434
title: pageTitle,

browser/websocket/deletePage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const deletePage = async (
2020
options?: DeletePageOptions,
2121
): Promise<void> => {
2222
const [
23-
{ pageId, commitId: parentId, persistent },
23+
{ id: pageId, commitId: parentId, persistent },
2424
projectId,
2525
userId,
2626
] = await Promise.all([

browser/websocket/makeChanges.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { diffToChanges } from "./diffToChanges.ts";
2-
import type { Line } from "../../deps/scrapbox.ts";
3-
import { Block, Node, parse } from "../../deps/scrapbox.ts";
2+
import { Block, Line, Node, parse } from "../../deps/scrapbox.ts";
3+
import { Page } from "../../deps/scrapbox-rest.ts";
44
import type { Change } from "../../deps/socket.ts";
5-
import type { HeadData } from "./pull.ts";
65
import { toTitleLc } from "../../title.ts";
76
import { parseYoutube } from "../../parser/youtube.ts";
87

98
export interface Init {
109
userId: string;
11-
head: HeadData;
10+
page: Page;
1211
}
1312
export function* makeChanges(
1413
left: Pick<Line, "text" | "id">[],
1514
right: string[],
16-
{ userId, head }: Init,
15+
{ userId, page }: Init,
1716
): Generator<Change, void, unknown> {
1817
// 改行文字が入るのを防ぐ
1918
const right_ = right.flatMap((text) => text.split("\n"));
@@ -24,7 +23,7 @@ export function* makeChanges(
2423

2524
// titleの差分を入れる
2625
// 空ページの場合もタイトル変更commitを入れる
27-
if (left[0].text !== right_[0] || !head.persistent) {
26+
if (left[0].text !== right_[0] || !page.persistent) {
2827
yield { title: right_[0] };
2928
}
3029

@@ -38,18 +37,18 @@ export function* makeChanges(
3837
// リンクと画像の差分を入れる
3938
const [links, projectLinks, image] = findLinksAndImage(right_.join("\n"));
4039
if (
41-
head.links.length !== links.length ||
42-
!head.links.every((link) => links.includes(link))
40+
page.links.length !== links.length ||
41+
!page.links.every((link) => links.includes(link))
4342
) {
4443
yield { links };
4544
}
4645
if (
47-
head.projectLinks.length !== projectLinks.length ||
48-
!head.projectLinks.every((link) => projectLinks.includes(link))
46+
page.projectLinks.length !== projectLinks.length ||
47+
!page.projectLinks.every((link) => projectLinks.includes(link))
4948
) {
5049
yield { projectLinks };
5150
}
52-
if (head.image !== image) {
51+
if (page.image !== image) {
5352
yield { image };
5453
}
5554
}

browser/websocket/patch.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ import { Socket, socketIO, wrap } from "../../deps/socket.ts";
22
import { connect, disconnect } from "./socket.ts";
33
import { getProjectId, getUserId } from "./id.ts";
44
import { makeChanges } from "./makeChanges.ts";
5-
import { HeadData, pull } from "./pull.ts";
6-
import type { Line } from "../../deps/scrapbox-rest.ts";
5+
import { pull } from "./pull.ts";
6+
import { Line, Page } from "../../deps/scrapbox-rest.ts";
77
import { pushCommit, pushWithRetry } from "./_fetch.ts";
88

99
export interface PatchOptions {
1010
socket?: Socket;
1111
}
1212

13+
export interface PatchMetadata extends Page {
14+
/** 書き換えを再試行した回数
15+
*
16+
* 初回は`0`で、再試行するたびに増える
17+
*/
18+
retry: number;
19+
}
20+
1321
/** ページ全体を書き換える
1422
*
1523
* serverには書き換え前後の差分だけを送信する
@@ -24,12 +32,12 @@ export const patch = async (
2432
title: string,
2533
update: (
2634
lines: Line[],
27-
metadata: HeadData,
35+
metadata: PatchMetadata,
2836
) => string[] | undefined | Promise<string[] | undefined>,
2937
options?: PatchOptions,
3038
): Promise<void> => {
3139
const [
32-
head_,
40+
page_,
3341
projectId,
3442
userId,
3543
] = await Promise.all([
@@ -38,7 +46,7 @@ export const patch = async (
3846
getUserId(),
3947
]);
4048

41-
let head = head_;
49+
let page = page_;
4250

4351
const injectedSocket = options?.socket;
4452
const socket = injectedSocket ?? await socketIO();
@@ -47,43 +55,43 @@ export const patch = async (
4755
const { request } = wrap(socket);
4856

4957
// 3回retryする
50-
for (let i = 0; i < 3; i++) {
58+
for (let retry = 0; retry < 3; retry++) {
5159
try {
52-
const pending = update(head.lines, head);
60+
const pending = update(page.lines, { ...page, retry });
5361
const newLines = pending instanceof Promise ? await pending : pending;
5462

5563
if (!newLines) return;
5664

5765
if (newLines.length === 0) {
5866
await pushWithRetry(request, [{ deleted: true }], {
5967
projectId,
60-
pageId: head.pageId,
61-
parentId: head.commitId,
68+
pageId: page.id,
69+
parentId: page.commitId,
6270
userId,
6371
project,
6472
title,
6573
});
6674
}
6775

6876
const changes = [
69-
...makeChanges(head.lines, newLines, { userId, head }),
77+
...makeChanges(page.lines, newLines, { userId, page }),
7078
];
7179
await pushCommit(request, changes, {
72-
parentId: head.commitId,
80+
parentId: page.commitId,
7381
projectId,
74-
pageId: head.pageId,
82+
pageId: page.id,
7583
userId,
7684
});
7785
break;
7886
} catch (_e: unknown) {
79-
if (i === 2) {
87+
if (retry === 2) {
8088
throw Error("Faild to retry pushing.");
8189
}
8290
console.log(
8391
"Faild to push a commit. Retry after pulling new commits",
8492
);
8593
try {
86-
head = await pull(project, title);
94+
page = await pull(project, title);
8795
} catch (e: unknown) {
8896
throw e;
8997
}

browser/websocket/pin.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const pin = async (
2727
options?: PinOptions,
2828
): Promise<void> => {
2929
const [
30-
head,
30+
page,
3131
projectId,
3232
userId,
3333
] = await Promise.all([
@@ -37,12 +37,12 @@ export const pin = async (
3737
]);
3838

3939
// 既にピン留めされている場合は何もしない
40-
if (head.pin > 0 || (!head.persistent && !(options?.create ?? false))) return;
40+
if (page.pin > 0 || (!page.persistent && !(options?.create ?? false))) return;
4141

4242
const init = {
43-
parentId: head.commitId,
43+
parentId: page.commitId,
4444
projectId,
45-
pageId: head.pageId,
45+
pageId: page.id,
4646
userId,
4747
project,
4848
title,
@@ -53,7 +53,7 @@ export const pin = async (
5353
const { request } = wrap(socket);
5454

5555
// タイトルのみのページを作る
56-
if (!head.persistent) {
56+
if (!page.persistent) {
5757
const commitId = await pushWithRetry(request, [{ title }], init);
5858
init.parentId = commitId;
5959
}
@@ -79,7 +79,7 @@ export const unpin = async (
7979
options: UnPinOptions,
8080
): Promise<void> => {
8181
const [
82-
head,
82+
page,
8383
projectId,
8484
userId,
8585
] = await Promise.all([
@@ -89,12 +89,12 @@ export const unpin = async (
8989
]);
9090

9191
// 既にピンが外れているか、そもそも存在しないページの場合は何もしない
92-
if (head.pin == 0 || !head.persistent) return;
92+
if (page.pin == 0 || !page.persistent) return;
9393

9494
const init = {
95-
parentId: head.commitId,
95+
parentId: page.commitId,
9696
projectId,
97-
pageId: head.pageId,
97+
pageId: page.id,
9898
userId,
9999
project,
100100
title,

browser/websocket/pull.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
1-
import type { Line } from "../../deps/scrapbox-rest.ts";
1+
import { Page } from "../../deps/scrapbox-rest.ts";
22
import { getPage } from "../../rest/pages.ts";
33

4-
export interface HeadData {
5-
commitId: string;
6-
pageId: string;
7-
persistent: boolean;
8-
image: string | null;
9-
pin: number;
10-
links: string[];
11-
projectLinks: string[];
12-
lines: Line[];
13-
}
144
export const pull = async (
155
project: string,
166
title: string,
17-
): Promise<HeadData> => {
7+
): Promise<Page> => {
188
const result = await getPage(project, title);
199

2010
// TODO: 編集不可なページはStream購読だけ提供する
2111
if (!result.ok) {
2212
throw new Error(`You have no privilege of editing "/${project}/${title}".`);
2313
}
24-
const { commitId, persistent, image, links, projectLinks, lines, id, pin } =
25-
result.value;
2614

27-
return {
28-
commitId,
29-
pageId: id,
30-
persistent,
31-
image,
32-
links,
33-
projectLinks,
34-
pin,
35-
lines,
36-
};
15+
return result.value;
3716
};

0 commit comments

Comments
 (0)