@@ -34,14 +34,13 @@ import {
3434 delay ,
3535 delayWhen ,
3636 filter ,
37- finalize ,
3837 first ,
3938 forkJoin ,
4039 map ,
4140 mergeMap ,
4241 Observable ,
4342 of ,
44- retryWhen ,
43+ retry ,
4544 Subscription ,
4645 switchMap ,
4746 tap ,
@@ -86,9 +85,9 @@ export class EditorService {
8685 private filterAttributes : FilterAttributesService = inject ( FILTER_ATTRIBUTES ) ;
8786 private configurationService : ConfigurationService = inject ( ConfigurationService ) ;
8887
89- private validateModel$ = new BehaviorSubject < boolean > ( this . settings . autoValidationEnabled ) ;
9088 private validateModelSubscription$ : Subscription ;
91- private lastSavedRDF$ = new BehaviorSubject < Partial < ILastSavedModel > > ( { } ) ;
89+ private saveModelSubscription$ : Subscription ;
90+
9291 private isAllShapesExpandedSubject = new BehaviorSubject < boolean > ( true ) ;
9392
9493 public isAllShapesExpanded$ = this . isAllShapesExpandedSubject . asObservable ( ) ;
@@ -123,7 +122,7 @@ export class EditorService {
123122 private titleService : TitleService ,
124123 private sidebarService : SidebarStateService ,
125124 private shapeSettingsStateService : ShapeSettingsStateService ,
126- private modelSavingTrackerService : ModelSavingTrackerService ,
125+ private modelSavingTracker : ModelSavingTrackerService ,
127126 private electronSignalsService : ElectronSignalsService ,
128127 private largeFileWarningService : LargeFileWarningService ,
129128 private loadingScreenService : LoadingScreenService ,
@@ -137,22 +136,11 @@ export class EditorService {
137136 }
138137 }
139138
140- removeLastSavedRdf ( ) {
141- this . lastSavedRDF$ . next ( { rdf : null , changed : true , date : null } ) ;
142- }
143-
144- updateLastSavedRdf ( changed : boolean , model : string , saveDate : Date ) {
145- this . lastSavedRDF$ . next ( {
146- changed : changed ,
147- rdf : model ,
148- date : saveDate ,
149- } ) ;
150- }
151-
152139 initCanvas ( ) : void {
153140 this . mxGraphService . initGraph ( ) ;
154141
155- this . startValidateModel ( ) ;
142+ this . enableAutoValidation ( ) ;
143+ this . enableAutoSave ( ) ;
156144
157145 mxEvent . addMouseWheelListener (
158146 mxUtils . bind ( this , ( evt , up ) => {
@@ -249,7 +237,6 @@ export class EditorService {
249237
250238 loadNewAspectModel ( payload : LoadModelPayload ) {
251239 this . sidebarService . workspace . refresh ( ) ;
252- this . removeLastSavedRdf ( ) ;
253240 this . notificationsService . info ( { title : 'Loading model' , timeout : 2000 } ) ;
254241
255242 let rdfModel : RdfModel = null ;
@@ -266,7 +253,7 @@ export class EditorService {
266253 ) ,
267254 ) ,
268255 tap ( ( ) => {
269- this . modelSavingTrackerService . updateSavedModel ( ) ;
256+ this . modelSavingTracker . updateSavedModel ( ) ;
270257 const [ namespace , version , file ] = ( payload . namespaceFileName || this . rdfService . currentRdfModel . absoluteAspectModelFileName ) . split (
271258 ':' ,
272259 ) ;
@@ -705,16 +692,14 @@ export class EditorService {
705692 } ) ;
706693 }
707694
708- refreshValidateModel ( ) {
709- this . validateModel$ . next ( this . settings . autoValidationEnabled ) ;
695+ enableAutoValidation ( ) {
696+ this . settings . autoValidationEnabled ? this . startValidateModel ( ) : this . stopValidateModel ( ) ;
710697 }
711698
712699 startValidateModel ( ) {
713700 this . stopValidateModel ( ) ;
714701 localStorage . removeItem ( ValidateStatus . validating ) ;
715- if ( this . settings . autoValidationEnabled ) {
716- this . validateModelSubscription$ = this . validateModel ( ) . subscribe ( ) ;
717- }
702+ this . validateModelSubscription$ = this . autoValidateModel ( ) . subscribe ( ) ;
718703 }
719704
720705 stopValidateModel ( ) {
@@ -724,30 +709,27 @@ export class EditorService {
724709 }
725710 }
726711
727- validateModel ( ) : Observable < ViolationError [ ] > {
728- return this . validateModel$ . asObservable ( ) . pipe (
712+ autoValidateModel ( ) : Observable < ViolationError [ ] > {
713+ return of ( { } ) . pipe (
729714 delayWhen ( ( ) => timer ( this . settings . validationTimerSeconds * 1000 ) ) ,
730- switchMap ( ( ) => this . validate ( ) . pipe ( first ( ) ) ) ,
731- tap ( ( ) => {
732- localStorage . removeItem ( ValidateStatus . validating ) ;
733- this . refreshValidateModel ( ) ;
715+ switchMap ( ( ) => ( this . namespaceCacheService . currentCachedFile . hasCachedElements ( ) ? this . validate ( ) . pipe ( first ( ) ) : of ( [ ] ) ) ) ,
716+ tap ( ( ) => localStorage . removeItem ( ValidateStatus . validating ) ) ,
717+ tap ( ( ) => this . enableAutoValidation ( ) ) ,
718+ retry ( {
719+ delay : error => {
720+ if ( ! Object . values ( SaveValidateErrorsCodes ) . includes ( error ?. type ) ) {
721+ this . logService . logError ( `Error occurred while validating the current model (${ error } )` ) ;
722+ this . notificationsService . error ( {
723+ title : this . translate . language . NOTIFICATION_SERVICE . VALIDATION_ERROR_TITLE ,
724+ message : this . translate . language . NOTIFICATION_SERVICE . VALIDATION_ERROR_MESSAGE ,
725+ timeout : 5000 ,
726+ } ) ;
727+ }
728+ localStorage . removeItem ( ValidateStatus . validating ) ;
729+
730+ return timer ( this . settings . validationTimerSeconds * 1000 ) ;
731+ } ,
734732 } ) ,
735- retryWhen ( errors =>
736- errors . pipe (
737- tap ( error => {
738- if ( ! Object . values ( SaveValidateErrorsCodes ) . includes ( error ?. type ) ) {
739- this . logService . logError ( `Error occurred while validating the current model (${ error } )` ) ;
740- this . notificationsService . error ( {
741- title : this . translate . language . NOTIFICATION_SERVICE . VALIDATION_ERROR_TITLE ,
742- message : this . translate . language . NOTIFICATION_SERVICE . VALIDATION_ERROR_MESSAGE ,
743- timeout : 5000 ,
744- } ) ;
745- }
746- localStorage . removeItem ( ValidateStatus . validating ) ;
747- } ) ,
748- delayWhen ( ( ) => timer ( this . settings . validationTimerSeconds * 1000 ) ) ,
749- ) ,
750- ) ,
751733 ) ;
752734 }
753735
@@ -770,9 +752,41 @@ export class EditorService {
770752 ) ;
771753 }
772754
773- saveModel ( ) {
755+ enableAutoSave ( ) : void {
756+ this . settings . autoSaveEnabled ? this . startSaveModel ( ) : this . stopSaveModel ( ) ;
757+ }
758+
759+ startSaveModel ( ) : void {
760+ this . stopSaveModel ( ) ;
761+ this . saveModelSubscription$ = this . autoSaveModel ( ) . subscribe ( ) ;
762+ }
763+
764+ stopSaveModel ( ) {
765+ if ( this . saveModelSubscription$ ) {
766+ this . saveModelSubscription$ . unsubscribe ( ) ;
767+ }
768+ }
769+
770+ autoSaveModel ( ) : Observable < RdfModel | object > {
771+ return of ( { } ) . pipe (
772+ delayWhen ( ( ) => timer ( this . settings . saveTimerSeconds * 1000 ) ) ,
773+ switchMap ( ( ) =>
774+ this . namespaceCacheService . currentCachedFile . hasCachedElements ( ) &&
775+ ! this . rdfService . currentRdfModel . aspectModelFileName . includes ( 'empty.ttl' )
776+ ? this . saveModel ( ) . pipe ( first ( ) )
777+ : of ( [ ] ) ,
778+ ) ,
779+ tap ( ( ) => this . enableAutoSave ( ) ) ,
780+ retry ( {
781+ delay : ( ) => timer ( this . settings . saveTimerSeconds * 1000 ) ,
782+ } ) ,
783+ ) ;
784+ }
785+
786+ saveModel ( ) : Observable < RdfModel | object > {
774787 return this . modelService . saveModel ( ) . pipe (
775788 tap ( ( ) => {
789+ this . modelSavingTracker . updateSavedModel ( ) ;
776790 this . notificationsService . info ( { title : this . translate . language . NOTIFICATION_SERVICE . ASPECT_SAVED_SUCCESS } ) ;
777791 this . logService . logInfo ( 'Aspect model was saved to the local folder' ) ;
778792 this . sidebarService . workspace . refresh ( ) ;
0 commit comments