6
6
*/
7
7
8
8
import { TableDelta } from 'app/common/ActionSummary' ;
9
+ import { PatchItem , PatchLog } from 'app/common/ActiveDocAPI' ;
9
10
import { UserAction } from 'app/common/DocActions' ;
10
11
import { DocStateComparisonDetails } from 'app/common/DocState' ;
11
12
import { ActiveDoc } from 'app/server/lib/ActiveDoc' ;
12
13
import { OptDocSession } from 'app/server/lib/DocSession' ;
13
14
14
- export interface Change {
15
- msg : string ;
16
- fail ?: boolean ;
17
- }
18
-
19
- export type Changes = Change [ ] ;
20
-
21
15
export class Patch {
22
16
private _otherId : number | undefined ;
23
17
private _linkId : number | undefined ;
24
18
25
19
public constructor ( public gristDoc : ActiveDoc , public docSession : OptDocSession ) { }
26
20
27
- public async applyChanges ( details : DocStateComparisonDetails ) : Promise < Changes > {
28
- const changes : Changes = [ ] ;
29
- const summary = details . leftChanges ;
30
- if ( summary . tableRenames . length > 0 ) {
31
- changes . push ( {
32
- msg : 'table renames ignored' ,
33
- } ) ;
34
- }
35
- for ( const [ tableId , delta ] of Object . entries ( summary . tableDeltas ) ) {
36
- if ( tableId . startsWith ( '_grist_' ) ) {
37
- continue ;
38
- }
39
- if ( delta . columnRenames . length > 0 ) {
40
- changes . push ( {
41
- msg : 'column renames ignored' ,
42
- } ) ;
43
- }
44
- if ( delta . removeRows . length > 0 ) {
45
- changes . push ( ...await this . removeRows ( tableId , delta ) ) ;
21
+ public async applyChanges ( details : DocStateComparisonDetails ) : Promise < PatchLog > {
22
+ const changes : PatchItem [ ] = [ ] ;
23
+ try {
24
+ const summary = details . leftChanges ;
25
+ if ( summary . tableRenames . length > 0 ) {
26
+ throw new Error ( 'table-level changes cannot be handled yet' ) ;
46
27
}
47
- if ( delta . updateRows . length > 0 ) {
48
- changes . push ( ...await this . updateRows ( tableId , delta ) ) ;
28
+ for ( const [ , delta ] of Object . entries ( summary . tableDeltas ) ) {
29
+ if ( delta . columnRenames . length > 0 ) {
30
+ throw new Error ( 'column-level changes cannot be handled yet' ) ;
31
+ }
49
32
}
50
- if ( delta . addRows . length > 0 ) {
51
- changes . push ( ...await this . addRows ( tableId , delta ) ) ;
33
+ for ( const [ tableId , delta ] of Object . entries ( summary . tableDeltas ) ) {
34
+ if ( tableId . startsWith ( '_grist_' ) ) {
35
+ continue ;
36
+ }
37
+ if ( delta . columnRenames . length > 0 ) {
38
+ changes . push ( {
39
+ msg : 'column renames ignored' ,
40
+ } ) ;
41
+ }
42
+ if ( delta . removeRows . length > 0 ) {
43
+ changes . push ( ...await this . removeRows ( tableId , delta ) ) ;
44
+ }
45
+ if ( delta . updateRows . length > 0 ) {
46
+ changes . push ( ...await this . updateRows ( tableId , delta ) ) ;
47
+ }
48
+ if ( delta . addRows . length > 0 ) {
49
+ changes . push ( ...await this . addRows ( tableId , delta ) ) ;
50
+ }
52
51
}
52
+ } catch ( e ) {
53
+ changes . push ( {
54
+ msg : String ( e ) ,
55
+ fail : true ,
56
+ } ) ;
53
57
}
54
- return changes ;
58
+ const applied = changes . some ( change => ! change . fail ) ;
59
+ return { changes, applied} ;
55
60
}
56
61
57
62
public async change ( delta : TableDelta , tableId : string , rowId : number , colId : string ,
58
- pre : any , post : any ) : Promise < Change > {
63
+ pre : any , post : any ) : Promise < PatchItem > {
59
64
await this . applyUserActions ( [
60
65
[ 'UpdateRecord' , tableId , rowId , { [ colId ] : post } ] ,
61
66
] ) ;
@@ -80,7 +85,7 @@ export class Patch {
80
85
this . _linkId = result . actionNum ;
81
86
}
82
87
83
- public async doAdd ( delta : TableDelta , tableId : string , rowId : number , rec : Record < string , any > ) : Promise < Change > {
88
+ public async doAdd ( delta : TableDelta , tableId : string , rowId : number , rec : Record < string , any > ) : Promise < PatchItem > {
84
89
if ( rec . manualSort ) {
85
90
delete rec . manualSort ;
86
91
}
@@ -95,7 +100,8 @@ export class Patch {
95
100
} ;
96
101
}
97
102
98
- public async doRemove ( delta : TableDelta , tableId : string , rowId : number , rec : Record < string , any > ) : Promise < Change > {
103
+ public async doRemove ( delta : TableDelta , tableId : string , rowId : number ,
104
+ rec : Record < string , any > ) : Promise < PatchItem > {
99
105
await this . applyUserActions ( [
100
106
[ 'RemoveRecord' , tableId , rowId ] ,
101
107
] ) ;
@@ -107,8 +113,8 @@ export class Patch {
107
113
} ;
108
114
}
109
115
110
- public async updateRows ( tableId : string , delta : TableDelta ) : Promise < Changes > {
111
- const changes : Changes = [ ] ;
116
+ public async updateRows ( tableId : string , delta : TableDelta ) : Promise < PatchItem [ ] > {
117
+ const changes : PatchItem [ ] = [ ] ;
112
118
const rows = remaining ( delta . updateRows , undefined ) ; //, delta.accepted?.updateRows);
113
119
const columnDeltas = delta . columnDeltas ;
114
120
for ( const row of rows ) {
@@ -128,8 +134,8 @@ export class Patch {
128
134
return changes ;
129
135
}
130
136
131
- public async addRows ( tableId : string , delta : TableDelta ) : Promise < Changes > {
132
- const changes : Changes = [ ] ;
137
+ public async addRows ( tableId : string , delta : TableDelta ) : Promise < PatchItem [ ] > {
138
+ const changes : PatchItem [ ] = [ ] ;
133
139
const rows = remaining ( delta . addRows , undefined ) ; //delta.accepted?.addRows);
134
140
const columnDeltas = delta . columnDeltas ;
135
141
for ( const row of rows ) {
@@ -149,8 +155,8 @@ export class Patch {
149
155
return changes ;
150
156
}
151
157
152
- public async removeRows ( tableId : string , delta : TableDelta ) : Promise < Changes > {
153
- const changes : Changes = [ ] ;
158
+ public async removeRows ( tableId : string , delta : TableDelta ) : Promise < PatchItem [ ] > {
159
+ const changes : PatchItem [ ] = [ ] ;
154
160
const rows = remaining ( delta . removeRows , undefined ) ; //delta.accepted?.removeRows);
155
161
const columnDeltas = delta . columnDeltas ;
156
162
for ( const row of rows ) {
0 commit comments