diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-11-16 03:00:45 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-11-16 03:11:00 +0100 |
commit | 5810a7eb3b19ef9868db170c9214686bfc613eee (patch) | |
tree | da122997d4ad58f4104d0f84b06a00fe14e7ad02 /language-web/src/main/js/xtext/UpdateService.ts | |
parent | feat(lang): basic formatting (diff) | |
download | refinery-5810a7eb3b19ef9868db170c9214686bfc613eee.tar.gz refinery-5810a7eb3b19ef9868db170c9214686bfc613eee.tar.zst refinery-5810a7eb3b19ef9868db170c9214686bfc613eee.zip |
chore(web): json validation with zod
Use the zod library instead of manually written type assertions for
validating json messages from the server. This makes it easier to add
and handle new messages.
Diffstat (limited to 'language-web/src/main/js/xtext/UpdateService.ts')
-rw-r--r-- | language-web/src/main/js/xtext/UpdateService.ts | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/language-web/src/main/js/xtext/UpdateService.ts b/language-web/src/main/js/xtext/UpdateService.ts index 9b672e79..fa48c5ab 100644 --- a/language-web/src/main/js/xtext/UpdateService.ts +++ b/language-web/src/main/js/xtext/UpdateService.ts | |||
@@ -11,10 +11,10 @@ import { ConditionVariable } from '../utils/ConditionVariable'; | |||
11 | import { getLogger } from '../utils/logger'; | 11 | import { getLogger } from '../utils/logger'; |
12 | import { Timer } from '../utils/Timer'; | 12 | import { Timer } from '../utils/Timer'; |
13 | import { | 13 | import { |
14 | IContentAssistEntry, | 14 | ContentAssistEntry, |
15 | isContentAssistResult, | 15 | contentAssistResult, |
16 | isDocumentStateResult, | 16 | documentStateResult, |
17 | isInvalidStateIdConflictResult, | 17 | isConflictResult, |
18 | } from './xtextServiceResults'; | 18 | } from './xtextServiceResults'; |
19 | 19 | ||
20 | const UPDATE_TIMEOUT_MS = 500; | 20 | const UPDATE_TIMEOUT_MS = 500; |
@@ -116,11 +116,8 @@ export class UpdateService { | |||
116 | serviceType: 'update', | 116 | serviceType: 'update', |
117 | fullText: this.store.state.doc.sliceString(0), | 117 | fullText: this.store.state.doc.sliceString(0), |
118 | }); | 118 | }); |
119 | if (isDocumentStateResult(result)) { | 119 | const { stateId } = documentStateResult.parse(result); |
120 | return [result.stateId, undefined]; | 120 | return [stateId, undefined]; |
121 | } | ||
122 | log.error('Unexpected full text update result:', result); | ||
123 | throw new Error('Full text update failed'); | ||
124 | } | 121 | } |
125 | 122 | ||
126 | /** | 123 | /** |
@@ -146,14 +143,14 @@ export class UpdateService { | |||
146 | requiredStateId: this.xtextStateId, | 143 | requiredStateId: this.xtextStateId, |
147 | ...delta, | 144 | ...delta, |
148 | }); | 145 | }); |
149 | if (isDocumentStateResult(result)) { | 146 | const parsedDocumentStateResult = documentStateResult.safeParse(result); |
150 | return [result.stateId, undefined]; | 147 | if (parsedDocumentStateResult.success) { |
148 | return [parsedDocumentStateResult.data.stateId, undefined]; | ||
151 | } | 149 | } |
152 | if (isInvalidStateIdConflictResult(result)) { | 150 | if (isConflictResult(result, 'invalidStateId')) { |
153 | return this.doFallbackToUpdateFullText(); | 151 | return this.doFallbackToUpdateFullText(); |
154 | } | 152 | } |
155 | log.error('Unexpected delta text update result:', result); | 153 | throw parsedDocumentStateResult.error; |
156 | throw new Error('Delta text update failed'); | ||
157 | }); | 154 | }); |
158 | } | 155 | } |
159 | 156 | ||
@@ -171,7 +168,7 @@ export class UpdateService { | |||
171 | async fetchContentAssist( | 168 | async fetchContentAssist( |
172 | params: Record<string, unknown>, | 169 | params: Record<string, unknown>, |
173 | signal: IAbortSignal, | 170 | signal: IAbortSignal, |
174 | ): Promise<IContentAssistEntry[]> { | 171 | ): Promise<ContentAssistEntry[]> { |
175 | await this.prepareForDeltaUpdate(); | 172 | await this.prepareForDeltaUpdate(); |
176 | if (signal.aborted) { | 173 | if (signal.aborted) { |
177 | return []; | 174 | return []; |
@@ -185,18 +182,19 @@ export class UpdateService { | |||
185 | requiredStateId: this.xtextStateId, | 182 | requiredStateId: this.xtextStateId, |
186 | ...delta, | 183 | ...delta, |
187 | }); | 184 | }); |
188 | if (isContentAssistResult(result)) { | 185 | const parsedContentAssistResult = contentAssistResult.safeParse(result); |
189 | return [result.stateId, result.entries]; | 186 | if (parsedContentAssistResult.success) { |
187 | const { stateId, entries: resultEntries } = parsedContentAssistResult.data; | ||
188 | return [stateId, resultEntries]; | ||
190 | } | 189 | } |
191 | if (isInvalidStateIdConflictResult(result)) { | 190 | if (isConflictResult(result, 'invalidStateId')) { |
192 | const [newStateId] = await this.doFallbackToUpdateFullText(); | 191 | const [newStateId] = await this.doFallbackToUpdateFullText(); |
193 | // We must finish this state update transaction to prepare for any push events | 192 | // We must finish this state update transaction to prepare for any push events |
194 | // before querying for content assist, so we just return `null` and will query | 193 | // before querying for content assist, so we just return `null` and will query |
195 | // the content assist service later. | 194 | // the content assist service later. |
196 | return [newStateId, null]; | 195 | return [newStateId, null]; |
197 | } | 196 | } |
198 | log.error('Unextpected content assist result with delta update', result); | 197 | throw parsedContentAssistResult.error; |
199 | throw new Error('Unexpexted content assist result with delta update'); | ||
200 | }); | 198 | }); |
201 | if (entries !== null) { | 199 | if (entries !== null) { |
202 | return entries; | 200 | return entries; |
@@ -214,11 +212,11 @@ export class UpdateService { | |||
214 | ...params, | 212 | ...params, |
215 | requiredStateId: expectedStateId, | 213 | requiredStateId: expectedStateId, |
216 | }); | 214 | }); |
217 | if (isContentAssistResult(result) && result.stateId === expectedStateId) { | 215 | const { stateId, entries } = contentAssistResult.parse(result); |
218 | return result.entries; | 216 | if (stateId !== expectedStateId) { |
217 | throw new Error(`Unexpected state id, expected: ${expectedStateId} got: ${stateId}`); | ||
219 | } | 218 | } |
220 | log.error('Unexpected content assist result', result); | 219 | return entries; |
221 | throw new Error('Unexpected content assist result'); | ||
222 | } | 220 | } |
223 | 221 | ||
224 | private computeDelta() { | 222 | private computeDelta() { |