Skip to content

Commit 9056916

Browse files
committed
✨ Enable to run pin() with an existing socket
1 parent f3d1f19 commit 9056916

File tree

3 files changed

+115
-102
lines changed

3 files changed

+115
-102
lines changed

browser/websocket/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./room.ts";
2-
export * from "./shortcuts.ts";
32
export * from "./patch.ts";
3+
export * from "./deletePage.ts";
4+
export * from "./pin.ts";
45
export * from "./stream.ts";

browser/websocket/pin.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,115 @@
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 PinOptions {
8+
/** ピン留め対象のページが存在しないときの振る舞いを変えるoption
9+
*
10+
* -`true`: タイトルのみのページを作成してピン留めする
11+
* - `false`: ピン留めしない
12+
*
13+
* @default false
14+
*/
15+
create?: boolean;
16+
socket?: Socket;
17+
}
18+
/** 指定したページをピン留めする
19+
*
20+
* @param project ピン留めしたいページのproject
21+
* @param title ピン留めしたいページのタイトル
22+
* @param options 使用したいSocketがあれば指定する
23+
*/
24+
export async function pin(
25+
project: string,
26+
title: string,
27+
options?: PinOptions,
28+
): Promise<void> {
29+
const [
30+
head,
31+
projectId,
32+
userId,
33+
] = await Promise.all([
34+
pull(project, title),
35+
getProjectId(project),
36+
getUserId(),
37+
]);
38+
39+
// 既にピン留めされている場合は何もしない
40+
if (head.pin > 0 || (!head.persistent && !(options?.create ?? false))) return;
41+
42+
const init = {
43+
parentId: head.commitId,
44+
projectId,
45+
pageId: head.pageId,
46+
userId,
47+
project,
48+
title,
49+
};
50+
const injectedSocket = options?.socket;
51+
const socket = injectedSocket ?? await socketIO();
52+
await connect(socket);
53+
const { request } = wrap(socket);
54+
55+
// タイトルのみのページを作る
56+
if (!head.persistent) {
57+
const commitId = await pushWithRetry(request, [{ title }], init);
58+
init.parentId = commitId;
59+
}
60+
61+
try {
62+
await pushWithRetry(request, [{ pin: pinNumber() }], init);
63+
} finally {
64+
if (!injectedSocket) await disconnect(socket);
65+
}
66+
}
67+
68+
export interface UnPinOptions {
69+
socket?: Socket;
70+
}
71+
/** 指定したページのピン留めを外す
72+
*
73+
* @param project ピン留めを外したいページのproject
74+
* @param title ピン留めを外したいページのタイトル
75+
*/
76+
export async function unpin(
77+
project: string,
78+
title: string,
79+
options: UnPinOptions,
80+
): Promise<void> {
81+
const [
82+
head,
83+
projectId,
84+
userId,
85+
] = await Promise.all([
86+
pull(project, title),
87+
getProjectId(project),
88+
getUserId(),
89+
]);
90+
91+
// 既にピンが外れているか、そもそも存在しないページの場合は何もしない
92+
if (head.pin == 0 || !head.persistent) return;
93+
94+
const init = {
95+
parentId: head.commitId,
96+
projectId,
97+
pageId: head.pageId,
98+
userId,
99+
project,
100+
title,
101+
};
102+
const injectedSocket = options?.socket;
103+
const socket = injectedSocket ?? await socketIO();
104+
await connect(socket);
105+
const { request } = wrap(socket);
106+
107+
try {
108+
await pushWithRetry(request, [{ pin: 0 }], init);
109+
} finally {
110+
if (!injectedSocket) await disconnect(socket);
111+
}
112+
}
113+
1114
export const pinNumber = (): number =>
2115
Number.MAX_SAFE_INTEGER - Math.floor(Date.now() / 1000);

browser/websocket/shortcuts.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)