diff options
author | 2022-08-12 19:54:46 +0200 | |
---|---|---|
committer | 2022-08-12 19:54:46 +0200 | |
commit | d22c3b0c257f5daf5b401988a35ab9ce981a2341 (patch) | |
tree | 0a661c927c37b52197326d1c05e211daf9bd19e5 /subprojects/frontend/src/xtext/UpdateService.ts | |
parent | fix(language): rule parsing test (diff) | |
download | refinery-d22c3b0c257f5daf5b401988a35ab9ce981a2341.tar.gz refinery-d22c3b0c257f5daf5b401988a35ab9ce981a2341.tar.zst refinery-d22c3b0c257f5daf5b401988a35ab9ce981a2341.zip |
refactor(frontend): move from Webpack to Vite
Also overhaulds the building and linting for frontend assets.
Diffstat (limited to 'subprojects/frontend/src/xtext/UpdateService.ts')
-rw-r--r-- | subprojects/frontend/src/xtext/UpdateService.ts | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/subprojects/frontend/src/xtext/UpdateService.ts b/subprojects/frontend/src/xtext/UpdateService.ts index e78944a9..2994b11b 100644 --- a/subprojects/frontend/src/xtext/UpdateService.ts +++ b/subprojects/frontend/src/xtext/UpdateService.ts | |||
@@ -1,22 +1,23 @@ | |||
1 | import { | 1 | import { |
2 | ChangeDesc, | 2 | type ChangeDesc, |
3 | ChangeSet, | 3 | ChangeSet, |
4 | ChangeSpec, | 4 | type ChangeSpec, |
5 | StateEffect, | 5 | StateEffect, |
6 | Transaction, | 6 | type Transaction, |
7 | } from '@codemirror/state'; | 7 | } from '@codemirror/state'; |
8 | import { nanoid } from 'nanoid'; | 8 | import { nanoid } from 'nanoid'; |
9 | 9 | ||
10 | import type { EditorStore } from '../editor/EditorStore'; | 10 | import type EditorStore from '../editor/EditorStore'; |
11 | import type { XtextWebSocketClient } from './XtextWebSocketClient'; | 11 | import ConditionVariable from '../utils/ConditionVariable'; |
12 | import { ConditionVariable } from '../utils/ConditionVariable'; | 12 | import Timer from '../utils/Timer'; |
13 | import { getLogger } from '../utils/logger'; | 13 | import getLogger from '../utils/getLogger'; |
14 | import { Timer } from '../utils/Timer'; | 14 | |
15 | import type XtextWebSocketClient from './XtextWebSocketClient'; | ||
15 | import { | 16 | import { |
16 | ContentAssistEntry, | 17 | type ContentAssistEntry, |
17 | contentAssistResult, | 18 | ContentAssistResult, |
18 | documentStateResult, | 19 | DocumentStateResult, |
19 | formattingResult, | 20 | FormattingResult, |
20 | isConflictResult, | 21 | isConflictResult, |
21 | } from './xtextServiceResults'; | 22 | } from './xtextServiceResults'; |
22 | 23 | ||
@@ -32,7 +33,7 @@ export interface IAbortSignal { | |||
32 | aborted: boolean; | 33 | aborted: boolean; |
33 | } | 34 | } |
34 | 35 | ||
35 | export class UpdateService { | 36 | export default class UpdateService { |
36 | resourceName: string; | 37 | resourceName: string; |
37 | 38 | ||
38 | xtextStateId: string | null = null; | 39 | xtextStateId: string | null = null; |
@@ -76,8 +77,8 @@ export class UpdateService { | |||
76 | } | 77 | } |
77 | 78 | ||
78 | onTransaction(transaction: Transaction): void { | 79 | onTransaction(transaction: Transaction): void { |
79 | const setDirtyChangesEffect = transaction.effects.find( | 80 | const setDirtyChangesEffect = transaction.effects.find((effect) => |
80 | (effect) => effect.is(setDirtyChanges), | 81 | effect.is(setDirtyChanges), |
81 | ) as StateEffect<ChangeSet> | undefined; | 82 | ) as StateEffect<ChangeSet> | undefined; |
82 | if (setDirtyChangesEffect) { | 83 | if (setDirtyChangesEffect) { |
83 | const { value } = setDirtyChangesEffect; | 84 | const { value } = setDirtyChangesEffect; |
@@ -102,7 +103,10 @@ export class UpdateService { | |||
102 | * @return the summary of changes since the last update | 103 | * @return the summary of changes since the last update |
103 | */ | 104 | */ |
104 | computeChangesSinceLastUpdate(): ChangeDesc { | 105 | computeChangesSinceLastUpdate(): ChangeDesc { |
105 | return this.pendingUpdate?.composeDesc(this.dirtyChanges.desc) || this.dirtyChanges.desc; | 106 | return ( |
107 | this.pendingUpdate?.composeDesc(this.dirtyChanges.desc) || | ||
108 | this.dirtyChanges.desc | ||
109 | ); | ||
106 | } | 110 | } |
107 | 111 | ||
108 | private handleIdleUpdate() { | 112 | private handleIdleUpdate() { |
@@ -131,7 +135,7 @@ export class UpdateService { | |||
131 | serviceType: 'update', | 135 | serviceType: 'update', |
132 | fullText: this.store.state.doc.sliceString(0), | 136 | fullText: this.store.state.doc.sliceString(0), |
133 | }); | 137 | }); |
134 | const { stateId } = documentStateResult.parse(result); | 138 | const { stateId } = DocumentStateResult.parse(result); |
135 | return [stateId, undefined]; | 139 | return [stateId, undefined]; |
136 | } | 140 | } |
137 | 141 | ||
@@ -158,7 +162,7 @@ export class UpdateService { | |||
158 | requiredStateId: this.xtextStateId, | 162 | requiredStateId: this.xtextStateId, |
159 | ...delta, | 163 | ...delta, |
160 | }); | 164 | }); |
161 | const parsedDocumentStateResult = documentStateResult.safeParse(result); | 165 | const parsedDocumentStateResult = DocumentStateResult.safeParse(result); |
162 | if (parsedDocumentStateResult.success) { | 166 | if (parsedDocumentStateResult.success) { |
163 | return [parsedDocumentStateResult.data.stateId, undefined]; | 167 | return [parsedDocumentStateResult.data.stateId, undefined]; |
164 | } | 168 | } |
@@ -197,9 +201,10 @@ export class UpdateService { | |||
197 | requiredStateId: this.xtextStateId, | 201 | requiredStateId: this.xtextStateId, |
198 | ...delta, | 202 | ...delta, |
199 | }); | 203 | }); |
200 | const parsedContentAssistResult = contentAssistResult.safeParse(result); | 204 | const parsedContentAssistResult = ContentAssistResult.safeParse(result); |
201 | if (parsedContentAssistResult.success) { | 205 | if (parsedContentAssistResult.success) { |
202 | const { stateId, entries: resultEntries } = parsedContentAssistResult.data; | 206 | const { stateId, entries: resultEntries } = |
207 | parsedContentAssistResult.data; | ||
203 | return [stateId, resultEntries]; | 208 | return [stateId, resultEntries]; |
204 | } | 209 | } |
205 | if (isConflictResult(result, 'invalidStateId')) { | 210 | if (isConflictResult(result, 'invalidStateId')) { |
@@ -223,14 +228,19 @@ export class UpdateService { | |||
223 | return this.doFetchContentAssist(params, this.xtextStateId as string); | 228 | return this.doFetchContentAssist(params, this.xtextStateId as string); |
224 | } | 229 | } |
225 | 230 | ||
226 | private async doFetchContentAssist(params: Record<string, unknown>, expectedStateId: string) { | 231 | private async doFetchContentAssist( |
232 | params: Record<string, unknown>, | ||
233 | expectedStateId: string, | ||
234 | ) { | ||
227 | const result = await this.webSocketClient.send({ | 235 | const result = await this.webSocketClient.send({ |
228 | ...params, | 236 | ...params, |
229 | requiredStateId: expectedStateId, | 237 | requiredStateId: expectedStateId, |
230 | }); | 238 | }); |
231 | const { stateId, entries } = contentAssistResult.parse(result); | 239 | const { stateId, entries } = ContentAssistResult.parse(result); |
232 | if (stateId !== expectedStateId) { | 240 | if (stateId !== expectedStateId) { |
233 | throw new Error(`Unexpected state id, expected: ${expectedStateId} got: ${stateId}`); | 241 | throw new Error( |
242 | `Unexpected state id, expected: ${expectedStateId} got: ${stateId}`, | ||
243 | ); | ||
234 | } | 244 | } |
235 | return entries; | 245 | return entries; |
236 | } | 246 | } |
@@ -250,7 +260,7 @@ export class UpdateService { | |||
250 | selectionStart: from, | 260 | selectionStart: from, |
251 | selectionEnd: to, | 261 | selectionEnd: to, |
252 | }); | 262 | }); |
253 | const { stateId, formattedText } = formattingResult.parse(result); | 263 | const { stateId, formattedText } = FormattingResult.parse(result); |
254 | this.applyBeforeDirtyChanges({ | 264 | this.applyBeforeDirtyChanges({ |
255 | from, | 265 | from, |
256 | to, | 266 | to, |
@@ -282,16 +292,15 @@ export class UpdateService { | |||
282 | } | 292 | } |
283 | 293 | ||
284 | private applyBeforeDirtyChanges(changeSpec: ChangeSpec) { | 294 | private applyBeforeDirtyChanges(changeSpec: ChangeSpec) { |
285 | const pendingChanges = this.pendingUpdate?.compose(this.dirtyChanges) || this.dirtyChanges; | 295 | const pendingChanges = |
296 | this.pendingUpdate?.compose(this.dirtyChanges) || this.dirtyChanges; | ||
286 | const revertChanges = pendingChanges.invert(this.store.state.doc); | 297 | const revertChanges = pendingChanges.invert(this.store.state.doc); |
287 | const applyBefore = ChangeSet.of(changeSpec, revertChanges.newLength); | 298 | const applyBefore = ChangeSet.of(changeSpec, revertChanges.newLength); |
288 | const redoChanges = pendingChanges.map(applyBefore.desc); | 299 | const redoChanges = pendingChanges.map(applyBefore.desc); |
289 | const changeSet = revertChanges.compose(applyBefore).compose(redoChanges); | 300 | const changeSet = revertChanges.compose(applyBefore).compose(redoChanges); |
290 | this.store.dispatch({ | 301 | this.store.dispatch({ |
291 | changes: changeSet, | 302 | changes: changeSet, |
292 | effects: [ | 303 | effects: [setDirtyChanges.of(redoChanges)], |
293 | setDirtyChanges.of(redoChanges), | ||
294 | ], | ||
295 | }); | 304 | }); |
296 | } | 305 | } |
297 | 306 | ||
@@ -316,7 +325,9 @@ export class UpdateService { | |||
316 | * @param callback the asynchronous callback that updates the server state | 325 | * @param callback the asynchronous callback that updates the server state |
317 | * @return a promise resolving to the second value returned by `callback` | 326 | * @return a promise resolving to the second value returned by `callback` |
318 | */ | 327 | */ |
319 | private async withUpdate<T>(callback: () => Promise<[string, T]>): Promise<T> { | 328 | private async withUpdate<T>( |
329 | callback: () => Promise<[string, T]>, | ||
330 | ): Promise<T> { | ||
320 | if (this.pendingUpdate !== null) { | 331 | if (this.pendingUpdate !== null) { |
321 | throw new Error('Another update is pending, will not perform update'); | 332 | throw new Error('Another update is pending, will not perform update'); |
322 | } | 333 | } |