diff --git a/browser/websocket/_fetch.ts b/browser/websocket/_fetch.ts index e722e5c..94f5f49 100644 --- a/browser/websocket/_fetch.ts +++ b/browser/websocket/_fetch.ts @@ -1,10 +1,15 @@ import { Change, CommitNotification, - Delete, - Pin, + DeletePageChange, + PageCommitError, + PageCommitResponse, + PinChange, ProjectUpdatesStreamCommit, ProjectUpdatesStreamEvent, + Result, + TimeoutError, + UnexpectedError, wrap, } from "../../deps/socket.ts"; import { pull } from "./pull.ts"; @@ -22,28 +27,37 @@ export type PushCommitInit = { userId: string; }; -export const pushCommit = async ( +export const pushCommit = ( request: RequestFunc, - changes: Change[] | [Delete] | [Pin], + changes: Change[] | [DeletePageChange] | [PinChange], commitInit: PushCommitInit, -) => { - if (changes.length === 0) return { commitId: commitInit.parentId }; - const res = await request("socket.io-request", { - method: "commit", - data: { - kind: "page", - ...commitInit, - changes, - cursor: null, - freeze: true, - }, - }); - return res as { commitId: string }; -}; +): Promise< + Result< + PageCommitResponse, + UnexpectedError | TimeoutError | PageCommitError + > +> => + changes.length === 0 + ? Promise.resolve({ ok: true, value: { commitId: commitInit.parentId } }) + : request("socket.io-request", { + method: "commit", + data: { + kind: "page", + ...commitInit, + changes, + cursor: null, + freeze: true, + }, + }) as Promise< + Result< + PageCommitResponse, + UnexpectedError | TimeoutError | PageCommitError + > + >; export const pushWithRetry = async ( request: RequestFunc, - changes: Change[] | [Delete] | [Pin], + changes: Change[] | [DeletePageChange] | [PinChange], { project, title, retry = 3, parentId, ...commitInit }: & PushCommitInit & { @@ -57,8 +71,9 @@ export const pushWithRetry = async ( parentId, ...commitInit, }); - parentId = res.commitId; - } catch (_e) { + if (!res.ok) throw Error("Faild to push a commit"); + parentId = res.value.commitId; + } catch (_) { console.log("Faild to push a commit. Retry after pulling new commits"); for (let i = 0; i < retry; i++) { const { commitId } = await pull(project, title); @@ -68,10 +83,11 @@ export const pushWithRetry = async ( parentId, ...commitInit, }); - parentId = res.commitId; + if (!res.ok) throw Error("Faild to push a commit"); + parentId = res.value.commitId; console.log("Success in retrying"); break; - } catch (_e) { + } catch (_) { continue; } } diff --git a/browser/websocket/diffToChanges.ts b/browser/websocket/diffToChanges.ts index b4c02f3..c5bd1b2 100644 --- a/browser/websocket/diffToChanges.ts +++ b/browser/websocket/diffToChanges.ts @@ -1,9 +1,9 @@ import { diff, toExtendedChanges } from "../../deps/onp.ts"; import type { Line } from "../../deps/scrapbox.ts"; import type { - DeleteCommit, - InsertCommit, - UpdateCommit, + DeleteChange, + InsertChange, + UpdateChange, } from "../../deps/socket.ts"; import { createNewLineId } from "./id.ts"; @@ -14,7 +14,7 @@ export function* diffToChanges( left: Pick[], right: string[], { userId }: Options, -): Generator { +): Generator { const { buildSES } = diff( left.map(({ text }) => text), right, diff --git a/browser/websocket/updateCodeBlock.ts b/browser/websocket/updateCodeBlock.ts index fab7791..d51ec23 100644 --- a/browser/websocket/updateCodeBlock.ts +++ b/browser/websocket/updateCodeBlock.ts @@ -1,10 +1,10 @@ import { Line } from "../../deps/scrapbox-rest.ts"; import { - DeleteCommit, - InsertCommit, + DeleteChange, + InsertChange, Socket, socketIO, - UpdateCommit, + UpdateChange, } from "../../deps/socket.ts"; import { TinyCodeBlock } from "../../rest/getCodeBlocks.ts"; import { diffToChanges } from "./diffToChanges.ts"; @@ -91,9 +91,9 @@ const getCodeBody = (code: string | string[] | SimpleCodeFile): string[] => { /** insertコミットの行IDとtextのインデントを修正する */ function* fixCommits( - commits: readonly (DeleteCommit | InsertCommit | UpdateCommit)[], + commits: readonly (DeleteChange | InsertChange | UpdateChange)[], target: TinyCodeBlock, -): Generator { +): Generator { const { nextLine } = target; const indent = " ".repeat(countBodyIndent(target)); for (const commit of commits) { @@ -136,7 +136,7 @@ function* fixCommits( const makeTitleChangeCommit = ( code: SimpleCodeFile, target: Pick, -): UpdateCommit | null => { +): UpdateChange | null => { const lineId = target.titleLine.id; const targetTitle = extractFromCodeTitle(target.titleLine.text); if ( diff --git a/browser/websocket/updateCodeFile.ts b/browser/websocket/updateCodeFile.ts index edad586..f092e8e 100644 --- a/browser/websocket/updateCodeFile.ts +++ b/browser/websocket/updateCodeFile.ts @@ -1,10 +1,10 @@ import type { Line } from "../../deps/scrapbox-rest.ts"; import { - DeleteCommit, - InsertCommit, + DeleteChange, + InsertChange, Socket, socketIO, - UpdateCommit, + UpdateChange, } from "../../deps/socket.ts"; import { getCodeBlocks, TinyCodeBlock } from "../../rest/getCodeBlocks.ts"; import { pull } from "./pull.ts"; @@ -130,7 +130,7 @@ function* makeCommits( UpdateCodeFileOptions["isInsertEmptyLineInTail"] >; }, -): Generator { +): Generator { function makeIndent(codeBlock: Pick): string { return " ".repeat(countBodyIndent(codeBlock)); } diff --git a/deps/socket.ts b/deps/socket.ts index 8d4e7c2..3897e3f 100644 --- a/deps/socket.ts +++ b/deps/socket.ts @@ -1,17 +1 @@ -export type { - Change, - CommitNotification, - Delete, - DeleteCommit, - InsertCommit, - ListenEventMap, - Pin, - ProjectUpdatesStreamCommit, - ProjectUpdatesStreamEvent, - Socket, - UpdateCommit, -} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.6/mod.ts"; -export { - socketIO, - wrap, -} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.6/mod.ts"; +export * from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.2.1/mod.ts"; diff --git a/deps/testing.ts b/deps/testing.ts index fa7558b..2e7f0ce 100644 --- a/deps/testing.ts +++ b/deps/testing.ts @@ -1,2 +1,2 @@ -export * from "https://deno.land/std@0.222.1/testing/asserts.ts"; -export * from "https://deno.land/std@0.222.1/testing/snapshot.ts"; +export * from "https://deno.land/std@0.223.0/testing/asserts.ts"; +export * from "https://deno.land/std@0.223.0/testing/snapshot.ts";