Skip to content

Commit f3d1f19

Browse files
committed
✨ Enable to run deletePage() with an existing socket
1 parent 9e981a7 commit f3d1f19

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

browser/websocket/deletePage.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Socket, socketIO, wrap } from "../../deps/socket.ts";
2+
import { connect, disconnect } from "./socket.ts";
3+
import { getProjectId, getUserId } from "./id.ts";
4+
import { pull } from "./pull.ts";
5+
import { pushWithRetry } from "./_fetch.ts";
6+
7+
export interface DeletePageOptions {
8+
socket?: Socket;
9+
}
10+
11+
/** 指定したページを削除する
12+
*
13+
* @param project 削除したいページのproject
14+
* @param title 削除したいページのタイトル
15+
* @param options 使用したいSocketがあれば指定する
16+
*/
17+
export async function deletePage(
18+
project: string,
19+
title: string,
20+
options?: DeletePageOptions,
21+
): Promise<void> {
22+
const [
23+
{ pageId, commitId: parentId, persistent },
24+
projectId,
25+
userId,
26+
] = await Promise.all([
27+
pull(project, title),
28+
getProjectId(project),
29+
getUserId(),
30+
]);
31+
32+
if (!persistent) return;
33+
34+
const injectedSocket = options?.socket;
35+
const socket = injectedSocket ?? await socketIO();
36+
await connect(socket);
37+
const { request } = wrap(socket);
38+
try {
39+
await pushWithRetry(request, [{ deleted: true }], {
40+
projectId,
41+
pageId,
42+
parentId,
43+
userId,
44+
project,
45+
title,
46+
});
47+
} finally {
48+
if (!injectedSocket) await disconnect(socket);
49+
}
50+
}

browser/websocket/shortcuts.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,6 @@ import { pull } from "./pull.ts";
44
import { pinNumber } from "./pin.ts";
55
import { pushWithRetry } from "./_fetch.ts";
66

7-
/** 指定したページを削除する
8-
*
9-
* @param project 削除したいページのproject
10-
* @param title 削除したいページのタイトル
11-
*/
12-
export async function deletePage(
13-
project: string,
14-
title: string,
15-
): Promise<void> {
16-
const [
17-
{ pageId, commitId: parentId, persistent },
18-
projectId,
19-
userId,
20-
] = await Promise.all([
21-
pull(project, title),
22-
getProjectId(project),
23-
getUserId(),
24-
]);
25-
26-
if (!persistent) return;
27-
28-
const io = await socketIO();
29-
const { request } = wrap(io);
30-
31-
try {
32-
await pushWithRetry(request, [{ deleted: true }], {
33-
projectId,
34-
pageId,
35-
parentId,
36-
userId,
37-
project,
38-
title,
39-
});
40-
} finally {
41-
io.disconnect();
42-
}
43-
}
44-
457
export interface PinOption {
468
/** ピン留め対象のページが存在しないときの振る舞いを変えるoption
479
*

0 commit comments

Comments
 (0)