11import {
22 Change ,
33 CommitNotification ,
4- Delete ,
5- Pin ,
4+ DeletePageChange ,
5+ PageCommitError ,
6+ PageCommitResponse ,
7+ PinChange ,
68 ProjectUpdatesStreamCommit ,
79 ProjectUpdatesStreamEvent ,
10+ Result ,
11+ TimeoutError ,
12+ UnexpectedError ,
813 wrap ,
914} from "../../deps/socket.ts" ;
1015import { pull } from "./pull.ts" ;
@@ -22,28 +27,37 @@ export type PushCommitInit = {
2227 userId : string ;
2328} ;
2429
25- export const pushCommit = async (
30+ export const pushCommit = (
2631 request : RequestFunc ,
27- changes : Change [ ] | [ Delete ] | [ Pin ] ,
32+ changes : Change [ ] | [ DeletePageChange ] | [ PinChange ] ,
2833 commitInit : PushCommitInit ,
29- ) => {
30- if ( changes . length === 0 ) return { commitId : commitInit . parentId } ;
31- const res = await request ( "socket.io-request" , {
32- method : "commit" ,
33- data : {
34- kind : "page" ,
35- ...commitInit ,
36- changes,
37- cursor : null ,
38- freeze : true ,
39- } ,
40- } ) ;
41- return res as { commitId : string } ;
42- } ;
34+ ) : Promise <
35+ Result <
36+ PageCommitResponse ,
37+ UnexpectedError | TimeoutError | PageCommitError
38+ >
39+ > =>
40+ changes . length === 0
41+ ? Promise . resolve ( { ok : true , value : { commitId : commitInit . parentId } } )
42+ : request ( "socket.io-request" , {
43+ method : "commit" ,
44+ data : {
45+ kind : "page" ,
46+ ...commitInit ,
47+ changes,
48+ cursor : null ,
49+ freeze : true ,
50+ } ,
51+ } ) as Promise <
52+ Result <
53+ PageCommitResponse ,
54+ UnexpectedError | TimeoutError | PageCommitError
55+ >
56+ > ;
4357
4458export const pushWithRetry = async (
4559 request : RequestFunc ,
46- changes : Change [ ] | [ Delete ] | [ Pin ] ,
60+ changes : Change [ ] | [ DeletePageChange ] | [ PinChange ] ,
4761 { project, title, retry = 3 , parentId, ...commitInit } :
4862 & PushCommitInit
4963 & {
@@ -57,8 +71,9 @@ export const pushWithRetry = async (
5771 parentId,
5872 ...commitInit ,
5973 } ) ;
60- parentId = res . commitId ;
61- } catch ( _e ) {
74+ if ( ! res . ok ) throw Error ( "Faild to push a commit" ) ;
75+ parentId = res . value . commitId ;
76+ } catch ( _ ) {
6277 console . log ( "Faild to push a commit. Retry after pulling new commits" ) ;
6378 for ( let i = 0 ; i < retry ; i ++ ) {
6479 const { commitId } = await pull ( project , title ) ;
@@ -68,10 +83,11 @@ export const pushWithRetry = async (
6883 parentId,
6984 ...commitInit ,
7085 } ) ;
71- parentId = res . commitId ;
86+ if ( ! res . ok ) throw Error ( "Faild to push a commit" ) ;
87+ parentId = res . value . commitId ;
7288 console . log ( "Success in retrying" ) ;
7389 break ;
74- } catch ( _e ) {
90+ } catch ( _ ) {
7591 continue ;
7692 }
7793 }
0 commit comments