@@ -2,14 +2,22 @@ import { Socket, socketIO, wrap } from "../../deps/socket.ts";
22import { connect , disconnect } from "./socket.ts" ;
33import { getProjectId , getUserId } from "./id.ts" ;
44import { makeChanges } from "./makeChanges.ts" ;
5- import { HeadData , pull } from "./pull.ts" ;
6- import type { Line } from "../../deps/scrapbox-rest.ts" ;
5+ import { pull } from "./pull.ts" ;
6+ import { Line , Page } from "../../deps/scrapbox-rest.ts" ;
77import { pushCommit , pushWithRetry } from "./_fetch.ts" ;
88
99export interface PatchOptions {
1010 socket ?: Socket ;
1111}
1212
13+ export interface PatchMetadata extends Page {
14+ /** 書き換えを再試行した回数
15+ *
16+ * 初回は`0`で、再試行するたびに増える
17+ */
18+ retry : number ;
19+ }
20+
1321/** ページ全体を書き換える
1422 *
1523 * serverには書き換え前後の差分だけを送信する
@@ -24,12 +32,12 @@ export const patch = async (
2432 title : string ,
2533 update : (
2634 lines : Line [ ] ,
27- metadata : HeadData ,
35+ metadata : PatchMetadata ,
2836 ) => string [ ] | undefined | Promise < string [ ] | undefined > ,
2937 options ?: PatchOptions ,
3038) : Promise < void > => {
3139 const [
32- head_ ,
40+ page_ ,
3341 projectId ,
3442 userId ,
3543 ] = await Promise . all ( [
@@ -38,7 +46,7 @@ export const patch = async (
3846 getUserId ( ) ,
3947 ] ) ;
4048
41- let head = head_ ;
49+ let page = page_ ;
4250
4351 const injectedSocket = options ?. socket ;
4452 const socket = injectedSocket ?? await socketIO ( ) ;
@@ -47,43 +55,43 @@ export const patch = async (
4755 const { request } = wrap ( socket ) ;
4856
4957 // 3回retryする
50- for ( let i = 0 ; i < 3 ; i ++ ) {
58+ for ( let retry = 0 ; retry < 3 ; retry ++ ) {
5159 try {
52- const pending = update ( head . lines , head ) ;
60+ const pending = update ( page . lines , { ... page , retry } ) ;
5361 const newLines = pending instanceof Promise ? await pending : pending ;
5462
5563 if ( ! newLines ) return ;
5664
5765 if ( newLines . length === 0 ) {
5866 await pushWithRetry ( request , [ { deleted : true } ] , {
5967 projectId,
60- pageId : head . pageId ,
61- parentId : head . commitId ,
68+ pageId : page . id ,
69+ parentId : page . commitId ,
6270 userId,
6371 project,
6472 title,
6573 } ) ;
6674 }
6775
6876 const changes = [
69- ...makeChanges ( head . lines , newLines , { userId, head } ) ,
77+ ...makeChanges ( page . lines , newLines , { userId, page } ) ,
7078 ] ;
7179 await pushCommit ( request , changes , {
72- parentId : head . commitId ,
80+ parentId : page . commitId ,
7381 projectId,
74- pageId : head . pageId ,
82+ pageId : page . id ,
7583 userId,
7684 } ) ;
7785 break ;
7886 } catch ( _e : unknown ) {
79- if ( i === 2 ) {
87+ if ( retry === 2 ) {
8088 throw Error ( "Faild to retry pushing." ) ;
8189 }
8290 console . log (
8391 "Faild to push a commit. Retry after pulling new commits" ,
8492 ) ;
8593 try {
86- head = await pull ( project , title ) ;
94+ page = await pull ( project , title ) ;
8795 } catch ( e : unknown ) {
8896 throw e ;
8997 }
0 commit comments