@istar-ts/actions 0.15.0-beta.4 → 0.15.0-beta.6

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.
package/dist/index.cjs CHANGED
@@ -66,6 +66,141 @@ function isRequestAction(action) {
66
66
  return REQUEST_KINDS.has(action.kind);
67
67
  }
68
68
  //#endregion
69
+ //#region src/created-ids.ts
70
+ /** How many new element ids, then new link ids, `operation` creates, mirroring the dispatcher's
71
+ * `createdIds` diff (`dispatcher.ts`) and `connect`/`addDependency`/`addLeg`
72
+ * (`packages/core/src/operations.ts`). */
73
+ function countCreatedIds(operation, model) {
74
+ switch (operation.kind) {
75
+ case "createNode": return {
76
+ elements: 1,
77
+ links: 0
78
+ };
79
+ case "createEdge": {
80
+ const metamodel = (0, _istar_ts_core.metamodelOf)(model);
81
+ const definition = metamodel.links.get(operation.elementTypeId);
82
+ if (definition?.category === "dependency") return {
83
+ elements: 1,
84
+ links: 2
85
+ };
86
+ if (definition?.parts && !targetIsHub(operation, model, metamodel)) return {
87
+ elements: 1,
88
+ links: 2
89
+ };
90
+ return {
91
+ elements: 0,
92
+ links: 1
93
+ };
94
+ }
95
+ case "istar.addLeg": return {
96
+ elements: 0,
97
+ links: 1
98
+ };
99
+ case "compound": {
100
+ let elements = 0;
101
+ let links = 0;
102
+ for (const child of operation.operationList) {
103
+ const counts = countCreatedIds(child, model);
104
+ elements += counts.elements;
105
+ links += counts.links;
106
+ }
107
+ return {
108
+ elements,
109
+ links
110
+ };
111
+ }
112
+ default: return {
113
+ elements: 0,
114
+ links: 0
115
+ };
116
+ }
117
+ }
118
+ function targetIsHub(operation, model, metamodel) {
119
+ const targetKind = model.elements.get(operation.targetElementId)?.kind;
120
+ return targetKind !== void 0 && metamodel.elements.get(targetKind)?.role === "hub";
121
+ }
122
+ /** `operation` with ids from `elementIds`/`linkIds` (consumed in model order) injected as the
123
+ * explicit ids its creates take, so a dispatcher that applies it against the same model state
124
+ * creates the same ids. */
125
+ function withCreatedIds(operation, elementIds, linkIds, model) {
126
+ switch (operation.kind) {
127
+ case "createNode": {
128
+ const id = elementIds.shift();
129
+ return id === void 0 ? operation : {
130
+ ...operation,
131
+ args: {
132
+ ...operation.args,
133
+ id
134
+ }
135
+ };
136
+ }
137
+ case "createEdge": {
138
+ const metamodel = (0, _istar_ts_core.metamodelOf)(model);
139
+ const definition = metamodel.links.get(operation.elementTypeId);
140
+ if (definition?.category === "dependency") {
141
+ const dependumId = elementIds.shift();
142
+ const linkIds2 = [linkIds.shift(), linkIds.shift()];
143
+ if (dependumId === void 0 || linkIds2[0] === void 0 || linkIds2[1] === void 0) return operation;
144
+ return {
145
+ ...operation,
146
+ args: {
147
+ ...operation.args,
148
+ dependum: {
149
+ ...operation.args?.dependum,
150
+ id: dependumId
151
+ },
152
+ linkIds: linkIds2
153
+ }
154
+ };
155
+ }
156
+ if (definition?.parts && !targetIsHub(operation, model, metamodel)) {
157
+ const hubId = elementIds.shift();
158
+ const legId = linkIds.shift();
159
+ const endId = linkIds.shift();
160
+ return hubId === void 0 || legId === void 0 || endId === void 0 ? operation : {
161
+ ...operation,
162
+ args: {
163
+ ...operation.args,
164
+ id: hubId,
165
+ legId,
166
+ endId
167
+ }
168
+ };
169
+ }
170
+ const id = linkIds.shift();
171
+ return id === void 0 ? operation : {
172
+ ...operation,
173
+ args: {
174
+ ...operation.args,
175
+ id
176
+ }
177
+ };
178
+ }
179
+ case "istar.addLeg": {
180
+ const id = linkIds.shift();
181
+ return id === void 0 ? operation : {
182
+ ...operation,
183
+ args: {
184
+ ...operation.args,
185
+ id
186
+ }
187
+ };
188
+ }
189
+ case "compound": return {
190
+ ...operation,
191
+ operationList: operation.operationList.map((child) => withCreatedIds(child, elementIds, linkIds, model))
192
+ };
193
+ default: return operation;
194
+ }
195
+ }
196
+ /** `operation`, with `createdIds` (an accepted result's) injected as explicit ids. */
197
+ function injectCreatedIds(operation, createdIds, model) {
198
+ const counts = countCreatedIds(operation, model);
199
+ const elementIds = createdIds.slice(0, counts.elements);
200
+ const linkIds = createdIds.slice(counts.elements, counts.elements + counts.links);
201
+ return withCreatedIds(operation, [...elementIds], [...linkIds], model);
202
+ }
203
+ //#endregion
69
204
  //#region src/hints.ts
70
205
  /**
71
206
  * What a client can know before it asks: type hints from the metamodel, and the palette as
@@ -1267,6 +1402,7 @@ exports.createMirrorStore = createMirrorStore;
1267
1402
  exports.createRemoteDispatcher = createRemoteDispatcher;
1268
1403
  exports.diagnosticToMarker = diagnosticToMarker;
1269
1404
  exports.hostToolContext = hostToolContext;
1405
+ exports.injectCreatedIds = injectCreatedIds;
1270
1406
  exports.isOperation = isOperation;
1271
1407
  exports.isRequestAction = isRequestAction;
1272
1408
  exports.markerToDiagnostic = markerToDiagnostic;