@apollo-annotation/shared 0.1.17 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,182 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
- /* eslint-disable @typescript-eslint/restrict-template-expressions */
3
- /* eslint-disable @typescript-eslint/require-await */
4
- import {
5
- ChangeOptions,
6
- ClientDataStore,
7
- FeatureChange,
8
- LocalGFF3DataStore,
9
- SerializedFeatureChange,
10
- ServerDataStore,
11
- } from '@apollo-annotation/common'
12
-
13
- interface SerializedDiscontinuousLocationStartChangeBase
14
- extends SerializedFeatureChange {
15
- typeName: 'DiscontinuousLocationStartChange'
16
- }
17
-
18
- interface DiscontinuousLocationStartChangeDetails {
19
- featureId: string
20
- oldStart: number
21
- newStart: number
22
- index: number
23
- }
24
-
25
- interface SerializedDiscontinuousLocationStartChangeSingle
26
- extends SerializedDiscontinuousLocationStartChangeBase,
27
- DiscontinuousLocationStartChangeDetails {}
28
-
29
- interface SerializedDiscontinuousLocationStartChangeMultiple
30
- extends SerializedDiscontinuousLocationStartChangeBase {
31
- changes: DiscontinuousLocationStartChangeDetails[]
32
- }
33
-
34
- type SerializedDiscontinuousLocationStartChange =
35
- | SerializedDiscontinuousLocationStartChangeSingle
36
- | SerializedDiscontinuousLocationStartChangeMultiple
37
-
38
- export class DiscontinuousLocationStartChange extends FeatureChange {
39
- typeName = 'DiscontinuousLocationStartChange' as const
40
- changes: DiscontinuousLocationStartChangeDetails[]
41
-
42
- constructor(
43
- json: SerializedDiscontinuousLocationStartChange,
44
- options?: ChangeOptions,
45
- ) {
46
- super(json, options)
47
- this.changes = 'changes' in json ? json.changes : [json]
48
- }
49
-
50
- toJSON(): SerializedDiscontinuousLocationStartChange {
51
- const { assembly, changedIds, changes, typeName } = this
52
- if (changes.length === 1) {
53
- const [{ featureId, index, newStart, oldStart }] = changes
54
- return {
55
- typeName,
56
- changedIds,
57
- assembly,
58
- featureId,
59
- oldStart,
60
- newStart,
61
- index,
62
- }
63
- }
64
- return { typeName, changedIds, assembly, changes }
65
- }
66
-
67
- async executeOnServer(backend: ServerDataStore) {
68
- const { featureModel, session } = backend
69
- const { changes, logger } = this
70
- for (const change of changes) {
71
- const { featureId, index, newStart, oldStart: expectedOldStart } = change
72
- const topLevelFeature = await featureModel
73
- .findOne({ allIds: featureId })
74
- .session(session)
75
- .exec()
76
-
77
- if (!topLevelFeature) {
78
- const errMsg = `ERROR: The following featureId was not found in database ='${featureId}'`
79
- logger.error(errMsg)
80
- throw new Error(errMsg)
81
- }
82
-
83
- const feature = this.getFeatureFromId(topLevelFeature, featureId)
84
- if (!feature) {
85
- const errMsg = 'ERROR when searching feature by featureId'
86
- logger.error(errMsg)
87
- throw new Error(errMsg)
88
- }
89
- logger.debug?.(`*** Found feature: ${JSON.stringify(feature)}`)
90
- if (
91
- !feature.discontinuousLocations ||
92
- feature.discontinuousLocations.length === 0
93
- ) {
94
- const errMsg =
95
- 'Must use "LocationStartChange" to change a feature start that does not have discontinuous locations'
96
- logger.error(errMsg)
97
- throw new Error(errMsg)
98
- }
99
- const oldStart = feature.discontinuousLocations[index].start
100
- if (oldStart !== expectedOldStart) {
101
- const errMsg = `Location's current start value ${oldStart} doesn't match with expected value ${expectedOldStart}`
102
- logger.error(errMsg)
103
- throw new Error(errMsg)
104
- }
105
- const { end } = feature.discontinuousLocations[index]
106
- if (newStart >= end) {
107
- const errMsg = `location start (${newStart}) can't be larger than location end (${end})`
108
- logger.error(errMsg)
109
- throw new Error(errMsg)
110
- }
111
- const previousLocation = feature.discontinuousLocations[index - 1]
112
- if (previousLocation && newStart <= previousLocation.end) {
113
- const errMsg = `Location start (${newStart}) can't be larger than the previous location's end (${previousLocation.end})`
114
- logger.error(errMsg)
115
- throw new Error(errMsg)
116
- }
117
- feature.discontinuousLocations[index].start = newStart
118
- if (index === 0) {
119
- feature.start = newStart
120
- }
121
-
122
- try {
123
- if (topLevelFeature._id.equals(feature._id)) {
124
- topLevelFeature.markModified('discontinuousLocations')
125
- } else {
126
- topLevelFeature.markModified('children')
127
- }
128
- await topLevelFeature.save()
129
- } catch (error) {
130
- logger.debug?.(`*** FAILED: ${error}`)
131
- throw error
132
- }
133
- }
134
- }
135
-
136
- async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
137
- throw new Error('executeOnLocalGFF3 not implemented')
138
- }
139
-
140
- async executeOnClient(dataStore: ClientDataStore) {
141
- if (!dataStore) {
142
- throw new Error('No data store')
143
- }
144
- for (const [idx, changedId] of this.changedIds.entries()) {
145
- const feature = dataStore.getFeature(changedId)
146
- if (!feature) {
147
- throw new Error(`Could not find feature with identifier "${changedId}"`)
148
- }
149
- const { index, newStart } = this.changes[idx]
150
- feature.setCDSDiscontinuousLocationStart(newStart, index)
151
- }
152
- }
153
-
154
- getInverse() {
155
- const { assembly, changedIds, changes, logger, typeName } = this
156
- const inverseChangedIds = [...changedIds].reverse()
157
- const inverseChanges = [...changes].reverse().map((change) => ({
158
- featureId: change.featureId,
159
- oldStart: change.newStart,
160
- newStart: change.oldStart,
161
- index: change.index,
162
- }))
163
- return new DiscontinuousLocationStartChange(
164
- {
165
- changedIds: inverseChangedIds,
166
- typeName,
167
- changes: inverseChanges,
168
- assembly,
169
- },
170
- { logger },
171
- )
172
- }
173
- }
174
-
175
- export function isDiscontinuousLocationStartChange(
176
- change: unknown,
177
- ): change is DiscontinuousLocationStartChange {
178
- return (
179
- (change as DiscontinuousLocationStartChange).typeName ===
180
- 'DiscontinuousLocationStartChange'
181
- )
182
- }