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

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.d.cts CHANGED
@@ -501,6 +501,11 @@ export declare function isOperation<EK extends string, LK extends string>(action
501
501
  /** True for the requests (actions answered with a response action). */
502
502
  export declare function isRequestAction(action: Action): action is IstarRequestAction;
503
503
  //#endregion
504
+ //#region src/created-ids.d.ts
505
+ type Operation$1 = IstarOperation<string, string>;
506
+ /** `operation`, with `createdIds` (an accepted result's) injected as explicit ids. */
507
+ export declare function injectCreatedIds(operation: Operation$1, createdIds: readonly string[], model: AnyIstarModel): Operation$1;
508
+ //#endregion
504
509
  //#region src/dispatcher.d.ts
505
510
  /** The response a request is answered with; `never` for other actions. */
506
511
  export type ResponseFor<A> = A extends RequestModelAction ? SetModelAction : A extends RequestTypeHintsAction ? SetTypeHintsAction : A extends RequestCheckEdgeAction ? CheckEdgeResultAction : A extends RequestContextActions ? SetContextActions : A extends RequestMarkersAction ? SetMarkersAction : never;
package/dist/index.d.ts CHANGED
@@ -501,6 +501,11 @@ export declare function isOperation<EK extends string, LK extends string>(action
501
501
  /** True for the requests (actions answered with a response action). */
502
502
  export declare function isRequestAction(action: Action): action is IstarRequestAction;
503
503
  //#endregion
504
+ //#region src/created-ids.d.ts
505
+ type Operation$1 = IstarOperation<string, string>;
506
+ /** `operation`, with `createdIds` (an accepted result's) injected as explicit ids. */
507
+ export declare function injectCreatedIds(operation: Operation$1, createdIds: readonly string[], model: AnyIstarModel): Operation$1;
508
+ //#endregion
504
509
  //#region src/dispatcher.d.ts
505
510
  /** The response a request is answered with; `never` for other actions. */
506
511
  export type ResponseFor<A> = A extends RequestModelAction ? SetModelAction : A extends RequestTypeHintsAction ? SetTypeHintsAction : A extends RequestCheckEdgeAction ? CheckEdgeResultAction : A extends RequestContextActions ? SetContextActions : A extends RequestMarkersAction ? SetMarkersAction : never;
package/dist/index.js CHANGED
@@ -65,6 +65,141 @@ function isRequestAction(action) {
65
65
  return REQUEST_KINDS.has(action.kind);
66
66
  }
67
67
  //#endregion
68
+ //#region src/created-ids.ts
69
+ /** How many new element ids, then new link ids, `operation` creates, mirroring the dispatcher's
70
+ * `createdIds` diff (`dispatcher.ts`) and `connect`/`addDependency`/`addLeg`
71
+ * (`packages/core/src/operations.ts`). */
72
+ function countCreatedIds(operation, model) {
73
+ switch (operation.kind) {
74
+ case "createNode": return {
75
+ elements: 1,
76
+ links: 0
77
+ };
78
+ case "createEdge": {
79
+ const metamodel = metamodelOf(model);
80
+ const definition = metamodel.links.get(operation.elementTypeId);
81
+ if (definition?.category === "dependency") return {
82
+ elements: 1,
83
+ links: 2
84
+ };
85
+ if (definition?.parts && !targetIsHub(operation, model, metamodel)) return {
86
+ elements: 1,
87
+ links: 2
88
+ };
89
+ return {
90
+ elements: 0,
91
+ links: 1
92
+ };
93
+ }
94
+ case "istar.addLeg": return {
95
+ elements: 0,
96
+ links: 1
97
+ };
98
+ case "compound": {
99
+ let elements = 0;
100
+ let links = 0;
101
+ for (const child of operation.operationList) {
102
+ const counts = countCreatedIds(child, model);
103
+ elements += counts.elements;
104
+ links += counts.links;
105
+ }
106
+ return {
107
+ elements,
108
+ links
109
+ };
110
+ }
111
+ default: return {
112
+ elements: 0,
113
+ links: 0
114
+ };
115
+ }
116
+ }
117
+ function targetIsHub(operation, model, metamodel) {
118
+ const targetKind = model.elements.get(operation.targetElementId)?.kind;
119
+ return targetKind !== void 0 && metamodel.elements.get(targetKind)?.role === "hub";
120
+ }
121
+ /** `operation` with ids from `elementIds`/`linkIds` (consumed in model order) injected as the
122
+ * explicit ids its creates take, so a dispatcher that applies it against the same model state
123
+ * creates the same ids. */
124
+ function withCreatedIds(operation, elementIds, linkIds, model) {
125
+ switch (operation.kind) {
126
+ case "createNode": {
127
+ const id = elementIds.shift();
128
+ return id === void 0 ? operation : {
129
+ ...operation,
130
+ args: {
131
+ ...operation.args,
132
+ id
133
+ }
134
+ };
135
+ }
136
+ case "createEdge": {
137
+ const metamodel = metamodelOf(model);
138
+ const definition = metamodel.links.get(operation.elementTypeId);
139
+ if (definition?.category === "dependency") {
140
+ const dependumId = elementIds.shift();
141
+ const linkIds2 = [linkIds.shift(), linkIds.shift()];
142
+ if (dependumId === void 0 || linkIds2[0] === void 0 || linkIds2[1] === void 0) return operation;
143
+ return {
144
+ ...operation,
145
+ args: {
146
+ ...operation.args,
147
+ dependum: {
148
+ ...operation.args?.dependum,
149
+ id: dependumId
150
+ },
151
+ linkIds: linkIds2
152
+ }
153
+ };
154
+ }
155
+ if (definition?.parts && !targetIsHub(operation, model, metamodel)) {
156
+ const hubId = elementIds.shift();
157
+ const legId = linkIds.shift();
158
+ const endId = linkIds.shift();
159
+ return hubId === void 0 || legId === void 0 || endId === void 0 ? operation : {
160
+ ...operation,
161
+ args: {
162
+ ...operation.args,
163
+ id: hubId,
164
+ legId,
165
+ endId
166
+ }
167
+ };
168
+ }
169
+ const id = linkIds.shift();
170
+ return id === void 0 ? operation : {
171
+ ...operation,
172
+ args: {
173
+ ...operation.args,
174
+ id
175
+ }
176
+ };
177
+ }
178
+ case "istar.addLeg": {
179
+ const id = linkIds.shift();
180
+ return id === void 0 ? operation : {
181
+ ...operation,
182
+ args: {
183
+ ...operation.args,
184
+ id
185
+ }
186
+ };
187
+ }
188
+ case "compound": return {
189
+ ...operation,
190
+ operationList: operation.operationList.map((child) => withCreatedIds(child, elementIds, linkIds, model))
191
+ };
192
+ default: return operation;
193
+ }
194
+ }
195
+ /** `operation`, with `createdIds` (an accepted result's) injected as explicit ids. */
196
+ function injectCreatedIds(operation, createdIds, model) {
197
+ const counts = countCreatedIds(operation, model);
198
+ const elementIds = createdIds.slice(0, counts.elements);
199
+ const linkIds = createdIds.slice(counts.elements, counts.elements + counts.links);
200
+ return withCreatedIds(operation, [...elementIds], [...linkIds], model);
201
+ }
202
+ //#endregion
68
203
  //#region src/hints.ts
69
204
  /**
70
205
  * What a client can know before it asks: type hints from the metamodel, and the palette as
@@ -1256,6 +1391,6 @@ function createRemoteDispatcher(port) {
1256
1391
  /** Version of the action protocol this package speaks. */
1257
1392
  const ACTIONS_PROTOCOL_VERSION = "0.0.0";
1258
1393
  //#endregion
1259
- export { ACTIONS_PROTOCOL_VERSION, HANDLED_ACTION_KINDS, connectDispatcher, contextActionsFor, contextText, createActionDispatcher, createMirrorStore, createRemoteDispatcher, diagnosticToMarker, hostToolContext, isOperation, isRequestAction, markerToDiagnostic, paletteFromMetamodel, readOnlyDispatcher, typeHintsFor };
1394
+ export { ACTIONS_PROTOCOL_VERSION, HANDLED_ACTION_KINDS, connectDispatcher, contextActionsFor, contextText, createActionDispatcher, createMirrorStore, createRemoteDispatcher, diagnosticToMarker, hostToolContext, injectCreatedIds, isOperation, isRequestAction, markerToDiagnostic, paletteFromMetamodel, readOnlyDispatcher, typeHintsFor };
1260
1395
 
1261
1396
  //# sourceMappingURL=index.js.map