@finos/legend-query-builder 4.14.9 → 4.14.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Copyright (c) 2020-present, Goldman Sachs
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { type RawLambda } from '@finos/legend-graph';
18
+ import {
19
+ ActionState,
20
+ LogEvent,
21
+ assertErrorThrown,
22
+ guaranteeNonNullable,
23
+ } from '@finos/legend-shared';
24
+ import { makeObservable, observable, computed, action } from 'mobx';
25
+ import type { QueryBuilderState } from './QueryBuilderState.js';
26
+ import { QUERY_BUILDER_EVENT } from '../__lib__/QueryBuilderEvent.js';
27
+
28
+ export class QueryBuilderChangeHistoryState {
29
+ readonly queryBuilderState: QueryBuilderState;
30
+ readonly initState = ActionState.create();
31
+
32
+ querySnapshotBuffer: RawLambda[] = [];
33
+ currentQuery: RawLambda | undefined;
34
+ pointer = -1;
35
+ bufferSize = 10;
36
+
37
+ constructor(queryBuilderState: QueryBuilderState) {
38
+ makeObservable(this, {
39
+ currentQuery: observable,
40
+ pointer: observable,
41
+ querySnapshotBuffer: observable,
42
+ initialize: action,
43
+ undo: action,
44
+ redo: action,
45
+ setCurrentQuery: action,
46
+ cacheNewQuery: action,
47
+ canRedo: computed,
48
+ canUndo: computed,
49
+ });
50
+
51
+ this.queryBuilderState = queryBuilderState;
52
+ }
53
+
54
+ get canRedo(): boolean {
55
+ return this.pointer < this.querySnapshotBuffer.length - 1;
56
+ }
57
+
58
+ get canUndo(): boolean {
59
+ return this.pointer > 0;
60
+ }
61
+
62
+ redo(): void {
63
+ if (this.canRedo) {
64
+ this.pointer = this.pointer + 1;
65
+ const query = this.querySnapshotBuffer[this.pointer];
66
+ this.setCurrentQuery(query);
67
+ this.queryBuilderState.rebuildWithQuery(guaranteeNonNullable(query), {
68
+ preserveParameterValues: true,
69
+ preserveResult: true,
70
+ });
71
+ }
72
+ }
73
+
74
+ undo(): void {
75
+ if (this.canUndo) {
76
+ this.pointer = this.pointer - 1;
77
+ const query = this.querySnapshotBuffer[this.pointer];
78
+ this.setCurrentQuery(query);
79
+ this.queryBuilderState.rebuildWithQuery(guaranteeNonNullable(query), {
80
+ preserveParameterValues: true,
81
+ preserveResult: true,
82
+ });
83
+ }
84
+ }
85
+
86
+ setCurrentQuery(query: RawLambda | undefined): void {
87
+ this.currentQuery = query;
88
+ }
89
+
90
+ initialize(initialQuery: RawLambda): void {
91
+ if (this.queryBuilderState.isQuerySupported) {
92
+ this.initState.inProgress();
93
+ this.currentQuery = initialQuery;
94
+ this.querySnapshotBuffer.push(initialQuery);
95
+ this.pointer = this.pointer + 1;
96
+ this.initState.complete();
97
+ }
98
+ }
99
+
100
+ cacheNewQuery(query: RawLambda): void {
101
+ try {
102
+ if (this.queryBuilderState.isQuerySupported) {
103
+ if (query.hashCode !== this.currentQuery?.hashCode) {
104
+ if (
105
+ this.querySnapshotBuffer.length === this.bufferSize &&
106
+ this.pointer === this.querySnapshotBuffer.length - 1
107
+ ) {
108
+ // only record 10 query snapshots
109
+ this.querySnapshotBuffer = this.querySnapshotBuffer.slice(1);
110
+ } else {
111
+ this.querySnapshotBuffer = this.querySnapshotBuffer.slice(
112
+ 0,
113
+ this.pointer + 1,
114
+ );
115
+ }
116
+ this.querySnapshotBuffer.push(query);
117
+ this.pointer = this.querySnapshotBuffer.length - 1;
118
+ this.setCurrentQuery(query);
119
+ }
120
+ }
121
+ } catch (error) {
122
+ assertErrorThrown(error);
123
+ this.queryBuilderState.applicationStore.logService.error(
124
+ LogEvent.create(QUERY_BUILDER_EVENT.CHANGE_HISTORY_ERROR),
125
+ `Can't cache query in query builder change history buffer: ${error.message}`,
126
+ );
127
+ }
128
+ }
129
+ }
@@ -104,6 +104,7 @@ import {
104
104
  } from './QueryBuilderExecutionContextState.js';
105
105
  import type { QueryBuilderConfig } from '../graph-manager/QueryBuilderConfig.js';
106
106
  import { QUERY_BUILDER_EVENT } from '../__lib__/QueryBuilderEvent.js';
107
+ import { QueryBuilderChangeHistoryState } from './QueryBuilderChangeHistoryState.js';
107
108
 
108
109
  export interface QuerySDLC {}
109
110
 
@@ -136,6 +137,7 @@ export abstract class QueryBuilderState implements CommandRegistrar {
136
137
  resultState: QueryBuilderResultState;
137
138
  textEditorState: QueryBuilderTextEditorState;
138
139
  unsupportedQueryState: QueryBuilderUnsupportedQueryState;
140
+ changeHistoryState: QueryBuilderChangeHistoryState;
139
141
  showFunctionsExplorerPanel = false;
140
142
  showParametersPanel = false;
141
143
  isEditingWatermark = false;
@@ -184,6 +186,7 @@ export abstract class QueryBuilderState implements CommandRegistrar {
184
186
  isCheckingEntitlments: observable,
185
187
  isCalendarEnabled: observable,
186
188
  changeDetectionState: observable,
189
+ changeHistoryState: observable,
187
190
  executionContextState: observable,
188
191
  class: observable,
189
192
  isQueryChatOpened: observable,
@@ -236,6 +239,7 @@ export abstract class QueryBuilderState implements CommandRegistrar {
236
239
  this.graphManagerState.pluginManager.getPureGraphManagerPlugins(),
237
240
  );
238
241
  this.changeDetectionState = new QueryBuilderChangeDetectionState(this);
242
+ this.changeHistoryState = new QueryBuilderChangeHistoryState(this);
239
243
  this.config = config;
240
244
  this.sourceInfo = sourceInfo;
241
245
  }
@@ -441,6 +445,7 @@ export abstract class QueryBuilderState implements CommandRegistrar {
441
445
  this.explorerState.refreshTreeData();
442
446
  this.fetchStructureState.implementation.onClassChange(val);
443
447
  this.milestoningState.updateMilestoningConfiguration();
448
+ this.changeHistoryState.cacheNewQuery(this.buildQuery());
444
449
  }
445
450
 
446
451
  changeMapping(val: Mapping, options?: { keepQueryContent?: boolean }): void {
@@ -553,6 +558,7 @@ export abstract class QueryBuilderState implements CommandRegistrar {
553
558
  });
554
559
  this.resetQueryResult();
555
560
  this.changeDetectionState.initialize(query);
561
+ this.changeHistoryState.initialize(query);
556
562
  }
557
563
 
558
564
  /**
package/tsconfig.json CHANGED
@@ -56,6 +56,7 @@
56
56
  "./src/graph-manager/protocol/pure/v1/V1_QueryBuilder_PureGraphManagerExtension.ts",
57
57
  "./src/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.ts",
58
58
  "./src/stores/QueryBuilderChangeDetectionState.ts",
59
+ "./src/stores/QueryBuilderChangeHistoryState.ts",
59
60
  "./src/stores/QueryBuilderCommand.ts",
60
61
  "./src/stores/QueryBuilderConfig.ts",
61
62
  "./src/stores/QueryBuilderConstantsState.ts",