|
| 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 { makeChanges } from "./makeChanges.ts"; |
| 5 | +import { HeadData, pull } from "./pull.ts"; |
| 6 | +import type { Line } from "../../deps/scrapbox.ts"; |
| 7 | +import { pushCommit, pushWithRetry } from "./_fetch.ts"; |
| 8 | + |
| 9 | +export interface PatchOptions { |
| 10 | + socket?: Socket; |
| 11 | +} |
| 12 | + |
| 13 | +/** ページ全体を書き換える |
| 14 | + * |
| 15 | + * serverには書き換え前後の差分だけを送信する |
| 16 | + * |
| 17 | + * @param project 書き換えたいページのproject |
| 18 | + * @param title 書き換えたいページのタイトル |
| 19 | + * @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される。undefinedを返すと書き換えを中断する |
| 20 | + * @param options 使用したいSocketがあれば指定する |
| 21 | + */ |
| 22 | +export async function patch( |
| 23 | + project: string, |
| 24 | + title: string, |
| 25 | + update: ( |
| 26 | + lines: Line[], |
| 27 | + metadata: HeadData, |
| 28 | + ) => string[] | undefined | Promise<string[] | undefined>, |
| 29 | + options?: PatchOptions, |
| 30 | +): Promise<void> { |
| 31 | + const [ |
| 32 | + head_, |
| 33 | + projectId, |
| 34 | + userId, |
| 35 | + ] = await Promise.all([ |
| 36 | + pull(project, title), |
| 37 | + getProjectId(project), |
| 38 | + getUserId(), |
| 39 | + ]); |
| 40 | + |
| 41 | + let head = head_; |
| 42 | + |
| 43 | + const injectedSocket = options?.socket; |
| 44 | + const socket = injectedSocket ?? await socketIO(); |
| 45 | + await connect(socket); |
| 46 | + try { |
| 47 | + const { request } = wrap(socket); |
| 48 | + |
| 49 | + // 3回retryする |
| 50 | + for (let i = 0; i < 3; i++) { |
| 51 | + try { |
| 52 | + const pending = update(head.lines, head); |
| 53 | + const newLines = pending instanceof Promise ? await pending : pending; |
| 54 | + |
| 55 | + if (!newLines) return; |
| 56 | + |
| 57 | + if (newLines.length === 0) { |
| 58 | + await pushWithRetry(request, [{ deleted: true }], { |
| 59 | + projectId, |
| 60 | + pageId: head.pageId, |
| 61 | + parentId: head.commitId, |
| 62 | + userId, |
| 63 | + project, |
| 64 | + title, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + const changes = makeChanges(head.lines, newLines, { userId, head }); |
| 69 | + await pushCommit(request, changes, { |
| 70 | + parentId: head.commitId, |
| 71 | + projectId, |
| 72 | + pageId: head.pageId, |
| 73 | + userId, |
| 74 | + }); |
| 75 | + break; |
| 76 | + } catch (_e: unknown) { |
| 77 | + if (i === 2) { |
| 78 | + throw Error("Faild to retry pushing."); |
| 79 | + } |
| 80 | + console.log( |
| 81 | + "Faild to push a commit. Retry after pulling new commits", |
| 82 | + ); |
| 83 | + try { |
| 84 | + head = await pull(project, title); |
| 85 | + } catch (e: unknown) { |
| 86 | + throw e; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } finally { |
| 91 | + if (!injectedSocket) await disconnect(socket); |
| 92 | + } |
| 93 | +} |
0 commit comments