Skip to content

Commit 626fb4a

Browse files
committed
若干refactoringした
1 parent dc3c398 commit 626fb4a

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

browser/websocket/updateCodeFile.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { HeadData, pull } from "./pull.ts";
1212
import { getCodeBlocks, TinyCodeBlock } from "./getCodeBlocks.ts";
1313
import { diffToChanges } from "./diffToChanges.ts";
1414
import { createNewLineId, getProjectId, getUserId } from "./id.ts";
15-
import { pushCommit } from "./_fetch.ts";
15+
import { pushWithRetry } from "./_fetch.ts";
1616

1717
/** コードブロックの上書きに使う情報のinterface */
1818
export interface CodeFile {
@@ -93,7 +93,7 @@ export const updateCodeFile = async (
9393
insertLineId,
9494
);
9595
if (codeBodies.length <= 0) {
96-
await applyCommit(commits, head, project, opt.socket);
96+
await applyCommit(commits, head, project, title, opt.socket);
9797
}
9898
return;
9999
} else if (codeBodies.length <= 0) {
@@ -106,7 +106,7 @@ export const updateCodeFile = async (
106106
insertLineId,
107107
);
108108
if (codeBodies.length <= 0) {
109-
await applyCommit(commits.splice(1), head, project, opt.socket);
109+
await applyCommit(commits.splice(1), head, project, title, opt.socket);
110110
}
111111
return;
112112
}
@@ -132,7 +132,7 @@ export const updateCodeFile = async (
132132
}
133133

134134
// 差分を送信
135-
await applyCommit(commits, head, project, opt.socket);
135+
await applyCommit(commits, head, project, title, opt.socket);
136136

137137
if (!options?.socket) opt.socket.disconnect();
138138
};
@@ -155,33 +155,23 @@ async function applyCommit(
155155
commits: Change[],
156156
head: HeadData,
157157
projectName: string,
158+
pageTitle: string,
158159
socket: Socket,
159-
): ReturnType<typeof pushCommit> {
160+
): ReturnType<typeof pushWithRetry> {
160161
const [projectId, userId] = await Promise.all([
161162
getProjectId(projectName),
162163
getUserId(),
163164
]);
164-
165-
// 3回retryする
166-
for (let i = 0; i < 3; i++) {
167-
try {
168-
// 差分を送信
169-
const { request } = wrap(socket);
170-
const res = await pushCommit(request, commits, {
171-
parentId: head.commitId,
172-
projectId: projectId,
173-
pageId: head.pageId,
174-
userId: userId,
175-
});
176-
return res;
177-
} catch (_e: unknown) {
178-
console.log(
179-
"Faild to push a commit.",
180-
);
181-
if (i === 2) break;
182-
}
183-
}
184-
throw Error("Faild to retry pushing.");
165+
const { request } = wrap(socket);
166+
return await pushWithRetry(request, commits, {
167+
parentId: head.commitId,
168+
projectId: projectId,
169+
pageId: head.pageId,
170+
userId: userId,
171+
project: projectName,
172+
title: pageTitle,
173+
retry: 3,
174+
});
185175
}
186176

187177
/** 新規コードブロックのコミットを作成する */

0 commit comments

Comments
 (0)