@reventlessdev/reventless-core 3.0.0-alpha.325 → 3.0.0-alpha.327

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 (80) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +7 -7
  3. package/src/Message.res +46 -0
  4. package/src/Message.res.mjs +45 -0
  5. package/src/Projection.res +269 -131
  6. package/src/Projection.res.mjs +338 -224
  7. package/src/ViewChange.res +194 -0
  8. package/src/ViewChange.res.mjs +235 -0
  9. package/src/adapter/Runtime/AggregateRuntime_Builder_Common.res.mjs +5 -5
  10. package/src/adapter/Runtime/EventCollectorRuntime_Builder_Single.res.mjs +2 -2
  11. package/src/admin/Platform_Admin_Callback.res.mjs +1 -1
  12. package/src/components/Aggregate/Aggregate_Callback.res +93 -18
  13. package/src/components/Aggregate/Aggregate_Callback.res.mjs +54 -32
  14. package/src/components/AutomationSlice/AutomationSlice_Builder.res.mjs +1 -1
  15. package/src/components/AutomationSlice/AutomationSlice_Callback.res +29 -9
  16. package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +35 -20
  17. package/src/components/CommandGenerator/CommandGenerator_Callback.res +47 -6
  18. package/src/components/CommandGenerator/CommandGenerator_Callback.res.mjs +41 -4
  19. package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
  20. package/src/components/CommandTopic/CommandTopic_Callback.res.mjs +1 -1
  21. package/src/components/CommandTopic/CommandTopic_Helpers.res +64 -26
  22. package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +37 -17
  23. package/src/components/CommandTopic/CommandTopic_Operations.res +2 -0
  24. package/src/components/CommandTopic/CommandTopic_Operations.res.mjs +2 -2
  25. package/src/components/Counter/Counter_Callback.res.mjs +3 -3
  26. package/src/components/Counter/Counter_Operations.res.mjs +4 -4
  27. package/src/components/DcbEventLog/DcbEventLog_Operations.res.mjs +3 -3
  28. package/src/components/EventLog/EventLog_Operations.res +1 -0
  29. package/src/components/EventLog/EventLog_Operations.res.mjs +3 -3
  30. package/src/components/EventMapper/EventMapper_Callback.res +4 -0
  31. package/src/components/EventMapper/EventMapper_Callback.res.mjs +13 -12
  32. package/src/components/EventTopic/EventTopic_Operations.res +2 -0
  33. package/src/components/EventTopic/EventTopic_Operations.res.mjs +2 -2
  34. package/src/components/Extension/Extension_Operations.res +4 -0
  35. package/src/components/Extension/Extension_Operations.res.mjs +8 -8
  36. package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -2
  37. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +2 -0
  38. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +9 -8
  39. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +1 -1
  40. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Webhook.res.mjs +1 -1
  41. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
  42. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +7 -3
  43. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +33 -12
  44. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +30 -17
  45. package/src/components/ReadModel/ReadModel_Callback.res +24 -3
  46. package/src/components/ReadModel/ReadModel_Callback.res.mjs +15 -3
  47. package/src/components/SideEffectHandler/SideEffectHandler_Callback.res +2 -0
  48. package/src/components/SideEffectHandler/SideEffectHandler_Callback.res.mjs +6 -6
  49. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +43 -7
  50. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +38 -20
  51. package/src/components/StateViewSlice/StateViewSlice_Builder.res +16 -2
  52. package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +15 -4
  53. package/src/components/StateViewSlice/StateViewSlice_Callback.res +35 -4
  54. package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +12 -4
  55. package/src/plugin/component/Plugin_Callback.res.mjs +2 -2
  56. package/src/plugin/connect/PluginExtensionPoint_Plugin.res.mjs +6 -6
  57. package/src/util/CommandPublisher.res.mjs +9 -9
  58. package/src/util/EffectLogger.res +111 -42
  59. package/src/util/EffectLogger.res.mjs +72 -30
  60. package/src/util/FTPHandler.res.mjs +6 -6
  61. package/src/util/Logger.res +231 -35
  62. package/src/util/Logger.res.mjs +263 -42
  63. package/src/util/ScheduleOps.res.mjs +4 -4
  64. package/src/util/Util_AdapterRuntime.res.mjs +2 -2
  65. package/src/util/Util_Promise.res.mjs +1 -1
  66. package/src/util/Util_QueryDbRuntime.res.mjs +1 -1
  67. package/src/util/Validation.res.mjs +1 -1
  68. package/tests/commandgenerator/OwnerActingTest.res +3 -1
  69. package/tests/commandgenerator/OwnerStampingTest.res +65 -0
  70. package/tests/commandgenerator/OwnerStampingTest.res.mjs +62 -0
  71. package/tests/commandtopic/CommandOutcomeHookTest.res +99 -2
  72. package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +120 -0
  73. package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
  74. package/tests/logger/ComponentLineIdsTest.res +97 -0
  75. package/tests/logger/ComponentLineIdsTest.res.mjs +118 -0
  76. package/tests/logger/LogFormatTest.res.mjs +5 -5
  77. package/tests/logger/LoggerSinkTest.res +205 -0
  78. package/tests/logger/LoggerSinkTest.res.mjs +209 -0
  79. package/tests/projection/ViewChangeTest.res +173 -0
  80. package/tests/projection/ViewChangeTest.res.mjs +292 -0
@@ -3,6 +3,7 @@
3
3
  import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
4
4
  import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
5
5
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
+ import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
6
7
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
7
8
  import * as Effect from "effect/Effect";
8
9
  import * as Stream$1 from "effect/Stream";
@@ -11,6 +12,7 @@ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
12
  import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
12
13
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
13
14
  import * as Lifecycle$Reventless from "@reventlessdev/reventless-spec/src/types/Lifecycle.res.mjs";
15
+ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
14
16
  import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
15
17
  import * as Logger$ReventlessCore from "./util/Logger.res.mjs";
16
18
  import * as DisplayName$Reventless from "@reventlessdev/reventless-spec/src/components/DisplayName.res.mjs";
@@ -204,9 +206,14 @@ function rewriteJsonAction(action, at, stateSchema) {
204
206
  }
205
207
  }
206
208
 
207
- async function applyChanges(action, id, beforeStates, afterStates, param, param$1) {
209
+ function mergeSources(a, b) {
210
+ return a.concat(b.filter(s => !a.some(other => other.msgId === s.msgId)));
211
+ }
212
+
213
+ async function applyChanges(action, id, beforeStates, afterStates, param, param$1, $staropt$star) {
208
214
  let getSubId = param$1.getSubId;
209
215
  let subIdField = param$1.subIdField;
216
+ let onRow = $staropt$star !== undefined ? $staropt$star : (param, param$1, param$2, param$3) => {};
210
217
  let beforeCount = beforeStates.length;
211
218
  let beforeSubIds = Belt_SetString.fromArray(beforeStates.map(state => getSubId(state)));
212
219
  let afterCount = afterStates.length;
@@ -251,12 +258,27 @@ async function applyChanges(action, id, beforeStates, afterStates, param, param$
251
258
  let err = result.find(r => r.TAG !== "Ok");
252
259
  if (err !== undefined) {
253
260
  return err;
254
- } else {
255
- return {
256
- TAG: "Ok",
257
- _0: undefined
258
- };
259
261
  }
262
+ let beforeBySubId = {};
263
+ beforeStates.forEach(before => {
264
+ beforeBySubId[getSubId(before)] = before;
265
+ });
266
+ afterStates.forEach(after => {
267
+ let subId = getSubId(after);
268
+ let before = beforeBySubId[subId];
269
+ if (before === undefined) {
270
+ return onRow(subId, undefined, Primitive_option.some(after), "Created");
271
+ }
272
+ let before$1 = Primitive_option.valFromOption(before);
273
+ if (Primitive_object.notequal(before$1, after)) {
274
+ return onRow(subId, Primitive_option.some(before$1), Primitive_option.some(after), "Changed");
275
+ }
276
+ });
277
+ deletedSubIds.forEach(subId => onRow(subId, beforeBySubId[subId], undefined, "Removed"));
278
+ return {
279
+ TAG: "Ok",
280
+ _0: undefined
281
+ };
260
282
  }
261
283
 
262
284
  function stateToString(state) {
@@ -267,11 +289,36 @@ function statesToString(states) {
267
289
  return states.map(stateToString).join(", ");
268
290
  }
269
291
 
270
- async function handleAction($staropt$star, action, operations, subIdConfig) {
292
+ async function handleAction($staropt$star, $staropt$star$1, onChange, action, operations, subIdConfig) {
271
293
  let saveBatch = operations.saveBatch;
272
294
  let save = operations.save;
273
295
  let loadStream = operations.loadStream;
274
296
  let comp = $staropt$star !== undefined ? $staropt$star : "Projection";
297
+ let sources = $staropt$star$1 !== undefined ? $staropt$star$1 : [];
298
+ let report = (key, subId, before, after, kind) => Stdlib_Option.forEach(onChange, f => {
299
+ try {
300
+ return f({
301
+ key: key,
302
+ subId: subId,
303
+ kind: kind,
304
+ before: before,
305
+ after: after,
306
+ sources: sources
307
+ });
308
+ } catch (raw_exn) {
309
+ let exn = Primitive_exceptions.internalToException(raw_exn);
310
+ return log.warn(comp, undefined, `a view-change listener failed: ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown"));
311
+ }
312
+ });
313
+ let subIdOf = state => Stdlib_Option.map(subIdConfig, param => param.getSubId(state));
314
+ let after = (result, reportAll) => {
315
+ if (result.TAG === "Ok") {
316
+ if (onChange !== undefined) {
317
+ reportAll();
318
+ }
319
+ }
320
+ return result;
321
+ };
275
322
  let logAction = makeStr => log.debugLazy(comp, () => `handling action: ` + makeStr());
276
323
  let loadAtMost = (n, id) => Effect.runPromise(Effect.catchAll(Effect.map(Stream.runCollect(Stream$1.take(loadStream(id), n)), states => ({
277
324
  TAG: "Ok",
@@ -299,7 +346,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
299
346
  let state = action._1;
300
347
  let id = action._0;
301
348
  logAction(() => `Create(` + id + `, ` + stateToString(state) + `)`);
302
- return await save(id, state, "Init", undefined);
349
+ return after(await save(id, state, "Init", undefined), () => report(id, undefined, undefined, Primitive_option.some(state), "Created"));
303
350
  case "CreateMany" :
304
351
  let batch = action._0.map(param => [
305
352
  param[0],
@@ -307,7 +354,9 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
307
354
  undefined
308
355
  ]);
309
356
  logAction(() => `CreateMany(` + batch.map(param => `(` + param[0] + `,` + stateToString(param[1]) + `)`).join(", ") + `)`);
310
- return await saveBatch(batch);
357
+ return after(await saveBatch(batch), () => {
358
+ batch.forEach(param => report(param[0], undefined, undefined, Primitive_option.some(param[1]), "Created"));
359
+ });
311
360
  case "Update" :
312
361
  let id$1 = action._0;
313
362
  let states = await loadAtMost(2, id$1);
@@ -337,7 +386,11 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
337
386
  let oldState = states$1[0];
338
387
  let newState = action._1(oldState);
339
388
  logAction(() => `Update(` + id$1 + `, ` + stateToString(oldState) + ` => ` + stateToString(newState) + `)`);
340
- return await save(id$1, newState, "Overwrite", undefined);
389
+ return after(await save(id$1, newState, "Overwrite", undefined), () => {
390
+ if (Primitive_object.notequal(newState, oldState)) {
391
+ return report(id$1, undefined, Primitive_option.some(oldState), Primitive_option.some(newState), "Changed");
392
+ }
393
+ });
341
394
  case "UpdateWithDefault" :
342
395
  let $$default = action._1;
343
396
  let id$2 = action._0;
@@ -354,13 +407,17 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
354
407
  };
355
408
  } else {
356
409
  logAction(() => `UpdateWithDefault(` + id$2 + `, default: ` + stateToString($$default) + `)`);
357
- return await save(id$2, $$default, "Init", undefined);
410
+ return after(await save(id$2, $$default, "Init", undefined), () => report(id$2, undefined, undefined, Primitive_option.some($$default), "Created"));
358
411
  }
359
412
  }
360
413
  let oldState$1 = states$3[0];
361
414
  let newState$1 = action._2(oldState$1);
362
415
  logAction(() => `UpdateWithDefault(` + id$2 + `, ` + stateToString(oldState$1) + ` => ` + stateToString(newState$1) + `)`);
363
- return await save(id$2, newState$1, "Overwrite", undefined);
416
+ return after(await save(id$2, newState$1, "Overwrite", undefined), () => {
417
+ if (Primitive_object.notequal(newState$1, oldState$1)) {
418
+ return report(id$2, undefined, Primitive_option.some(oldState$1), Primitive_option.some(newState$1), "Changed");
419
+ }
420
+ });
364
421
  }
365
422
  let err = states$2._0;
366
423
  logAction(() => `UpdateWithDefault Error: Couldn't load oldState(s) for ` + id$2 + `: ` + QueryDb$ReventlessCore.storageErrorToString(err) + `)`);
@@ -372,7 +429,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
372
429
  let state$1 = action._1;
373
430
  let id$3 = action._0;
374
431
  logAction(() => `Set(` + id$3 + `, ` + stateToString(state$1) + `)`);
375
- return await save(id$3, state$1, "Any", undefined);
432
+ return after(await save(id$3, state$1, "Any", undefined), () => report(id$3, undefined, undefined, Primitive_option.some(state$1), "Changed"));
376
433
  case "SetMany" :
377
434
  let set = action._1;
378
435
  let batch$1 = action._0.map(id => [
@@ -381,22 +438,29 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
381
438
  undefined
382
439
  ]);
383
440
  logAction(() => `SetMany(` + batch$1.map(param => `(` + param[0] + `,` + stateToString(param[1]) + `)`).join(", ") + `)`);
384
- return await saveBatch(batch$1);
441
+ return after(await saveBatch(batch$1), () => {
442
+ batch$1.forEach(param => report(param[0], undefined, undefined, Primitive_option.some(param[1]), "Changed"));
443
+ });
385
444
  case "Delete" :
386
445
  let id$4 = action._0;
387
446
  logAction(() => `Delete(` + id$4 + `)`);
388
- return await operations.delete(id$4, undefined);
447
+ return after(await operations.delete(id$4, undefined), () => report(id$4, undefined, undefined, undefined, "Removed"));
389
448
  case "DeleteMany" :
390
449
  let ids = action._0;
391
450
  logAction(() => `DeleteMany(` + ids.join(", ") + `)`);
392
- return await operations.deleteBatch(ids.map(id => [
451
+ return after(await operations.deleteBatch(ids.map(id => [
393
452
  id,
394
453
  undefined
395
- ]));
454
+ ])), () => {
455
+ ids.forEach(id => report(id, undefined, undefined, undefined, "Removed"));
456
+ });
396
457
  case "CreateMultiState" :
397
458
  let states$4 = action._1;
398
459
  let id$5 = action._0;
399
460
  logAction(() => `CreateMultiState(` + id$5 + `, ` + states$4.map(stateToString).join(", ") + `)`);
461
+ let reportAll = () => {
462
+ states$4.forEach(state => report(id$5, subIdOf(state), undefined, Primitive_option.some(state), "Created"));
463
+ };
400
464
  let len$2 = states$4.length;
401
465
  if (len$2 !== 1) {
402
466
  if (len$2 === 0) {
@@ -410,10 +474,10 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
410
474
  state,
411
475
  undefined
412
476
  ]);
413
- return await saveBatch(batch$2);
477
+ return after(await saveBatch(batch$2), reportAll);
414
478
  }
415
479
  let state$2 = states$4[0];
416
- return await save(id$5, state$2, "Init", undefined);
480
+ return after(await save(id$5, state$2, "Init", undefined), reportAll);
417
481
  case "UpdateMultiState" :
418
482
  let id$6 = action._0;
419
483
  let match = await loadAll(id$6);
@@ -421,7 +485,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
421
485
  if (subIdConfig !== undefined) {
422
486
  let states$5 = match._0;
423
487
  let afterStates = action._1(states$5);
424
- return await applyChanges("UpdateMultiState", id$6, states$5, afterStates, operations, subIdConfig);
488
+ return await applyChanges("UpdateMultiState", id$6, states$5, afterStates, operations, subIdConfig, (subId, before, after, kind) => report(id$6, subId, before, after, kind));
425
489
  }
426
490
  } else if (subIdConfig !== undefined) {
427
491
  let err$1 = match._0;
@@ -508,8 +572,17 @@ function actionsWithId(action) {
508
572
  return [];
509
573
  }
510
574
 
511
- function groupActionsById(actions) {
512
- let allActionsWithId = actions.map(actionsWithId).flat();
575
+ function groupSourcedById(actions) {
576
+ let allActionsWithId = actions.map(param => {
577
+ let sources = param.sources;
578
+ return actionsWithId(param.action).map(param => [
579
+ param[0],
580
+ {
581
+ action: param[1],
582
+ sources: sources
583
+ }
584
+ ]);
585
+ }).flat();
513
586
  let groups = {};
514
587
  allActionsWithId.forEach(param => {
515
588
  let action = param[1];
@@ -527,235 +600,263 @@ function groupActionsById(actions) {
527
600
  ]);
528
601
  }
529
602
 
530
- function optimizeActions(actions) {
531
- return Stdlib_Array.reduce(actions, [], (optimizedActions, action) => {
532
- let optimizedActionsCount = optimizedActions.length;
533
- if (optimizedActionsCount === 0) {
534
- return [action];
535
- }
536
- let lastAction = optimizedActions[optimizedActionsCount - 1 | 0];
537
- let previousActions = optimizedActions.slice(0, optimizedActionsCount - 1 | 0);
538
- if (typeof lastAction === "object") {
539
- switch (lastAction.TAG) {
540
- case "Create" :
541
- let state1 = lastAction._1;
542
- let id1 = lastAction._0;
543
- if (typeof action === "object") {
544
- switch (action.TAG) {
545
- case "Create" :
546
- if (id1 === action._0) {
547
- log.warn("Projection", undefined, `optimizing 2 sequential Create for id=` + id1 + `, ignoring second: ` + Stdlib_Option.getOr(JSON.stringify(action._1), "?"));
548
- return previousActions.concat([{
549
- TAG: "Create",
550
- _0: id1,
551
- _1: state1
552
- }]);
553
- }
554
- break;
555
- case "Update" :
556
- if (id1 === action._0) {
557
- return previousActions.concat([{
558
- TAG: "Create",
559
- _0: id1,
560
- _1: action._1(state1)
561
- }]);
562
- }
563
- break;
564
- case "UpdateWithDefault" :
565
- if (id1 === action._0) {
566
- return previousActions.concat([{
567
- TAG: "Create",
568
- _0: id1,
569
- _1: action._2(state1)
570
- }]);
571
- }
572
- break;
573
- case "Set" :
574
- if (id1 === action._0) {
575
- log.warn("Projection", undefined, `optimizing Set after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
576
- return previousActions.concat([{
577
- TAG: "Set",
578
- _0: id1,
579
- _1: action._1
580
- }]);
581
- }
582
- break;
583
- case "Delete" :
584
- if (id1 === action._0) {
585
- log.warn("Projection", undefined, `optimizing Delete after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
586
- return previousActions.concat([{
587
- TAG: "Delete",
588
- _0: id1
589
- }]);
590
- }
591
- break;
592
- }
593
- }
594
- break;
595
- case "Update" :
596
- let f = lastAction._1;
597
- let id1$1 = lastAction._0;
598
- if (typeof action === "object") {
599
- switch (action.TAG) {
600
- case "Update" :
601
- if (id1$1 === action._0) {
602
- let g = action._1;
603
- return previousActions.concat([{
604
- TAG: "Update",
605
- _0: id1$1,
606
- _1: state => g(f(state))
607
- }]);
608
- }
609
- break;
610
- case "UpdateWithDefault" :
611
- if (id1$1 === action._0) {
612
- let g$1 = action._2;
613
- return previousActions.concat([{
614
- TAG: "UpdateWithDefault",
615
- _0: id1$1,
616
- _1: action._1,
617
- _2: state => g$1(f(state))
618
- }]);
619
- }
620
- break;
621
- case "Set" :
622
- if (id1$1 === action._0) {
623
- log.warn("Projection", undefined, `optimizing Set after Update for id=` + id1$1 + `, ignoring the Update`);
624
- return previousActions.concat([{
625
- TAG: "Set",
626
- _0: id1$1,
627
- _1: action._1
628
- }]);
629
- }
630
- break;
631
- case "Delete" :
632
- if (id1$1 === action._0) {
633
- log.warn("Projection", undefined, `optimizing Delete after Update for id=` + id1$1 + `, ignoring the Update`);
634
- return previousActions.concat([{
635
- TAG: "Delete",
636
- _0: id1$1
637
- }]);
638
- }
639
- break;
640
- }
603
+ function groupActionsById(actions) {
604
+ return groupSourcedById(actions.map(action => ({
605
+ action: action,
606
+ sources: []
607
+ }))).map(param => [
608
+ param[0],
609
+ param[1].map(s => s.action)
610
+ ]);
611
+ }
612
+
613
+ function mergeActions(lastAction, action) {
614
+ if (typeof lastAction === "object") {
615
+ switch (lastAction.TAG) {
616
+ case "Create" :
617
+ let state1 = lastAction._1;
618
+ let id1 = lastAction._0;
619
+ if (typeof action === "object") {
620
+ switch (action.TAG) {
621
+ case "Create" :
622
+ if (id1 === action._0) {
623
+ log.warn("Projection", undefined, `optimizing 2 sequential Create for id=` + id1 + `, ignoring second: ` + Stdlib_Option.getOr(JSON.stringify(action._1), "?"));
624
+ return {
625
+ TAG: "Create",
626
+ _0: id1,
627
+ _1: state1
628
+ };
629
+ }
630
+ break;
631
+ case "Update" :
632
+ if (id1 === action._0) {
633
+ return {
634
+ TAG: "Create",
635
+ _0: id1,
636
+ _1: action._1(state1)
637
+ };
638
+ }
639
+ break;
640
+ case "UpdateWithDefault" :
641
+ if (id1 === action._0) {
642
+ return {
643
+ TAG: "Create",
644
+ _0: id1,
645
+ _1: action._2(state1)
646
+ };
647
+ }
648
+ break;
649
+ case "Set" :
650
+ if (id1 === action._0) {
651
+ log.warn("Projection", undefined, `optimizing Set after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
652
+ return {
653
+ TAG: "Set",
654
+ _0: id1,
655
+ _1: action._1
656
+ };
657
+ }
658
+ break;
659
+ case "Delete" :
660
+ if (id1 === action._0) {
661
+ log.warn("Projection", undefined, `optimizing Delete after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
662
+ return {
663
+ TAG: "Delete",
664
+ _0: id1
665
+ };
666
+ }
667
+ break;
641
668
  }
642
- break;
643
- case "UpdateWithDefault" :
644
- let f$1 = lastAction._2;
645
- let defaultState1 = lastAction._1;
646
- let id1$2 = lastAction._0;
647
- if (typeof action === "object") {
648
- switch (action.TAG) {
649
- case "Create" :
650
- if (id1$2 === action._0) {
651
- log.warn("Projection", undefined, `optimizing Create after UpdateWithDefault for id=` + id1$2 + `, ignoring the Create`);
652
- return previousActions.concat([{
653
- TAG: "UpdateWithDefault",
654
- _0: id1$2,
655
- _1: defaultState1,
656
- _2: f$1
657
- }]);
658
- }
659
- break;
660
- case "Update" :
661
- if (id1$2 === action._0) {
662
- let g$2 = action._1;
663
- return previousActions.concat([{
664
- TAG: "UpdateWithDefault",
665
- _0: id1$2,
666
- _1: g$2(defaultState1),
667
- _2: state => g$2(f$1(state))
668
- }]);
669
- }
670
- break;
671
- case "UpdateWithDefault" :
672
- if (id1$2 === action._0) {
673
- let g$3 = action._2;
674
- return previousActions.concat([{
675
- TAG: "UpdateWithDefault",
676
- _0: id1$2,
677
- _1: g$3(defaultState1),
678
- _2: state => g$3(f$1(state))
679
- }]);
680
- }
681
- break;
682
- case "Set" :
683
- if (id1$2 === action._0) {
684
- log.warn("Projection", undefined, `optimizing Set after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
685
- return previousActions.concat([{
686
- TAG: "Set",
687
- _0: id1$2,
688
- _1: action._1
689
- }]);
690
- }
691
- break;
692
- case "Delete" :
693
- if (id1$2 === action._0) {
694
- log.warn("Projection", undefined, `optimizing Delete after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
695
- return previousActions.concat([{
696
- TAG: "Delete",
697
- _0: id1$2
698
- }]);
699
- }
700
- break;
701
- }
669
+ }
670
+ break;
671
+ case "Update" :
672
+ let f = lastAction._1;
673
+ let id1$1 = lastAction._0;
674
+ if (typeof action === "object") {
675
+ switch (action.TAG) {
676
+ case "Update" :
677
+ if (id1$1 === action._0) {
678
+ let g = action._1;
679
+ return {
680
+ TAG: "Update",
681
+ _0: id1$1,
682
+ _1: state => g(f(state))
683
+ };
684
+ }
685
+ break;
686
+ case "UpdateWithDefault" :
687
+ if (id1$1 === action._0) {
688
+ let g$1 = action._2;
689
+ return {
690
+ TAG: "UpdateWithDefault",
691
+ _0: id1$1,
692
+ _1: action._1,
693
+ _2: state => g$1(f(state))
694
+ };
695
+ }
696
+ break;
697
+ case "Set" :
698
+ if (id1$1 === action._0) {
699
+ log.warn("Projection", undefined, `optimizing Set after Update for id=` + id1$1 + `, ignoring the Update`);
700
+ return {
701
+ TAG: "Set",
702
+ _0: id1$1,
703
+ _1: action._1
704
+ };
705
+ }
706
+ break;
707
+ case "Delete" :
708
+ if (id1$1 === action._0) {
709
+ log.warn("Projection", undefined, `optimizing Delete after Update for id=` + id1$1 + `, ignoring the Update`);
710
+ return {
711
+ TAG: "Delete",
712
+ _0: id1$1
713
+ };
714
+ }
715
+ break;
702
716
  }
703
- break;
704
- case "Delete" :
705
- if (typeof action === "object" && action.TAG === "Create") {
706
- let id1$3 = lastAction._0;
707
- if (id1$3 === action._0) {
708
- return previousActions.concat([{
717
+ }
718
+ break;
719
+ case "UpdateWithDefault" :
720
+ let f$1 = lastAction._2;
721
+ let defaultState1 = lastAction._1;
722
+ let id1$2 = lastAction._0;
723
+ if (typeof action === "object") {
724
+ switch (action.TAG) {
725
+ case "Create" :
726
+ if (id1$2 === action._0) {
727
+ log.warn("Projection", undefined, `optimizing Create after UpdateWithDefault for id=` + id1$2 + `, ignoring the Create`);
728
+ return {
729
+ TAG: "UpdateWithDefault",
730
+ _0: id1$2,
731
+ _1: defaultState1,
732
+ _2: f$1
733
+ };
734
+ }
735
+ break;
736
+ case "Update" :
737
+ if (id1$2 === action._0) {
738
+ let g$2 = action._1;
739
+ return {
740
+ TAG: "UpdateWithDefault",
741
+ _0: id1$2,
742
+ _1: g$2(defaultState1),
743
+ _2: state => g$2(f$1(state))
744
+ };
745
+ }
746
+ break;
747
+ case "UpdateWithDefault" :
748
+ if (id1$2 === action._0) {
749
+ let g$3 = action._2;
750
+ return {
751
+ TAG: "UpdateWithDefault",
752
+ _0: id1$2,
753
+ _1: g$3(defaultState1),
754
+ _2: state => g$3(f$1(state))
755
+ };
756
+ }
757
+ break;
758
+ case "Set" :
759
+ if (id1$2 === action._0) {
760
+ log.warn("Projection", undefined, `optimizing Set after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
761
+ return {
709
762
  TAG: "Set",
710
- _0: id1$3,
763
+ _0: id1$2,
711
764
  _1: action._1
712
- }]);
713
- }
765
+ };
766
+ }
767
+ break;
768
+ case "Delete" :
769
+ if (id1$2 === action._0) {
770
+ log.warn("Projection", undefined, `optimizing Delete after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
771
+ return {
772
+ TAG: "Delete",
773
+ _0: id1$2
774
+ };
775
+ }
776
+ break;
714
777
  }
715
- break;
716
- case "UpdateMultiState" :
717
- if (typeof action === "object" && action.TAG === "UpdateMultiState") {
718
- let id1$4 = lastAction._0;
719
- if (id1$4 === action._0) {
720
- let g$4 = action._1;
721
- let f$2 = lastAction._1;
722
- return previousActions.concat([{
723
- TAG: "UpdateMultiState",
724
- _0: id1$4,
725
- _1: state => g$4(f$2(state))
726
- }]);
727
- }
778
+ }
779
+ break;
780
+ case "Delete" :
781
+ if (typeof action === "object" && action.TAG === "Create") {
782
+ let id1$3 = lastAction._0;
783
+ if (id1$3 === action._0) {
784
+ return {
785
+ TAG: "Set",
786
+ _0: id1$3,
787
+ _1: action._1
788
+ };
728
789
  }
729
- break;
730
- }
790
+ }
791
+ break;
792
+ case "UpdateMultiState" :
793
+ if (typeof action === "object" && action.TAG === "UpdateMultiState") {
794
+ let id1$4 = lastAction._0;
795
+ if (id1$4 === action._0) {
796
+ let g$4 = action._1;
797
+ let f$2 = lastAction._1;
798
+ return {
799
+ TAG: "UpdateMultiState",
800
+ _0: id1$4,
801
+ _1: state => g$4(f$2(state))
802
+ };
803
+ }
804
+ }
805
+ break;
806
+ }
807
+ }
808
+ log.warn("Projection", undefined, `actions not optimized: ` + Stdlib_Option.getOr(JSON.stringify(lastAction), "?") + ` + ` + Stdlib_Option.getOr(JSON.stringify(action), "?"));
809
+ }
810
+
811
+ function optimizeSourced(actions) {
812
+ return Stdlib_Array.reduce(actions, [], (optimized, next) => {
813
+ let count = optimized.length;
814
+ if (count === 0) {
815
+ return [next];
816
+ }
817
+ let last = optimized[count - 1 | 0];
818
+ let previous = optimized.slice(0, count - 1 | 0);
819
+ let action = mergeActions(last.action, next.action);
820
+ if (action !== undefined) {
821
+ return previous.concat([{
822
+ action: action,
823
+ sources: mergeSources(last.sources, next.sources)
824
+ }]);
825
+ } else {
826
+ return optimized.concat([next]);
731
827
  }
732
- log.warn("Projection", undefined, `actions not optimized: ` + Stdlib_Option.getOr(JSON.stringify(lastAction), "?") + ` + ` + Stdlib_Option.getOr(JSON.stringify(action), "?"));
733
- return optimizedActions.concat([action]);
734
828
  });
735
829
  }
736
830
 
737
- async function handleActions(compOpt, actions, operations, subIdConfig) {
831
+ function optimizeActions(actions) {
832
+ return optimizeSourced(actions.map(action => ({
833
+ action: action,
834
+ sources: []
835
+ }))).map(s => s.action);
836
+ }
837
+
838
+ async function handleSourcedActions(compOpt, onChange, actions, operations, subIdConfig) {
738
839
  let comp = compOpt !== undefined ? compOpt : "Projection";
739
840
  let handleActionsForId = async (actions, id) => {
740
841
  let actionCount = actions.length;
741
842
  if (actionCount > 1) {
742
843
  log.debug(comp, undefined, `handleActions: optimizing ` + actionCount.toString() + ` actions for id=` + id);
743
844
  }
744
- let optimizedActions = optimizeActions(actions);
845
+ let optimizedActions = optimizeSourced(actions);
745
846
  let optimizedActionCount = optimizedActions.length;
746
847
  log.debug(comp, undefined, `handleActions: id=` + id + ` actions=` + optimizedActionCount.toString());
747
848
  return await Stdlib_Array.reduce(optimizedActions, Promise.resolve({
748
849
  TAG: "Ok",
749
850
  _0: undefined
750
- }), async (p, action) => {
851
+ }), async (p, param) => {
751
852
  let err = await p;
752
853
  if (err.TAG !== "Ok") {
753
854
  log.error(comp, undefined, `storage error: ` + JSON.stringify(Message$ReventlessCore.encode(err._0, QueryDb$ReventlessInfra.storageErrorSchema)));
754
855
  }
755
- return await handleAction(comp, action, operations, subIdConfig);
856
+ return await handleAction(comp, param.sources, onChange, param.action, operations, subIdConfig);
756
857
  });
757
858
  };
758
- let results = await Promise.all(groupActionsById(actions).map(param => handleActionsForId(param[1], param[0])));
859
+ let results = await Promise.all(groupSourcedById(actions).map(param => handleActionsForId(param[1], param[0])));
759
860
  let errors = Stdlib_Array.filterMap(results, x => {
760
861
  if (x.TAG === "Ok") {
761
862
  return;
@@ -770,6 +871,14 @@ async function handleActions(compOpt, actions, operations, subIdConfig) {
770
871
  return Stdlib_JsError.throwWithMessage(`Projection.handleActions failed with ` + count.toString() + ` errors: ` + errors.map(QueryDb$ReventlessCore.storageErrorToString).join(","));
771
872
  }
772
873
 
874
+ function handleActions(compOpt, onChange, actions, operations, subIdConfig) {
875
+ let comp = compOpt !== undefined ? compOpt : "Projection";
876
+ return handleSourcedActions(comp, onChange, actions.map(action => ({
877
+ action: action,
878
+ sources: []
879
+ })), operations, subIdConfig);
880
+ }
881
+
773
882
  let $$Set;
774
883
 
775
884
  export {
@@ -783,13 +892,18 @@ export {
783
892
  trailOverlays,
784
893
  rewriteTrail,
785
894
  rewriteJsonAction,
895
+ mergeSources,
786
896
  applyChanges,
787
897
  stateToString,
788
898
  statesToString,
789
899
  handleAction,
790
900
  actionsWithId,
901
+ groupSourcedById,
791
902
  groupActionsById,
903
+ mergeActions,
904
+ optimizeSourced,
792
905
  optimizeActions,
906
+ handleSourcedActions,
793
907
  handleActions,
794
908
  }
795
909
  /* log Not a pure module */