@@ -2,18 +2,18 @@ import {NodeSelections} from './NodeSelectors';
2
2
import { NodeChangeSet } from './NodeChangeSet' ;
3
3
import { NodeSearchUtils , setNested } from './Utils' ;
4
4
import { Maybe , Result } from 'ts-monads' ;
5
- import diff from 'changeset' ;
5
+ import diff , { ChangeSet , ChangeType } from 'changeset' ;
6
6
import JSONImporter from "../JSONImporter" ;
7
7
import Core = GmeClasses . Core ;
8
8
9
9
type PatchFunction = ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) => Promise < Result < PatchResult , PatchError > > ;
10
10
type PatchResultPromise = Promise < Result < PatchResult , PatchError > > ;
11
11
12
12
class PatchResult {
13
- patch : NodeChangeSet ;
13
+ patches : NodeChangeSet | NodeChangeSet [ ] ;
14
14
15
- constructor ( patch ) {
16
- this . patch = patch ;
15
+ constructor ( patches : NodeChangeSet | NodeChangeSet [ ] ) {
16
+ this . patches = patches ;
17
17
}
18
18
}
19
19
@@ -148,44 +148,50 @@ export class PointersPatch extends NodeStatePatch {
148
148
149
149
export class GuidPatch extends NodeStatePatch {
150
150
delete = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
151
- return Result . Ok ( new PatchResult ( change ) ) ;
151
+ return change . asResult ( ) . map ( v => Maybe . some ( new PatchResult ( v ) ) ) ;
152
152
}
153
153
154
154
put = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
155
- const { value} = change ;
156
- this . core . setGuid ( node , value ) ;
157
- return Result . Ok ( new PatchResult ( change ) ) ;
155
+ return change . asResult ( ) . map ( chg => {
156
+ const { value} = chg ;
157
+ this . core . setGuid ( node , value ) ;
158
+ return Maybe . some ( new PatchResult ( chg ) ) ;
159
+ } ) ;
158
160
}
159
161
}
160
162
161
163
export class MixinsPatch extends NodeStatePatch {
162
164
delete = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
163
- const [ , index ] = change . key ;
164
- const mixinPath = this . core . getMixinPaths ( node ) [ index ] ;
165
- this . core . delMixin ( node , mixinPath ) ;
166
- return Result . Ok ( new PatchResult ( change ) ) ;
165
+ return change . asResult ( ) . map ( chg => {
166
+ const [ , index ] = chg . key ;
167
+ const mixinPath = this . core . getMixinPaths ( node ) [ index ] ;
168
+ this . core . delMixin ( node , mixinPath ) ;
169
+ return Maybe . some ( new PatchResult ( chg ) ) ;
170
+ } ) ;
167
171
} ;
168
172
169
173
put = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
170
- const [ , index ] = change . key ;
171
- const oldMixinPath = this . core . getMixinPaths ( node ) [ index ] ;
172
- if ( oldMixinPath ) {
173
- this . core . delMixin ( node , oldMixinPath ) ;
174
- }
174
+ return await change . asResult ( ) . mapAsync ( async chg => {
175
+ const [ , index ] = chg . key ;
176
+ const oldMixinPath = this . core . getMixinPaths ( node ) [ index ] ;
177
+ if ( oldMixinPath ) {
178
+ this . core . delMixin ( node , oldMixinPath ) ;
179
+ }
175
180
176
- const mixinId = change . value ;
181
+ const mixinId = chg . value ;
182
+
183
+ const mixinPath = await this . nodeSearchUtils . getNodeId ( node , mixinId , resolvedSelectors ) ;
184
+ const canSet = this . core . canSetAsMixin ( node , mixinPath ) ;
185
+ if ( canSet . isOk ) {
186
+ this . core . addMixin ( node , mixinPath ) ;
187
+ return Maybe . some ( new PatchResult ( chg ) ) ;
188
+ } else {
189
+ throw new PatchError (
190
+ `Cannot set ${ mixinId } as mixin for ${ this . core . getPath ( node ) } : ${ canSet . reason } `
191
+ ) ;
192
+ }
193
+ } ) ;
177
194
178
- const mixinPath = await this . nodeSearchUtils . getNodeId ( node , mixinId , resolvedSelectors ) ;
179
- const canSet = this . core . canSetAsMixin ( node , mixinPath ) ;
180
- if ( canSet . isOk ) {
181
- this . core . addMixin ( node , mixinPath ) ;
182
- return Result . Ok ( new PatchResult ( change ) ) ;
183
- } else {
184
- const err = new PatchError (
185
- `Cannot set ${ mixinId } as mixin for ${ this . core . getPath ( node ) } : ${ canSet . reason } `
186
- ) ;
187
- return Result . Error ( err ) ;
188
- }
189
195
} ;
190
196
191
197
}
@@ -373,32 +379,39 @@ export class MemberAttributesPatch extends NodeStatePatch {
373
379
export class MemberRegistryPatch extends NodeStatePatch {
374
380
put = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
375
381
const [ /*type*/ , set , nodeId , name ] = change . key ;
382
+ const parent = this . core . getParent ( node )
383
+ const parentPath = parent ? this . core . getPath ( parent ) : '' ;
376
384
const isNewSet = nodeId === undefined ;
377
385
const isNewMember = name === undefined ;
378
- if ( isNewSet || isNewMember ) {
379
- const changesets = Object . entries ( change . value )
380
- . map ( entry => ( {
381
- type : 'put' ,
382
- key : change . key . concat ( [ entry [ 0 ] ] ) ,
383
- value : entry [ 1 ] ,
384
- } ) ) ;
386
+ return await change . asResult ( ) . mapAsync ( async chg => {
387
+ if ( isNewSet || isNewMember ) {
388
+ const changesets = Object . entries ( chg . value )
389
+ . map ( entry => {
390
+ const changeSet : ChangeSet = ( {
391
+ type : ChangeType . PUT ,
392
+ key : chg . key . concat ( [ entry [ 0 ] ] ) ,
393
+ value : entry [ 1 ] ,
394
+ } ) ;
395
+ return NodeChangeSet . fromChangeSet ( parentPath , nodeId , changeSet ) ;
396
+ } ) ;
385
397
386
- for ( let i = changesets . length ; i -- ; ) {
387
- await this . put ( node , changesets [ i ] , resolvedSelectors ) ;
388
- }
389
- } else {
390
- const gmeId = await this . nodeSearchUtils . getNodeId ( node , nodeId , resolvedSelectors ) ;
391
- const isNested = change . key . length > 4 ;
392
- if ( isNested ) {
393
- const value = this . core . getMemberRegistry ( node , set , gmeId , name ) ;
394
- setNested ( value , change . key . slice ( 4 ) , change . value ) ;
395
- this . core . setMemberRegistry ( node , set , gmeId , name , value ) ;
398
+ for ( let i = changesets . length ; i -- ; ) {
399
+ await this . put ( node , changesets [ i ] , resolvedSelectors ) ;
400
+ }
396
401
} else {
397
- this . core . setMemberRegistry ( node , set , gmeId , name , change . value ) ;
402
+ const gmeId = await this . nodeSearchUtils . getNodeId ( node , nodeId , resolvedSelectors ) ;
403
+ const isNested = chg . key . length > 4 ;
404
+ if ( isNested ) {
405
+ const value = this . core . getMemberRegistry ( node , set , gmeId , name ) ;
406
+ setNested ( value , chg . key . slice ( 4 ) , chg . value ) ;
407
+ this . core . setMemberRegistry ( node , set , gmeId , name , value ) ;
408
+ } else {
409
+ this . core . setMemberRegistry ( node , set , gmeId , name , chg . value ) ;
410
+ }
398
411
}
399
- }
412
+ return Maybe . some ( new PatchResult ( chg ) ) ;
413
+ } ) ;
400
414
401
- return Result . Ok ( new PatchResult ( change ) ) ;
402
415
}
403
416
404
417
delete = async ( node : Core . Node , change : NodeChangeSet , resolvedSelectors : NodeSelections ) : Promise < Result < PatchResult , PatchError > > => {
0 commit comments