Skip to content

Commit 38b5941

Browse files
authored
Merge pull request #11 from takker99/refactor-pull
Use `pull` for getting HEAD info
2 parents 92aba4a + c59a411 commit 38b5941

File tree

5 files changed

+80
-89
lines changed

5 files changed

+80
-89
lines changed

browser/websocket/_fetch.ts

+2-12
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

+1-7
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

+32
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

+8-22
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

+37-48
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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";
7-
import { ensureEditablePage, pushCommit, pushWithRetry } from "./_fetch.ts";
7+
import { pushCommit, pushWithRetry } from "./_fetch.ts";
88

99
/** 指定したページを削除する
1010
*
@@ -16,23 +16,22 @@ export async function deletePage(
1616
title: string,
1717
): Promise<void> {
1818
const [
19-
{ id: pageId, commitId: initialCommitId, persistent },
19+
{ pageId, commitId: parentId, persistent },
2020
projectId,
2121
userId,
2222
] = await Promise.all([
23-
ensureEditablePage(project, title),
23+
pull(project, title),
2424
getProjectId(project),
2525
getUserId(),
2626
]);
27-
let parentId = initialCommitId;
2827

2928
if (!persistent) return;
3029

3130
const io = await socketIO();
3231
const { request } = wrap(io);
3332

3433
try {
35-
parentId = await pushWithRetry(request, [{ deleted: true }], {
34+
await pushWithRetry(request, [{ deleted: true }], {
3635
projectId,
3736
pageId,
3837
parentId,
@@ -59,23 +58,16 @@ export async function patch(
5958
update: (lines: Line[]) => string[] | Promise<string[]>,
6059
): Promise<void> {
6160
const [
62-
page,
61+
head_,
6362
projectId,
6463
userId,
6564
] = await Promise.all([
66-
ensureEditablePage(project, title),
65+
pull(project, title),
6766
getProjectId(project),
6867
getUserId(),
6968
]);
7069

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;
70+
let head = head_;
7971

8072
const io = await socketIO();
8173
try {
@@ -91,7 +83,7 @@ export async function patch(
9183
await pushCommit(request, changes, {
9284
parentId: head.commitId,
9385
projectId,
94-
pageId,
86+
pageId: head.pageId,
9587
userId,
9688
});
9789
break;
@@ -103,14 +95,7 @@ export async function patch(
10395
"Faild to push a commit. Retry after pulling new commits",
10496
);
10597
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-
};
98+
head = await pull(project, title);
11499
} catch (e: unknown) {
115100
throw e;
116101
}
@@ -142,36 +127,37 @@ export async function pin(
142127
option?: PinOption,
143128
): Promise<void> {
144129
const [
145-
{ id: pageId, commitId: initialCommitId, persistent, pin },
130+
head,
146131
projectId,
147132
userId,
148133
] = await Promise.all([
149-
ensureEditablePage(project, title),
134+
pull(project, title),
150135
getProjectId(project),
151136
getUserId(),
152137
]);
153-
let parentId = initialCommitId;
154138

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

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

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

170159
try {
171-
parentId = await pushWithRetry(request, [{ pin: pinNumber() }], {
172-
parentId,
173-
...init,
174-
});
160+
await pushWithRetry(request, [{ pin: pinNumber() }], init);
175161
} finally {
176162
io.disconnect();
177163
}
@@ -186,28 +172,31 @@ export async function unpin(
186172
title: string,
187173
): Promise<void> {
188174
const [
189-
{ id: pageId, commitId: initialCommitId, persistent, pin },
175+
head,
190176
projectId,
191177
userId,
192178
] = await Promise.all([
193-
ensureEditablePage(project, title),
179+
pull(project, title),
194180
getProjectId(project),
195181
getUserId(),
196182
]);
197-
let parentId = initialCommitId;
198183

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

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

206198
try {
207-
parentId = await pushWithRetry(request, [{ pin: 0 }], {
208-
parentId,
209-
...init,
210-
});
199+
await pushWithRetry(request, [{ pin: 0 }], init);
211200
} finally {
212201
io.disconnect();
213202
}

0 commit comments

Comments
 (0)