Skip to content

Commit 5520624

Browse files
committed
♻️ Use pull for getting HEAD info
1 parent 92aba4a commit 5520624

File tree

5 files changed

+76
-84
lines changed

5 files changed

+76
-84
lines changed

browser/websocket/_fetch.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ProjectUpdatesStreamEvent,
88
wrap,
99
} from "../../deps/socket.ts";
10-
import { getPage } from "../../rest/pages.ts";
10+
import { pull } from "./pull.ts";
1111
export type {
1212
CommitNotification,
1313
ProjectUpdatesStreamCommit,
@@ -61,7 +61,7 @@ export async function pushWithRetry(
6161
} catch (_e) {
6262
console.log("Faild to push a commit. Retry after pulling new commits");
6363
for (let i = 0; i < retry; i++) {
64-
const { commitId } = await ensureEditablePage(project, title);
64+
const { commitId } = await pull(project, title);
6565
parentId = commitId;
6666
try {
6767
const res = await pushCommit(request, changes, {
@@ -79,13 +79,3 @@ export async function pushWithRetry(
7979
}
8080
return parentId;
8181
}
82-
83-
export async function ensureEditablePage(project: string, title: string) {
84-
const result = await getPage(project, title);
85-
86-
// TODO: 編集不可なページはStream購読だけ提供する
87-
if (!result.ok) {
88-
throw new Error(`You have no privilege of editing "/${project}/${title}".`);
89-
}
90-
return result.value;
91-
}

browser/websocket/makeChanges.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@ import {
88
parseToRows,
99
} from "../../deps/scrapbox.ts";
1010
import type { Change } from "../../deps/socket.ts";
11+
import type { HeadData } from "./pull.ts";
1112
import { toTitleLc } from "../../title.ts";
1213

13-
export interface HeadData {
14-
commitId: string;
15-
persistent: boolean;
16-
image: string | null;
17-
linksLc: string[];
18-
lines: Line[];
19-
}
2014
export interface Init {
2115
userId: string;
2216
head: HeadData;

browser/websocket/pull.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Line } from "../../deps/scrapbox.ts";
2+
import { toTitleLc } from "../../title.ts";
3+
import { getPage } from "../../rest/pages.ts";
4+
5+
export interface HeadData {
6+
commitId: string;
7+
pageId: string;
8+
persistent: boolean;
9+
image: string | null;
10+
pin: number;
11+
linksLc: string[];
12+
lines: Line[];
13+
}
14+
export async function pull(project: string, title: string): Promise<HeadData> {
15+
const result = await getPage(project, title);
16+
17+
// TODO: 編集不可なページはStream購読だけ提供する
18+
if (!result.ok) {
19+
throw new Error(`You have no privilege of editing "/${project}/${title}".`);
20+
}
21+
const { commitId, persistent, image, links, lines, id, pin } = result.value;
22+
23+
return {
24+
commitId,
25+
pageId: id,
26+
persistent,
27+
image,
28+
linksLc: links.map((link) => toTitleLc(link)),
29+
pin,
30+
lines,
31+
};
32+
}

browser/websocket/room.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CommitNotification, socketIO, wrap } from "../../deps/socket.ts";
22
import { getProjectId, getUserId } from "./id.ts";
33
import { applyCommit } from "./applyCommit.ts";
4-
import { toTitleLc } from "../../title.ts";
54
import { makeChanges } from "./makeChanges.ts";
5+
import { pull } from "./pull.ts";
66
import type { Line } from "../../deps/scrapbox.ts";
7-
import { ensureEditablePage, pushCommit } from "./_fetch.ts";
7+
import { pushCommit } from "./_fetch.ts";
88
export type { CommitNotification };
99

1010
export interface JoinPageRoomResult {
@@ -34,30 +34,23 @@ export async function joinPageRoom(
3434
title: string,
3535
): Promise<JoinPageRoomResult> {
3636
const [
37-
page,
37+
head_,
3838
projectId,
3939
userId,
4040
] = await Promise.all([
41-
ensureEditablePage(project, title),
41+
pull(project, title),
4242
getProjectId(project),
4343
getUserId(),
4444
]);
4545

4646
// 接続したページの情報
47-
let head = {
48-
persistent: page.persistent,
49-
lines: page.lines,
50-
image: page.image,
51-
commitId: page.commitId,
52-
linksLc: page.links.map((link) => toTitleLc(link)),
53-
};
54-
const pageId = page.id;
47+
let head = head_;
5548

5649
const io = await socketIO();
5750
const { request, response } = wrap(io);
5851
await request("socket.io-request", {
5952
method: "room:join",
60-
data: { projectId, pageId, projectUpdatesStream: false },
53+
data: { projectId, pageId: head.pageId, projectUpdatesStream: false },
6154
});
6255

6356
// subscribe the latest commit
@@ -82,7 +75,7 @@ export async function joinPageRoom(
8275
const { commitId } = await pushCommit(request, changes, {
8376
parentId: head.commitId,
8477
projectId,
85-
pageId,
78+
pageId: head.pageId,
8679
userId,
8780
});
8881

@@ -102,14 +95,7 @@ export async function joinPageRoom(
10295
"Faild to push a commit. Retry after pulling new commits",
10396
);
10497
try {
105-
const page = await ensureEditablePage(project, title);
106-
head = {
107-
persistent: page.persistent,
108-
lines: page.lines,
109-
image: page.image,
110-
commitId: page.commitId,
111-
linksLc: page.links.map((link) => toTitleLc(link)),
112-
};
98+
head = await pull(project, title);
11399
} catch (e: unknown) {
114100
throw e;
115101
}

browser/websocket/shortcuts.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { socketIO, wrap } from "../../deps/socket.ts";
22
import { getProjectId, getUserId } from "./id.ts";
33
import { makeChanges } from "./makeChanges.ts";
4+
import { pull } from "./pull.ts";
45
import { pinNumber } from "./pin.ts";
56
import type { Line } from "../../deps/scrapbox.ts";
6-
import { toTitleLc } from "../../title.ts";
77
import { ensureEditablePage, pushCommit, pushWithRetry } from "./_fetch.ts";
88

99
/** 指定したページを削除する
@@ -59,23 +59,16 @@ export async function patch(
5959
update: (lines: Line[]) => string[] | Promise<string[]>,
6060
): Promise<void> {
6161
const [
62-
page,
62+
head_,
6363
projectId,
6464
userId,
6565
] = await Promise.all([
66-
ensureEditablePage(project, title),
66+
pull(project, title),
6767
getProjectId(project),
6868
getUserId(),
6969
]);
7070

71-
let head = {
72-
persistent: page.persistent,
73-
lines: page.lines,
74-
image: page.image,
75-
commitId: page.commitId,
76-
linksLc: page.links.map((link) => toTitleLc(link)),
77-
};
78-
const pageId = page.id;
71+
let head = head_;
7972

8073
const io = await socketIO();
8174
try {
@@ -91,7 +84,7 @@ export async function patch(
9184
await pushCommit(request, changes, {
9285
parentId: head.commitId,
9386
projectId,
94-
pageId,
87+
pageId: head.pageId,
9588
userId,
9689
});
9790
break;
@@ -103,14 +96,7 @@ export async function patch(
10396
"Faild to push a commit. Retry after pulling new commits",
10497
);
10598
try {
106-
const page = await ensureEditablePage(project, title);
107-
head = {
108-
persistent: page.persistent,
109-
lines: page.lines,
110-
image: page.image,
111-
commitId: page.commitId,
112-
linksLc: page.links.map((link) => toTitleLc(link)),
113-
};
99+
head = await pull(project, title);
114100
} catch (e: unknown) {
115101
throw e;
116102
}
@@ -142,36 +128,37 @@ export async function pin(
142128
option?: PinOption,
143129
): Promise<void> {
144130
const [
145-
{ id: pageId, commitId: initialCommitId, persistent, pin },
131+
head,
146132
projectId,
147133
userId,
148134
] = await Promise.all([
149-
ensureEditablePage(project, title),
135+
pull(project, title),
150136
getProjectId(project),
151137
getUserId(),
152138
]);
153-
let parentId = initialCommitId;
154139

155140
// 既にピン留めされている場合は何もしない
156-
if (pin > 0 || (!persistent && !(option?.create ?? false))) return;
141+
if (head.pin > 0 || (!head.persistent && !(option?.create ?? false))) return;
157142

158-
const init = { projectId, pageId, userId, project, title };
143+
const init = {
144+
parentId: head.commitId,
145+
projectId,
146+
pageId: head.pageId,
147+
userId,
148+
project,
149+
title,
150+
};
159151
const io = await socketIO();
160152
const { request } = wrap(io);
161153

162154
// タイトルのみのページを作る
163-
if (!persistent) {
164-
parentId = await pushWithRetry(request, [{ title }], {
165-
parentId,
166-
...init,
167-
});
155+
if (!head.persistent) {
156+
const commitId = await pushWithRetry(request, [{ title }], init);
157+
init.parentId = commitId;
168158
}
169159

170160
try {
171-
parentId = await pushWithRetry(request, [{ pin: pinNumber() }], {
172-
parentId,
173-
...init,
174-
});
161+
await pushWithRetry(request, [{ pin: pinNumber() }], init);
175162
} finally {
176163
io.disconnect();
177164
}
@@ -186,28 +173,31 @@ export async function unpin(
186173
title: string,
187174
): Promise<void> {
188175
const [
189-
{ id: pageId, commitId: initialCommitId, persistent, pin },
176+
head,
190177
projectId,
191178
userId,
192179
] = await Promise.all([
193-
ensureEditablePage(project, title),
180+
pull(project, title),
194181
getProjectId(project),
195182
getUserId(),
196183
]);
197-
let parentId = initialCommitId;
198184

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

202-
const init = { projectId, pageId, userId, project, title };
188+
const init = {
189+
parentId: head.commitId,
190+
projectId,
191+
pageId: head.pageId,
192+
userId,
193+
project,
194+
title,
195+
};
203196
const io = await socketIO();
204197
const { request } = wrap(io);
205198

206199
try {
207-
parentId = await pushWithRetry(request, [{ pin: 0 }], {
208-
parentId,
209-
...init,
210-
});
200+
await pushWithRetry(request, [{ pin: 0 }], init);
211201
} finally {
212202
io.disconnect();
213203
}

0 commit comments

Comments
 (0)