diff options
author | 2021-11-16 03:00:45 +0100 | |
---|---|---|
committer | 2021-11-16 03:11:00 +0100 | |
commit | 5810a7eb3b19ef9868db170c9214686bfc613eee (patch) | |
tree | da122997d4ad58f4104d0f84b06a00fe14e7ad02 /language-web/src/main/js/xtext/OccurrencesService.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/OccurrencesService.ts')
-rw-r--r-- | language-web/src/main/js/xtext/OccurrencesService.ts | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/language-web/src/main/js/xtext/OccurrencesService.ts b/language-web/src/main/js/xtext/OccurrencesService.ts index d1dec9e9..bc865537 100644 --- a/language-web/src/main/js/xtext/OccurrencesService.ts +++ b/language-web/src/main/js/xtext/OccurrencesService.ts | |||
@@ -7,9 +7,9 @@ import { getLogger } from '../utils/logger'; | |||
7 | import { Timer } from '../utils/Timer'; | 7 | import { Timer } from '../utils/Timer'; |
8 | import { XtextWebSocketClient } from './XtextWebSocketClient'; | 8 | import { XtextWebSocketClient } from './XtextWebSocketClient'; |
9 | import { | 9 | import { |
10 | isOccurrencesResult, | 10 | isConflictResult, |
11 | isServiceConflictResult, | 11 | occurrencesResult, |
12 | ITextRegion, | 12 | TextRegion, |
13 | } from './xtextServiceResults'; | 13 | } from './xtextServiceResults'; |
14 | 14 | ||
15 | const FIND_OCCURRENCES_TIMEOUT_MS = 1000; | 15 | const FIND_OCCURRENCES_TIMEOUT_MS = 1000; |
@@ -20,7 +20,7 @@ const CLEAR_OCCURRENCES_TIMEOUT_MS = 10; | |||
20 | 20 | ||
21 | const log = getLogger('xtext.OccurrencesService'); | 21 | const log = getLogger('xtext.OccurrencesService'); |
22 | 22 | ||
23 | function transformOccurrences(regions: ITextRegion[]): IOccurrence[] { | 23 | function transformOccurrences(regions: TextRegion[]): IOccurrence[] { |
24 | const occurrences: IOccurrence[] = []; | 24 | const occurrences: IOccurrence[] = []; |
25 | regions.forEach(({ offset, length }) => { | 25 | regions.forEach(({ offset, length }) => { |
26 | if (length > 0) { | 26 | if (length > 0) { |
@@ -87,21 +87,32 @@ export class OccurrencesService { | |||
87 | caretOffset: this.store.state.selection.main.head, | 87 | caretOffset: this.store.state.selection.main.head, |
88 | }); | 88 | }); |
89 | const allChanges = this.updateService.computeChangesSinceLastUpdate(); | 89 | const allChanges = this.updateService.computeChangesSinceLastUpdate(); |
90 | if (!allChanges.empty | 90 | if (!allChanges.empty || isConflictResult(result, 'canceled')) { |
91 | || (isServiceConflictResult(result) && result.conflict === 'canceled')) { | ||
92 | // Stale occurrences result, the user already made some changes. | 91 | // Stale occurrences result, the user already made some changes. |
93 | // We can safely ignore the occurrences and schedule a new find occurrences call. | 92 | // We can safely ignore the occurrences and schedule a new find occurrences call. |
94 | this.clearOccurrences(); | 93 | this.clearOccurrences(); |
95 | this.findOccurrencesTimer.schedule(); | 94 | this.findOccurrencesTimer.schedule(); |
96 | return; | 95 | return; |
97 | } | 96 | } |
98 | if (!isOccurrencesResult(result) || result.stateId !== this.updateService.xtextStateId) { | 97 | const parsedOccurrencesResult = occurrencesResult.safeParse(result); |
99 | log.error('Unexpected occurrences result', result); | 98 | if (!parsedOccurrencesResult.success) { |
99 | log.error( | ||
100 | 'Unexpected occurences result', | ||
101 | result, | ||
102 | 'not an OccurrencesResult: ', | ||
103 | parsedOccurrencesResult.error, | ||
104 | ); | ||
100 | this.clearOccurrences(); | 105 | this.clearOccurrences(); |
101 | return; | 106 | return; |
102 | } | 107 | } |
103 | const write = transformOccurrences(result.writeRegions); | 108 | const { stateId, writeRegions, readRegions } = parsedOccurrencesResult.data; |
104 | const read = transformOccurrences(result.readRegions); | 109 | if (stateId !== this.updateService.xtextStateId) { |
110 | log.error('Unexpected state id, expected:', this.updateService.xtextStateId, 'got:', stateId); | ||
111 | this.clearOccurrences(); | ||
112 | return; | ||
113 | } | ||
114 | const write = transformOccurrences(writeRegions); | ||
115 | const read = transformOccurrences(readRegions); | ||
105 | this.hasOccurrences = write.length > 0 || read.length > 0; | 116 | this.hasOccurrences = write.length > 0 || read.length > 0; |
106 | log.debug('Found', write.length, 'write and', read.length, 'read occurrences'); | 117 | log.debug('Found', write.length, 'write and', read.length, 'read occurrences'); |
107 | this.store.updateOccurrences(write, read); | 118 | this.store.updateOccurrences(write, read); |