@configura/web-api 1.4.0 → 1.6.0-alpha.0

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.
Files changed (73) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +1 -1
  3. package/dist/CatalogueAPI.d.ts +511 -504
  4. package/dist/CatalogueAPI.js +273 -248
  5. package/dist/CfgMeasure.d.ts +32 -32
  6. package/dist/CfgMeasure.js +30 -30
  7. package/dist/CfgProduct.d.ts +263 -217
  8. package/dist/CfgProduct.js +763 -636
  9. package/dist/index.d.ts +20 -16
  10. package/dist/index.js +20 -16
  11. package/dist/material/CfgMaterialMapping.d.ts +7 -7
  12. package/dist/material/CfgMaterialMapping.js +181 -181
  13. package/dist/material/CfgMtrlApplication.d.ts +18 -18
  14. package/dist/material/CfgMtrlApplication.js +43 -43
  15. package/dist/material/CfgMtrlApplicationSource.d.ts +7 -7
  16. package/dist/material/CfgMtrlApplicationSource.js +8 -8
  17. package/dist/material/CfgMtrlSource.d.ts +19 -19
  18. package/dist/material/CfgMtrlSource.js +40 -40
  19. package/dist/material/CfgMtrlSourceWithMetaData.d.ts +7 -7
  20. package/dist/material/CfgMtrlSourceWithMetaData.js +1 -1
  21. package/dist/productConfiguration/CfgFeature.d.ts +181 -160
  22. package/dist/productConfiguration/CfgFeature.js +648 -593
  23. package/dist/productConfiguration/CfgOption.d.ts +139 -128
  24. package/dist/productConfiguration/CfgOption.js +421 -394
  25. package/dist/productConfiguration/CfgProductConfiguration.d.ts +122 -120
  26. package/dist/productConfiguration/CfgProductConfiguration.js +324 -307
  27. package/dist/productConfiguration/filters.d.ts +15 -15
  28. package/dist/productConfiguration/filters.js +67 -67
  29. package/dist/productConfiguration/productParamsGenerator.d.ts +15 -15
  30. package/dist/productConfiguration/productParamsGenerator.js +51 -51
  31. package/dist/productConfiguration/utilitiesProductConfiguration.d.ts +17 -17
  32. package/dist/productConfiguration/utilitiesProductConfiguration.js +80 -80
  33. package/dist/productLoader.d.ts +33 -33
  34. package/dist/productLoader.js +49 -49
  35. package/dist/syncGroups/SyncGroupsApplier.d.ts +20 -0
  36. package/dist/syncGroups/SyncGroupsApplier.js +518 -0
  37. package/dist/syncGroups/SyncGroupsApplyMode.d.ts +15 -0
  38. package/dist/syncGroups/SyncGroupsApplyMode.js +15 -0
  39. package/dist/syncGroups/SyncGroupsHandler.d.ts +30 -0
  40. package/dist/syncGroups/SyncGroupsHandler.js +71 -0
  41. package/dist/syncGroups/SyncGroupsState.d.ts +20 -0
  42. package/dist/syncGroups/SyncGroupsState.js +61 -0
  43. package/dist/syncGroups/SyncGroupsTransaction.d.ts +50 -0
  44. package/dist/syncGroups/SyncGroupsTransaction.js +106 -0
  45. package/dist/tasks/TaskHandler.d.ts +79 -0
  46. package/dist/tasks/TaskHandler.js +265 -0
  47. package/dist/tasks/formats.d.ts +5 -0
  48. package/dist/tasks/formats.js +7 -0
  49. package/dist/tests/testData/collectorForTest.d.ts +73 -73
  50. package/dist/tests/testData/collectorForTest.js +195 -195
  51. package/dist/tests/testData/dummyProductForTest.d.ts +4 -4
  52. package/dist/tests/testData/dummyProductForTest.js +36 -36
  53. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.d.ts +32 -32
  54. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +277 -348
  55. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  56. package/dist/tests/testData/testDataCachedGetProduct.js +185 -196
  57. package/dist/tests/testData/testDataCachedPostValidate.d.ts +7 -7
  58. package/dist/tests/testData/testDataCachedPostValidate.js +183 -183
  59. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.d.ts +3 -3
  60. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.js +1099 -1099
  61. package/dist/tests/testData/testDataOptions.d.ts +13 -0
  62. package/dist/tests/testData/testDataOptions.js +60 -0
  63. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -6
  64. package/dist/tests/testData/testDataProductAggregatedPrice.js +187 -198
  65. package/dist/tests/testData/testDataUpcharge.d.ts +29 -29
  66. package/dist/tests/testData/testDataUpcharge.js +119 -151
  67. package/dist/utilitiesCatalogueData.d.ts +25 -25
  68. package/dist/utilitiesCatalogueData.js +64 -64
  69. package/dist/utilitiesCataloguePermission.d.ts +39 -39
  70. package/dist/utilitiesCataloguePermission.js +84 -84
  71. package/dist/utilitiesNumericValues.d.ts +24 -24
  72. package/dist/utilitiesNumericValues.js +109 -109
  73. package/package.json +3 -3
@@ -1,49 +1,49 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { PromiseCache } from "@configura/web-utilities";
11
- import { makeProductKey, makeSelOptionsKey, } from "./utilitiesCatalogueData.js";
12
- /**
13
- * Wraps a getProduct function so that it caches for the time it lives.
14
- *
15
- * It has no cache invalidation or timeouts, so whatever ends up in the cache stays there
16
- * indefinitely. For this reason we recommend using this with caution.
17
- *
18
- * In our Example App we use it to cache the internal calls in additional products as in such a
19
- * product you tend to frequently get calls to same sub-product. Such as loading the same table leg
20
- * four times.
21
- */
22
- export function wrapWithGetProductCache(getProduct) {
23
- const cache = new PromiseCache();
24
- return (params) => __awaiter(this, void 0, void 0, function* () { return cache.get(makeProductKey(params, params.partNumber), () => getProduct(params)); });
25
- }
26
- /**
27
- * Wraps a postValidate function so that it caches for the time it lives.
28
- *
29
- * It has no cache invalidation or timeouts, so whatever ends up in the cache stays there
30
- * indefinitely. We recommend you do not use this unless you are really, really sure what you are
31
- * doing.
32
- *
33
- * The SDK uses it for additional products to avoid validating for example the same table legs four
34
- * times at the same time. There we make sure the this cache only stays in scope for one user
35
- * interaction. No longer than the actions caused by a user clicking an option.
36
- */
37
- export function wrapWithPostValidateCache(postValidate) {
38
- const cache = new PromiseCache();
39
- return (params, body) => __awaiter(this, void 0, void 0, function* () {
40
- return cache.get(`${makeProductKey(params, params.partNumber)}-${makeSelOptionsKey(body.selOptions)}`, () => postValidate(params, body));
41
- });
42
- }
43
- /** Does both wrapWithGetProductCache and wrapWithPostValidateCache. */
44
- export function wrapWithCache(loader) {
45
- return {
46
- getProduct: wrapWithGetProductCache(loader.getProduct.bind(loader)),
47
- postValidate: wrapWithPostValidateCache(loader.postValidate.bind(loader)),
48
- };
49
- }
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { PromiseCache } from "@configura/web-utilities";
11
+ import { makeProductKey, makeSelOptionsKey, } from "./utilitiesCatalogueData.js";
12
+ /**
13
+ * Wraps a getProduct function so that it caches for the time it lives.
14
+ *
15
+ * It has no cache invalidation or timeouts, so whatever ends up in the cache stays there
16
+ * indefinitely. For this reason we recommend using this with caution.
17
+ *
18
+ * In our Example App we use it to cache the internal calls in additional products as in such a
19
+ * product you tend to frequently get calls to same sub-product. Such as loading the same table leg
20
+ * four times.
21
+ */
22
+ export function wrapWithGetProductCache(getProduct) {
23
+ const cache = new PromiseCache();
24
+ return (params) => __awaiter(this, void 0, void 0, function* () { return cache.get(makeProductKey(params, params.partNumber), () => getProduct(params)); });
25
+ }
26
+ /**
27
+ * Wraps a postValidate function so that it caches for the time it lives.
28
+ *
29
+ * It has no cache invalidation or timeouts, so whatever ends up in the cache stays there
30
+ * indefinitely. We recommend you do not use this unless you are really, really sure what you are
31
+ * doing.
32
+ *
33
+ * The SDK uses it for additional products to avoid validating for example the same table legs four
34
+ * times at the same time. There we make sure the this cache only stays in scope for one user
35
+ * interaction. No longer than the actions caused by a user clicking an option.
36
+ */
37
+ export function wrapWithPostValidateCache(postValidate) {
38
+ const cache = new PromiseCache();
39
+ return (params, body) => __awaiter(this, void 0, void 0, function* () {
40
+ return cache.get(`${makeProductKey(params, params.partNumber)}-${makeSelOptionsKey(body.selOptions)}`, () => postValidate(params, body));
41
+ });
42
+ }
43
+ /** Does both wrapWithGetProductCache and wrapWithPostValidateCache. */
44
+ export function wrapWithCache(loader) {
45
+ return {
46
+ getProduct: wrapWithGetProductCache(loader.getProduct.bind(loader)),
47
+ postValidate: wrapWithPostValidateCache(loader.postValidate.bind(loader)),
48
+ };
49
+ }
@@ -0,0 +1,20 @@
1
+ import { SyncGroup } from "../CatalogueAPI.js";
2
+ import { _CfgOptionInternal } from "../productConfiguration/CfgOption.js";
3
+ import { SyncCode } from "./SyncGroupsHandler.js";
4
+ import { SyncGroupsTransaction } from "./SyncGroupsTransaction.js";
5
+ export declare class SyncGroupsApplier {
6
+ /**
7
+ * At load of a new product this is used to move it into a synced state
8
+ */
9
+ static init(transaction: SyncGroupsTransaction): Promise<boolean>;
10
+ /**
11
+ * At an active select of an option this is the entry point
12
+ */
13
+ static selectOption(transaction: SyncGroupsTransaction, option: _CfgOptionInternal, on: boolean): Promise<boolean>;
14
+ /**
15
+ * @returns undefined if the @syncGroup is undefined, false if
16
+ * the @mustSupport is not fulfilled or the syncCode if all is ok
17
+ */
18
+ static getSyncCode(syncGroup: SyncGroup | undefined, mustSupport: "push" | "pull"): SyncCode | undefined | false;
19
+ }
20
+ //# sourceMappingURL=SyncGroupsApplier.d.ts.map
@@ -0,0 +1,518 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { CfgProductBubbleMode } from "../CfgProduct.js";
11
+ import { SelectionType, } from "../productConfiguration/CfgFeature.js";
12
+ import { ProductConfigurationBubbleMode, } from "../productConfiguration/CfgOption.js";
13
+ import { SyncGroupsApplyMode } from "./SyncGroupsApplyMode.js";
14
+ export class SyncGroupsApplier {
15
+ /**
16
+ * At load of a new product this is used to move it into a synced state
17
+ */
18
+ static init(transaction) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return yield OntoSyncState.rootProduct(transaction);
21
+ });
22
+ }
23
+ /**
24
+ * At an active select of an option this is the entry point
25
+ */
26
+ static selectOption(transaction, option, on) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const feature = option.parent;
29
+ const productsToValidate = new Set();
30
+ let change = false;
31
+ // Here we sort of hand back to normal selecting. This is because after a normal selection has happened
32
+ // this can have brought features into scope which should pull or push to the Sync State. (Push if the
33
+ // SyncGroup is yet undefined)
34
+ // We use BubbleSelected as we can then delay the validation until after we have applied the sync group (if any)
35
+ if (yield feature.selectOption(option, on, ProductConfigurationBubbleMode.BubbleSelected)) {
36
+ productsToValidate.add(feature.parentProduct);
37
+ switch (feature.selectionType) {
38
+ case SelectionType.SelectOne:
39
+ transaction.addSelectOneFeatureAffected(feature);
40
+ break;
41
+ case SelectionType.SelectMany:
42
+ transaction.addSelectManyOptionAffected(option);
43
+ break;
44
+ default:
45
+ throw new Error("Should not happen");
46
+ }
47
+ change = true;
48
+ }
49
+ switch (feature.selectionType) {
50
+ case SelectionType.SelectOne:
51
+ OntoSyncState.selectOneFeature(transaction, feature, true, false);
52
+ break;
53
+ case SelectionType.SelectMany:
54
+ OntoSyncState.selectManyOption(transaction, option, true);
55
+ break;
56
+ }
57
+ if (yield SyncStateOnto.rootProduct(transaction, productsToValidate)) {
58
+ change = true;
59
+ }
60
+ return change;
61
+ });
62
+ }
63
+ /**
64
+ * @returns undefined if the @syncGroup is undefined, false if
65
+ * the @mustSupport is not fulfilled or the syncCode if all is ok
66
+ */
67
+ static getSyncCode(syncGroup, mustSupport) {
68
+ if (syncGroup === undefined) {
69
+ return undefined;
70
+ }
71
+ const { syncGroupCode, syncMethod } = syncGroup;
72
+ if (syncMethod !== mustSupport && syncMethod !== "twoWay") {
73
+ return false;
74
+ }
75
+ return syncGroupCode;
76
+ }
77
+ }
78
+ class SyncStateOnto {
79
+ /**
80
+ * Apply current sync groups on those who wants to listen until no more to settle
81
+ * @returns true if at least one Feature changed selected Option
82
+ */
83
+ static rootProduct(transaction, productsToValidate) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ productsToValidate = productsToValidate !== null && productsToValidate !== void 0 ? productsToValidate : new Set();
86
+ yield SyncStateOnto.product(transaction, transaction, productsToValidate);
87
+ if (productsToValidate.size === 0) {
88
+ // All settled, continue to pullPhase
89
+ return yield OntoSyncState.rootProduct(transaction);
90
+ }
91
+ if (transaction.isAborted) {
92
+ // We could exit in more places when the transaction has been aborted,
93
+ // but as revalidate is really the only thing that could be expensive /
94
+ // time consuming we only check here.
95
+ return false;
96
+ }
97
+ const promises = [];
98
+ for (const product of productsToValidate) {
99
+ promises.push(product._revalidate(CfgProductBubbleMode.ToRoot, transaction.productLoader));
100
+ }
101
+ const revalidationResults = yield Promise.all(promises);
102
+ // When using SyncGroups we require each and every validate call to validate on the
103
+ // server. Without SyncGroups we let these pass through, because any diversion from
104
+ // what you did chose should be immediately visible. With SyncGroups the end result
105
+ // is the combination of potentially many different validate calls, and so errors
106
+ // could accumulate and what is cause and effect be hard to know.
107
+ if (revalidationResults.some((r) => r.wasAborted || !r.requestDidValidate)) {
108
+ transaction.abort();
109
+ return false;
110
+ }
111
+ // Apply over again, to settle deeper down. Our theory is that the front of
112
+ // "settled" will move deeper and deeper into the tree.
113
+ yield SyncStateOnto.rootProduct(transaction, undefined);
114
+ // We had productsToValidate, and so there must have been a change
115
+ return true;
116
+ });
117
+ }
118
+ /**
119
+ * Applies the SyncState to the Product and it's AdditionalProducts (sub-products)
120
+ * @param productsToValidate To this all products that will need validation are added
121
+ */
122
+ static product(transaction, product, productsToValidate) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ const promises = [];
125
+ promises.push(SyncStateOnto.features(transaction, getFeaturesFromProduct(product), productsToValidate));
126
+ for (const additionalProduct of getAdditionalProducts(product)) {
127
+ promises.push(SyncStateOnto.product(transaction, additionalProduct, productsToValidate));
128
+ }
129
+ yield Promise.all(promises);
130
+ });
131
+ }
132
+ /**
133
+ * Applies the SyncState to an array of Features
134
+ * @param productsToValidate To this all products that will need validation are added
135
+ */
136
+ static features(transaction, features, productsToValidate) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ yield Promise.all(features.map((feature) => __awaiter(this, void 0, void 0, function* () {
139
+ switch (yield SyncStateOnto.feature(transaction, feature, productsToValidate)) {
140
+ case "stop":
141
+ return;
142
+ case "recurseDown":
143
+ yield SyncStateOnto.options(transaction, getSelectedOptions(feature), productsToValidate);
144
+ return;
145
+ }
146
+ })));
147
+ });
148
+ }
149
+ /**
150
+ * Applies the SyncState to an array of Options
151
+ * @param productsToValidate To this all products that will need validation are added
152
+ */
153
+ static options(transaction, options, productsToValidate) {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ yield Promise.all(options.map((option) => {
156
+ return SyncStateOnto.features(transaction, getFeaturesFromOption(option), productsToValidate);
157
+ }));
158
+ });
159
+ }
160
+ /**
161
+ * Applies the SyncState to a Feature
162
+ * @param productsToValidate To this all products that will need validation are added
163
+ * @returns Whether we shall stop recursing down (because we are in a state which we
164
+ * expect to be resolved later), we shall continue recursing down.
165
+ */
166
+ static feature(transaction, featureWithInitial, productsToValidate) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const feature = featureWithInitial.target;
169
+ const syncCode = SyncGroupsApplier.getSyncCode(feature.syncGroup, "pull");
170
+ if (syncCode === undefined) {
171
+ // Just continue down, features with no sync groups are always settled
172
+ return "recurseDown";
173
+ }
174
+ if (syncCode === false) {
175
+ // Here we only handle pull. Initializing those missing in SyncMap
176
+ // we do later. We wait until we have settled as much as we can with what
177
+ // we have now, to increase chances of the values being written to the
178
+ // SyncState being the right ones.
179
+ return "stop";
180
+ }
181
+ switch (feature.selectionType) {
182
+ case SelectionType.Group:
183
+ return "recurseDown";
184
+ case SelectionType.SelectOne:
185
+ return yield SyncStateOnto.selectOneFeature(transaction, syncCode, featureWithInitial, productsToValidate);
186
+ case SelectionType.SelectMany:
187
+ return yield SyncStateOnto.selectManyFeature(transaction, syncCode, featureWithInitial, productsToValidate);
188
+ }
189
+ });
190
+ }
191
+ /**
192
+ * Decides if the SyncState can be applied to the SelectOne Feature, and then changes
193
+ * the state of the Feature if so.
194
+ * @param syncCode What SyncGroup the Feature belongs to
195
+ * @param productsToValidate To this all products that will need validation are added
196
+ * @returns Whether we shall stop recursing down (because we are in a state which we
197
+ * expect to be resolved later), we shall continue recursing down.
198
+ */
199
+ static selectOneFeature(transaction, syncCode, featureWithInitial, productsToValidate) {
200
+ return __awaiter(this, void 0, void 0, function* () {
201
+ const feature = featureWithInitial.target;
202
+ const featureDidJustComeIntoScope = featureWithInitial.initial === undefined;
203
+ if (transaction.hasSelectOneFeatureBeenAffected(feature)) {
204
+ // This feature has already changed selection once for this transaction. We expect
205
+ // this to happen very rarely, as the algorithm should settle the selection tree
206
+ // further and further out. Nevertheless, this safeguard is needed to avoid infinite
207
+ // looping if for example the server would return the same data over and over.
208
+ return "recurseDown";
209
+ }
210
+ if (!(featureDidJustComeIntoScope || transaction.isSyncGroupAffectedForSelectOne(syncCode))) {
211
+ return "recurseDown";
212
+ }
213
+ const currentSyncGroupValue = transaction.syncState.getForSelectOne(syncCode);
214
+ if (currentSyncGroupValue === undefined) {
215
+ // This branch will have to be settled later. Don't go further down here.
216
+ return "stop";
217
+ }
218
+ const selectedOption = feature.selectedOptions[0];
219
+ if (selectedOption !== undefined && selectedOption.code === currentSyncGroupValue) {
220
+ // Settled, continue
221
+ return "recurseDown";
222
+ }
223
+ const optionToSelect = feature.options.find((o) => o.code === currentSyncGroupValue);
224
+ if (optionToSelect === undefined) {
225
+ // No option which can be selected (keep current value and recurse down)
226
+ return "recurseDown";
227
+ }
228
+ // Update the value. Validations are collected so that we do not
229
+ // send more than necessary. Do not recurse further as we will change
230
+ // the state and so what is selected now won't be selected then.
231
+ yield feature.selectOption(optionToSelect._internal, true, ProductConfigurationBubbleMode.ToRoot);
232
+ transaction.addSelectOneFeatureAffected(feature);
233
+ productsToValidate.add(feature.parentProduct);
234
+ return "stop";
235
+ });
236
+ }
237
+ /**
238
+ * Decides if the SyncState can be applied to Options in the SelectMany Feature, and
239
+ * then changes the state of the Options if so.
240
+ * @param syncCode What SyncGroup the Feature belongs to
241
+ * @param productsToValidate To this all products that will need validation are added
242
+ * @returns Always "stop" as recursion is handled internally. Return for consistency.
243
+ */
244
+ static selectManyFeature(transaction, syncCode, feature, productsToValidate) {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ const optionsToContinueDown = [];
247
+ for (const option of getOptions(feature)) {
248
+ switch (yield SyncStateOnto.selectManyOption(transaction, syncCode, option, productsToValidate)) {
249
+ case "stop":
250
+ continue;
251
+ case "recurseDown":
252
+ optionsToContinueDown.push(option);
253
+ continue;
254
+ }
255
+ }
256
+ yield SyncStateOnto.options(transaction, optionsToContinueDown, productsToValidate);
257
+ // stop as the method above handles the recursion down
258
+ return "stop";
259
+ });
260
+ }
261
+ /**
262
+ * Decides if the SyncState can be applied to the SelectMany Option, and then changes
263
+ * the state of the Option if so.
264
+ * @param syncCode What SyncGroup the Feature belongs to
265
+ * @param productsToValidate To this all products that will need validation are added
266
+ * @returns Whether we shall stop recursing down (because we are in a state which we
267
+ * expect to be resolved later), we shall continue recursing down.
268
+ */
269
+ static selectManyOption(transaction, syncCode, optionWithInitial, productsToValidate) {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ const option = optionWithInitial.target;
272
+ const featureDidJustComeIntoScope = optionWithInitial.initial === undefined;
273
+ const optionSelected = option.selected;
274
+ const recurseOrStopIfNoChange = optionSelected ? "recurseDown" : "stop";
275
+ if (transaction.hasSelectManyOptionBeenAffected(option)) {
276
+ // This option has already changed selection once for this transaction. We expect
277
+ // this to happen very rarely, as the algorithm should settle the selection tree
278
+ // further and further out. Nevertheless, this safeguard is needed to avoid infinite
279
+ // looping if for example the server would return the same data over and over.
280
+ return recurseOrStopIfNoChange;
281
+ }
282
+ const optionCode = option.code;
283
+ if (!(featureDidJustComeIntoScope ||
284
+ transaction.isSyncGroupAffectedForSelectMany(syncCode, optionCode))) {
285
+ return "recurseDown";
286
+ }
287
+ const syncGroupValueForOption = transaction.syncState.getForSelectMany(syncCode, optionCode);
288
+ if (syncGroupValueForOption === undefined) {
289
+ // The sync group has no opinion on this. If it is selected, just continue down.
290
+ return recurseOrStopIfNoChange;
291
+ }
292
+ if (syncGroupValueForOption === optionSelected) {
293
+ // We are in sync for this option
294
+ return recurseOrStopIfNoChange;
295
+ }
296
+ const feature = option.parent;
297
+ // Update the value. Validations are collected so that we do not
298
+ // send more than necessary. Do not recurse further as we will change
299
+ // the state and so what is selected now won't be selected then.
300
+ yield feature.selectOption(option, syncGroupValueForOption, ProductConfigurationBubbleMode.ToRoot);
301
+ transaction.addSelectManyOptionAffected(option);
302
+ productsToValidate.add(feature.parentProduct);
303
+ return "stop";
304
+ });
305
+ }
306
+ }
307
+ class OntoSyncState {
308
+ /**
309
+ * @returns true if at least one Feature changed it state. This is a bit counter intuitive,
310
+ * but as this method will hand over to productOntoSyncState if it updates the SyncState it
311
+ * can cause the Features to change. And we need to pass that information back to know when
312
+ * to stop
313
+ */
314
+ static rootProduct(transaction) {
315
+ return __awaiter(this, void 0, void 0, function* () {
316
+ if (!(yield OntoSyncState.product(transaction, transaction))) {
317
+ // All done!
318
+ return false;
319
+ }
320
+ return yield SyncStateOnto.rootProduct(transaction, undefined);
321
+ });
322
+ }
323
+ static product(transaction, product) {
324
+ return __awaiter(this, void 0, void 0, function* () {
325
+ let change = false;
326
+ if (yield OntoSyncState.features(transaction, getFeaturesFromProduct(product))) {
327
+ if (transaction.updateMode === SyncGroupsApplyMode.Strict) {
328
+ return true;
329
+ }
330
+ change = true;
331
+ }
332
+ for (const additionalProduct of getAdditionalProducts(product)) {
333
+ if (yield OntoSyncState.product(transaction, additionalProduct)) {
334
+ if (transaction.updateMode === SyncGroupsApplyMode.Strict) {
335
+ return true;
336
+ }
337
+ change = true;
338
+ }
339
+ }
340
+ return change;
341
+ });
342
+ }
343
+ static features(transaction, featureWithInitials) {
344
+ return __awaiter(this, void 0, void 0, function* () {
345
+ let change = false;
346
+ for (const featureWithInitial of featureWithInitials) {
347
+ const feature = featureWithInitial.target;
348
+ switch (feature.selectionType) {
349
+ case SelectionType.SelectOne:
350
+ if (OntoSyncState.selectOneFeature(transaction, feature, false, featureWithInitial.initial === undefined)) {
351
+ change = true;
352
+ }
353
+ break;
354
+ case SelectionType.SelectMany:
355
+ if (OntoSyncState.selectManyFeature(transaction, featureWithInitial)) {
356
+ change = true;
357
+ }
358
+ break;
359
+ }
360
+ if (change && transaction.updateMode === SyncGroupsApplyMode.Strict) {
361
+ return true;
362
+ }
363
+ if (yield OntoSyncState.options(transaction, getSelectedOptions(featureWithInitial))) {
364
+ if (transaction.updateMode === SyncGroupsApplyMode.Strict) {
365
+ return true;
366
+ }
367
+ change = true;
368
+ }
369
+ }
370
+ return change;
371
+ });
372
+ }
373
+ static options(transaction, options) {
374
+ return __awaiter(this, void 0, void 0, function* () {
375
+ let change = false;
376
+ for (const option of options) {
377
+ if (yield OntoSyncState.features(transaction, getFeaturesFromOption(option))) {
378
+ if (transaction.updateMode === SyncGroupsApplyMode.Strict) {
379
+ return true;
380
+ }
381
+ change = true;
382
+ }
383
+ }
384
+ return change;
385
+ });
386
+ }
387
+ static selectOneFeature(transaction, feature, activeSelectionForce, featureDidJustComeIntoScope) {
388
+ const selectionType = feature.selectionType;
389
+ if (selectionType !== SelectionType.SelectOne) {
390
+ throw new Error("can only be used for selectOne");
391
+ }
392
+ const syncCode = SyncGroupsApplier.getSyncCode(feature.syncGroup, "push");
393
+ if (syncCode === undefined || syncCode === false) {
394
+ return false;
395
+ }
396
+ if (transaction.isSyncGroupAffectedForSelectOne(syncCode)) {
397
+ return false;
398
+ }
399
+ const syncState = transaction.syncState;
400
+ const currentSyncGroupOptionCode = syncState.getForSelectOne(syncCode);
401
+ const option = feature.selectedOptions[0];
402
+ if (option === undefined) {
403
+ // Options with no default are never written
404
+ return false;
405
+ }
406
+ if (activeSelectionForce) {
407
+ // To make re-apply happen, even if it actually does not update the sync group
408
+ transaction.addSyncGroupAffectedForSelectOne(syncCode);
409
+ }
410
+ const optionCode = option.code;
411
+ if (optionCode === currentSyncGroupOptionCode) {
412
+ return false;
413
+ }
414
+ // featureDidJustComeIntoScope, in CET there is a feature that if a feature appears which can not be set
415
+ // to the current sync group value, then it will set in the opposite direction. Like if the sync group was
416
+ // empty. To avoid bouncing back and forth we will need to enforce that a sync group can only be updated
417
+ // once per transaction
418
+ if (!(activeSelectionForce ||
419
+ currentSyncGroupOptionCode === undefined ||
420
+ (featureDidJustComeIntoScope &&
421
+ feature.options.every((o) => currentSyncGroupOptionCode !== o.code)))) {
422
+ return false;
423
+ }
424
+ syncState.logDebug();
425
+ transaction.addSyncGroupAffectedForSelectOne(syncCode);
426
+ syncState.setForSelectOne(syncCode, optionCode);
427
+ return true;
428
+ }
429
+ static selectManyFeature(transaction, featureWithInitial) {
430
+ let change = false;
431
+ for (const optionWithInitial of getOptions(featureWithInitial)) {
432
+ if (OntoSyncState.selectManyOption(transaction, optionWithInitial.target, false)) {
433
+ if (transaction.updateMode === SyncGroupsApplyMode.Strict) {
434
+ return true;
435
+ }
436
+ change = true;
437
+ }
438
+ }
439
+ return change;
440
+ }
441
+ static selectManyOption(transaction, option, activeSelectionForce) {
442
+ const feature = option.parent;
443
+ if (feature.selectionType !== SelectionType.SelectMany) {
444
+ throw new Error("can only be used for selectMany");
445
+ }
446
+ const syncCode = SyncGroupsApplier.getSyncCode(feature.syncGroup, "push");
447
+ if (syncCode === undefined || syncCode === false) {
448
+ return false;
449
+ }
450
+ const optionCode = option.code;
451
+ if (transaction.isSyncGroupAffectedForSelectMany(syncCode, optionCode)) {
452
+ return false;
453
+ }
454
+ const syncState = transaction.syncState;
455
+ const optionSelected = option.selected;
456
+ const currentSyncGroupValue = syncState.getForSelectMany(syncCode, optionCode);
457
+ if (activeSelectionForce) {
458
+ // To make re-apply happen, even if it actually does not update the sync group
459
+ transaction.addSyncGroupAffectedForSelectMany(syncCode, optionCode);
460
+ }
461
+ // We only initialize if the option is selected or we force.
462
+ // activeSelectionForce = active selection by the user.
463
+ if (!(currentSyncGroupValue === undefined && optionSelected) &&
464
+ !(activeSelectionForce && currentSyncGroupValue !== optionSelected)) {
465
+ return false;
466
+ }
467
+ transaction.addSyncGroupAffectedForSelectMany(syncCode, optionCode);
468
+ syncState.setForSelectMany(syncCode, optionCode, optionSelected);
469
+ return true;
470
+ }
471
+ }
472
+ function getAdditionalProducts(product) {
473
+ const initial = product.initial;
474
+ return product.target.additionalProducts.map((childTarget) => {
475
+ const refKey = childTarget.refKey;
476
+ const childInitial = initial === null || initial === void 0 ? void 0 : initial.additionalProducts.find((p) => refKey === p.refKey);
477
+ return {
478
+ target: childTarget._internal,
479
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
480
+ };
481
+ });
482
+ }
483
+ function pairOptions(targets, initials) {
484
+ return targets.map((childTarget) => {
485
+ const key = childTarget.key;
486
+ const childInitial = initials === null || initials === void 0 ? void 0 : initials.find((o) => key === o.key);
487
+ return {
488
+ target: childTarget._internal,
489
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
490
+ };
491
+ });
492
+ }
493
+ function getSelectedOptions(feature) {
494
+ var _a;
495
+ return pairOptions(feature.target.selectedOptions, (_a = feature.initial) === null || _a === void 0 ? void 0 : _a.selectedOptions);
496
+ }
497
+ function getOptions(feature) {
498
+ var _a;
499
+ return pairOptions(feature.target.options, (_a = feature.initial) === null || _a === void 0 ? void 0 : _a.options);
500
+ }
501
+ function pairFeatures(targets, initials) {
502
+ return targets.map((childTarget) => {
503
+ const key = childTarget.key;
504
+ const childInitial = initials === null || initials === void 0 ? void 0 : initials.find((f) => key === f.key);
505
+ return {
506
+ target: childTarget._internal,
507
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
508
+ };
509
+ });
510
+ }
511
+ function getFeaturesFromProduct(product) {
512
+ var _a;
513
+ return pairFeatures(product.target.configuration.features, (_a = product.initial) === null || _a === void 0 ? void 0 : _a.configuration.features);
514
+ }
515
+ function getFeaturesFromOption(option) {
516
+ var _a;
517
+ return pairFeatures(option.target.features, (_a = option.initial) === null || _a === void 0 ? void 0 : _a.features);
518
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The SyncGroupsApplyMode controls how many SyncGroups can be updated in the SyncState for one
3
+ * run of rootProductOntoSyncState (C.). "Fast" is default and will make any SyncGroup that
4
+ * should be updated update before running syncStateOntoRootProduct (B.) This way several
5
+ * SyncGroups can be applied in one go before sending validation calls to the server. The
6
+ * downside of this is that is not exactly how CET (the desktop software) works. CET will apply
7
+ * Features to the SyncState as soon as it gets the chance. This is the same as running with
8
+ * "Strict" here. This will potentially generate a lot more validate calls, increasing delay
9
+ * and cost. So if you don't have to, stick with "Fast".
10
+ */
11
+ export declare enum SyncGroupsApplyMode {
12
+ Fast = "Fast",
13
+ Strict = "Strict"
14
+ }
15
+ //# sourceMappingURL=SyncGroupsApplyMode.d.ts.map
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The SyncGroupsApplyMode controls how many SyncGroups can be updated in the SyncState for one
3
+ * run of rootProductOntoSyncState (C.). "Fast" is default and will make any SyncGroup that
4
+ * should be updated update before running syncStateOntoRootProduct (B.) This way several
5
+ * SyncGroups can be applied in one go before sending validation calls to the server. The
6
+ * downside of this is that is not exactly how CET (the desktop software) works. CET will apply
7
+ * Features to the SyncState as soon as it gets the chance. This is the same as running with
8
+ * "Strict" here. This will potentially generate a lot more validate calls, increasing delay
9
+ * and cost. So if you don't have to, stick with "Fast".
10
+ */
11
+ export var SyncGroupsApplyMode;
12
+ (function (SyncGroupsApplyMode) {
13
+ SyncGroupsApplyMode["Fast"] = "Fast";
14
+ SyncGroupsApplyMode["Strict"] = "Strict";
15
+ })(SyncGroupsApplyMode || (SyncGroupsApplyMode = {}));