Skip to content

Commit 4bf8d8e

Browse files
committed
♻️ websocketの函数のほうを変換し忘れていた
1 parent e34ad0b commit 4bf8d8e

File tree

10 files changed

+56
-61
lines changed

10 files changed

+56
-61
lines changed

browser/websocket/_fetch.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export type PushCommitInit = {
2222
userId: string;
2323
};
2424

25-
export async function pushCommit(
25+
export const pushCommit = async (
2626
request: RequestFunc,
2727
changes: Change[] | [Delete] | [Pin],
2828
commitInit: PushCommitInit,
29-
) {
29+
) => {
3030
if (changes.length === 0) return { commitId: commitInit.parentId };
3131
const res = await request("socket.io-request", {
3232
method: "commit",
@@ -39,9 +39,9 @@ export async function pushCommit(
3939
},
4040
});
4141
return res as { commitId: string };
42-
}
42+
};
4343

44-
export async function pushWithRetry(
44+
export const pushWithRetry = async (
4545
request: RequestFunc,
4646
changes: Change[] | [Delete] | [Pin],
4747
{ project, title, retry = 3, parentId, ...commitInit }:
@@ -51,7 +51,7 @@ export async function pushWithRetry(
5151
title: string;
5252
retry?: number;
5353
},
54-
) {
54+
) => {
5555
try {
5656
const res = await pushCommit(request, changes, {
5757
parentId,
@@ -78,4 +78,4 @@ export async function pushWithRetry(
7878
throw Error("Faild to retry pushing.");
7979
}
8080
return parentId;
81-
}
81+
};

browser/websocket/applyCommit.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CommitNotification } from "../../deps/socket.ts";
2-
import type { Line } from "../../deps/scrapbox.ts";
2+
import type { Line } from "../../deps/scrapbox-rest.ts";
33
import { getUnixTimeFromId } from "./id.ts";
44

55
export interface ApplyCommitProp {
@@ -17,11 +17,11 @@ export interface ApplyCommitProp {
1717
* @param lines commitsを適用する行
1818
* @param changes 適用するcommits
1919
*/
20-
export function applyCommit(
20+
export const applyCommit = (
2121
lines: readonly Line[],
2222
changes: CommitNotification["changes"],
2323
{ updated, userId }: ApplyCommitProp,
24-
) {
24+
): Line[] => {
2525
const newLines = [...lines];
2626
const getPos = (lineId: string) => {
2727
const position = newLines.findIndex(({ id }) => id === lineId);
@@ -57,4 +57,4 @@ export function applyCommit(
5757
}
5858
}
5959
return newLines;
60-
}
60+
};

browser/websocket/deletePage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export interface DeletePageOptions {
1414
* @param title 削除したいページのタイトル
1515
* @param options 使用したいSocketがあれば指定する
1616
*/
17-
export async function deletePage(
17+
export const deletePage = async (
1818
project: string,
1919
title: string,
2020
options?: DeletePageOptions,
21-
): Promise<void> {
21+
): Promise<void> => {
2222
const [
2323
{ pageId, commitId: parentId, persistent },
2424
projectId,
@@ -47,4 +47,4 @@ export async function deletePage(
4747
} finally {
4848
if (!injectedSocket) await disconnect(socket);
4949
}
50-
}
50+
};

browser/websocket/diff.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export interface DiffResult<T> {
3636
buildSES(): Generator<Change<T>, void, unknown>;
3737
}
3838

39-
export function diff<T>(
39+
export const diff = <T>(
4040
left: ArrayLike<T>,
4141
right: ArrayLike<T>,
42-
): DiffResult<T> {
42+
): DiffResult<T> => {
4343
const reversed = left.length > right.length;
4444
const a = reversed ? right : left;
4545
const b = reversed ? left : right;
@@ -114,7 +114,7 @@ export function diff<T>(
114114
}
115115
},
116116
};
117-
}
117+
};
118118

119119
export function* toExtendedChanges<T>(
120120
changes: Iterable<Change<T>>,
@@ -165,16 +165,14 @@ export function* toExtendedChanges<T>(
165165
yield* flush();
166166
}
167167

168-
function makeReplaced<T>(
168+
const makeReplaced = <T>(
169169
left: Added<T>,
170170
right: Deleted<T>,
171-
): Replaced<T> {
172-
return {
173-
value: left.value,
174-
oldValue: right.value,
175-
type: "replaced",
176-
};
177-
}
171+
): Replaced<T> => ({
172+
value: left.value,
173+
oldValue: right.value,
174+
type: "replaced",
175+
});
178176

179177
function* reverse<T>(list: ArrayLike<T>) {
180178
for (let i = list.length - 1; i >= 0; i--) {

browser/websocket/id.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getProfile } from "../../rest/profile.ts";
33

44
/** cached user ID */
55
let userId: string | undefined;
6-
export async function getUserId() {
6+
export const getUserId = async (): Promise<string> => {
77
if (userId !== undefined) return userId;
88

99
const user = await getProfile();
@@ -12,11 +12,11 @@ export async function getUserId() {
1212
}
1313
userId = user.id;
1414
return userId;
15-
}
15+
};
1616

1717
/** cached pairs of project name and project id */
1818
const projectMap = new Map<string, string>();
19-
export async function getProjectId(project: string) {
19+
export const getProjectId = async (project: string): Promise<string> => {
2020
const cachedId = projectMap.get(project);
2121
if (cachedId !== undefined) return cachedId;
2222

@@ -28,22 +28,18 @@ export async function getProjectId(project: string) {
2828
const { id } = result.value;
2929
projectMap.set(project, id);
3030
return id;
31-
}
31+
};
3232

33-
function zero(n: string) {
34-
return n.padStart(8, "0");
35-
}
33+
const zero = (n: string) => n.padStart(8, "0");
3634

37-
export function createNewLineId(userId: string) {
35+
export const createNewLineId = (userId: string): string => {
3836
const time = Math.floor(new Date().getTime() / 1000).toString(16);
3937
const rand = Math.floor(0xFFFFFE * Math.random()).toString(16);
4038
return `${zero(time).slice(-8)}${userId.slice(-6)}0000${zero(rand)}`;
41-
}
42-
export function getUnixTimeFromId(id: string) {
39+
};
40+
export const getUnixTimeFromId = (id: string): number => {
4341
if (!isId(id)) throw SyntaxError(`"${id}" is an invalid id.`);
4442

4543
return parseInt(`0x${id.slice(0, 8)}`, 16);
46-
}
47-
export function isId(id: string) {
48-
return /^[a-f\d]{24,32}$/.test(id);
49-
}
44+
};
45+
export const isId = (id: string): boolean => /^[a-f\d]{24,32}$/.test(id);

browser/websocket/makeChanges.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export interface Init {
1515
userId: string;
1616
head: HeadData;
1717
}
18-
export function makeChanges(
18+
export const makeChanges = (
1919
left: Pick<Line, "text" | "id">[],
2020
right: string[],
2121
{ userId, head }: Init,
22-
) {
22+
): Change[] => {
2323
// 改行文字が入るのを防ぐ
2424
const right_ = right.flatMap((text) => text.split("\n"));
2525
// 本文の差分
@@ -51,10 +51,10 @@ export function makeChanges(
5151
}
5252

5353
return changes;
54-
}
54+
};
5555

5656
/** テキストに含まれる全てのリンクと最初の画像を探す */
57-
function findLinksAndImage(text: string): [string[], string | null] {
57+
const findLinksAndImage = (text: string): [string[], string | null] => {
5858
const rows = parseToRows(text);
5959
const blocks = packRows(rows, { hasTitle: true }).flatMap((pack) => {
6060
switch (pack.type) {
@@ -115,7 +115,7 @@ function findLinksAndImage(text: string): [string[], string | null] {
115115
}
116116

117117
return [links, image];
118-
}
118+
};
119119

120120
function* blocksToNodes(blocks: Iterable<Block>) {
121121
for (const block of blocks) {

browser/websocket/patch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ export interface PatchOptions {
1919
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される。undefinedを返すと書き換えを中断する
2020
* @param options 使用したいSocketがあれば指定する
2121
*/
22-
export async function patch(
22+
export const patch = async (
2323
project: string,
2424
title: string,
2525
update: (
2626
lines: Line[],
2727
metadata: HeadData,
2828
) => string[] | undefined | Promise<string[] | undefined>,
2929
options?: PatchOptions,
30-
): Promise<void> {
30+
): Promise<void> => {
3131
const [
3232
head_,
3333
projectId,
@@ -90,4 +90,4 @@ export async function patch(
9090
} finally {
9191
if (!injectedSocket) await disconnect(socket);
9292
}
93-
}
93+
};

browser/websocket/pin.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export interface PinOptions {
2121
* @param title ピン留めしたいページのタイトル
2222
* @param options 使用したいSocketがあれば指定する
2323
*/
24-
export async function pin(
24+
export const pin = async (
2525
project: string,
2626
title: string,
2727
options?: PinOptions,
28-
): Promise<void> {
28+
): Promise<void> => {
2929
const [
3030
head,
3131
projectId,
@@ -63,7 +63,7 @@ export async function pin(
6363
} finally {
6464
if (!injectedSocket) await disconnect(socket);
6565
}
66-
}
66+
};
6767

6868
export interface UnPinOptions {
6969
socket?: Socket;
@@ -73,11 +73,11 @@ export interface UnPinOptions {
7373
* @param project ピン留めを外したいページのproject
7474
* @param title ピン留めを外したいページのタイトル
7575
*/
76-
export async function unpin(
76+
export const unpin = async (
7777
project: string,
7878
title: string,
7979
options: UnPinOptions,
80-
): Promise<void> {
80+
): Promise<void> => {
8181
const [
8282
head,
8383
projectId,
@@ -109,7 +109,7 @@ export async function unpin(
109109
} finally {
110110
if (!injectedSocket) await disconnect(socket);
111111
}
112-
}
112+
};
113113

114114
export const pinNumber = (): number =>
115115
Number.MAX_SAFE_INTEGER - Math.floor(Date.now() / 1000);

browser/websocket/pull.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export interface HeadData {
1010
links: string[];
1111
lines: Line[];
1212
}
13-
export async function pull(project: string, title: string): Promise<HeadData> {
13+
export const pull = async (
14+
project: string,
15+
title: string,
16+
): Promise<HeadData> => {
1417
const result = await getPage(project, title);
1518

1619
// TODO: 編集不可なページはStream購読だけ提供する
@@ -28,4 +31,4 @@ export async function pull(project: string, title: string): Promise<HeadData> {
2831
pin,
2932
lines,
3033
};
31-
}
34+
};

browser/websocket/socket.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@ import { Socket, socketIO } from "../../deps/socket.ts";
22
export type { Socket } from "../../deps/socket.ts";
33

44
/** 新しいsocketを作る */
5-
export function makeSocket() {
6-
return socketIO();
7-
}
5+
export const makeSocket = (): Promise<Socket> => socketIO();
86

97
/** websocketに(再)接続する
108
*
119
* @param socket 接続したいsocket
1210
*/
13-
export async function connect(socket: Socket): Promise<void> {
11+
export const connect = async (socket: Socket): Promise<void> => {
1412
if (socket.connected) return;
1513

1614
const waiting = new Promise<void>((resolve) =>
1715
socket.once("connect", () => resolve())
1816
);
1917
socket.connect();
2018
await waiting;
21-
}
19+
};
2220

2321
/** websocketを切断する
2422
*
2523
* @param socket 切断したいsocket
2624
*/
27-
export async function disconnect(socket: Socket): Promise<void> {
25+
export const disconnect = async (socket: Socket): Promise<void> => {
2826
if (socket.disconnected) return;
2927

3028
const waiting = new Promise<void>((resolve) => {
@@ -37,4 +35,4 @@ export async function disconnect(socket: Socket): Promise<void> {
3735
});
3836
socket.disconnect();
3937
await waiting;
40-
}
38+
};

0 commit comments

Comments
 (0)